diff --git a/static/js/narrow.js b/static/js/narrow.js index da18e1d647..2c61b51f3b 100644 --- a/static/js/narrow.js +++ b/static/js/narrow.js @@ -209,6 +209,11 @@ exports.Filter = Filter; var current_filter; +// A small concession to unit testing follows: +exports._set_current_filter = function (filter) { + current_filter = filter; +}; + exports.active = function () { return current_filter !== undefined; }; diff --git a/static/js/subs.js b/static/js/subs.js index 80eecab930..a763d492ac 100644 --- a/static/js/subs.js +++ b/static/js/subs.js @@ -863,3 +863,6 @@ exports.show_and_focus_on_narrow = function () { return exports; }()); +if (typeof module !== 'undefined') { + module.exports = subs; +} diff --git a/tools/test-js-with-node b/tools/test-js-with-node index 5f4a14bf96..6d1b198a4e 100755 --- a/tools/test-js-with-node +++ b/tools/test-js-with-node @@ -11,5 +11,6 @@ NODEJS=$(which nodejs || which node) $NODEJS util.js $NODEJS message_list.js $NODEJS message_tour.js +$NODEJS narrow.js $NODEJS unread.js $NODEJS search_suggestion.js diff --git a/zerver/tests/frontend/node/narrow.js b/zerver/tests/frontend/node/narrow.js new file mode 100644 index 0000000000..35ecda4391 --- /dev/null +++ b/zerver/tests/frontend/node/narrow.js @@ -0,0 +1,36 @@ +var assert = require('assert'); + +(function set_up_dependencies () { + global._ = require('third/underscore/underscore.js'); + global.util = require('js/util.js'); + global.narrow = require('js/narrow.js'); + global.$ = function () {}; // for subs.js + global.subs = require('js/subs.js'); +}()); + +var narrow = global.narrow; + +(function test_parse_and_unparse() { + var string ='stream:Foo topic:Bar yo'; + var operators = [['stream', 'Foo'], ['topic', 'Bar'], ['search', 'yo']]; + + assert.deepEqual(narrow.parse(string), operators); + + string = 'stream:foo topic:bar yo'; + assert.deepEqual(narrow.unparse(operators), string); +}()); + +(function test_stream() { + var operators = [['stream', 'Foo'], ['topic', 'Bar'], ['search', 'yo']]; + narrow._set_current_filter(new narrow.Filter(operators)); + + assert.equal(narrow.stream(), 'foo'); +}()); + +(function test_operators() { + var operators = [['stream', 'Foo'], ['topic', 'Bar'], ['search', 'yo']]; + var canonical_operators = [['stream', 'foo'], ['topic', 'bar'], ['search', 'yo']]; + narrow._set_current_filter(new narrow.Filter(operators)); + + assert.deepEqual(narrow.operators(), canonical_operators); +}());