From cef72fd8b8271a25a98c31b7f2dbf13f313b986c Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Fri, 9 Aug 2013 09:30:18 -0400 Subject: [PATCH] Add tests for the Filter class. (imported from commit ef0917bd7911c5cc6f6d20d356c156d483ba723f) --- tools/test-js-with-node | 1 + zerver/tests/frontend/node/filter.js | 49 ++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 zerver/tests/frontend/node/filter.js diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 6d1b198a4e..8785d072a3 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -8,6 +8,7 @@ export NODE_PATH=$STATIC_DIR NODEJS=$(which nodejs || which node) +$NODEJS filter.js $NODEJS util.js $NODEJS message_list.js $NODEJS message_tour.js diff --git a/zerver/tests/frontend/node/filter.js b/zerver/tests/frontend/node/filter.js new file mode 100644 index 0000000000..1cce023602 --- /dev/null +++ b/zerver/tests/frontend/node/filter.js @@ -0,0 +1,49 @@ +var assert = require('assert'); + +(function set_up_dependencies () { + global._ = require('third/underscore/underscore.js'); + + // An upcoming change is to put Filter in its own module, but + // for now it still lives in narrow.js. (I'm waiting for a big + // commit from Zev to hit master first. Once that happens, + // I will make js/filter.js, update the references here, and + // hopefully remember to delete this comment.) + global.narrow = require('js/narrow.js'); + global.$ = function () {}; // for subs.js to load + global.subs = require('js/subs.js'); + global.page_params = { + domain: 'zulip.com' + }; +}()); + +var Filter = global.narrow.Filter; + +(function test_basics() { + var operators = [['stream', 'foo'], ['topic', 'bar']]; + var filter = new Filter(operators); + + assert.deepEqual(filter.operators(), operators); + + var predicate = filter.predicate(); + assert(predicate({type: 'stream', stream: 'foo', subject: 'bar'})); + assert(!predicate({type: 'stream', stream: 'foo', subject: 'whatever'})); + + assert.deepEqual(filter.operands('stream'), ['foo']); + + assert(filter.has_operator('stream')); + assert(!filter.has_operator('search')); + + assert(filter.has_operand('stream', 'foo')); + assert(!filter.has_operand('stream', 'nada')); + + assert(!filter.is_search()); + assert(filter.can_apply_locally()); + + operators = [['stream', 'foo'], ['topic', 'bar'], ['search', 'pizza']]; + filter = new Filter(operators); + + assert(filter.is_search()); + assert(! filter.can_apply_locally()); +}()); + +