tests: Improve hashchange coverage for operators.

Test round tripping of operators in hashchange.js.
This commit is contained in:
Steve Howell 2017-01-05 13:04:33 -08:00 committed by Tim Abbott
parent da84ff3746
commit 030c6649ae
2 changed files with 18 additions and 4 deletions

View File

@ -1,8 +1,9 @@
var hashchange = require('js/hashchange.js');
(function test_basics() {
(function test_operators_round_trip() {
var operators;
var hash;
var narrow;
operators = [
{operator: 'stream', operand: 'devel'},
@ -11,10 +12,23 @@ var hashchange = require('js/hashchange.js');
hash = hashchange.operators_to_hash(operators);
assert.equal(hash, '#narrow/stream/devel/topic/algol');
narrow = hashchange.parse_narrow(hash.split('/'));
assert.deepEqual(narrow, [
{operator: 'stream', operand: 'devel', negated: false},
{operator: 'topic', operand: 'algol', negated: false},
]);
operators = [
{operator: 'stream', operand: 'devel'},
{operator: 'topic', operand: 'visual c++', negated: true},
];
hash = hashchange.operators_to_hash(operators);
assert.equal(hash, '#narrow/stream/devel/-topic/visual.20c.2B.2B');
narrow = hashchange.parse_narrow(hash.split('/'));
assert.deepEqual(narrow, [
{operator: 'stream', operand: 'devel', negated: false},
{operator: 'topic', operand: 'visual c++', negated: true},
]);
}());

View File

@ -82,7 +82,7 @@ exports.save_narrow = function (operators) {
exports.changehash(new_hash);
};
function parse_narrow(hash) {
exports.parse_narrow = function (hash) {
var i;
var operators = [];
for (i=1; i<hash.length; i+=2) {
@ -102,7 +102,7 @@ function parse_narrow(hash) {
}
}
return operators;
}
};
function activate_home_tab() {
ui.change_tab_to("#home");
@ -135,7 +135,7 @@ function do_hashchange(from_reload) {
switch (hash[0]) {
case "#narrow":
ui.change_tab_to("#home");
var operators = parse_narrow(hash);
var operators = exports.parse_narrow(hash);
if (operators === undefined) {
// If the narrow URL didn't parse, clear
// window.location.hash and send them to the home tab