node tests: Add coverage to hashchange.js.

This commit is contained in:
Steve Howell 2018-04-11 11:14:39 +00:00 committed by Tim Abbott
parent 1568df352d
commit b1fd86c5c7
1 changed files with 256 additions and 0 deletions

View File

@ -3,6 +3,36 @@ zrequire('hash_util');
zrequire('hashchange'); zrequire('hashchange');
zrequire('stream_data'); zrequire('stream_data');
set_global('document', 'document-stub');
set_global('history', {});
set_global('window', {
location: {
protocol: 'http:',
host: 'example.com',
},
});
set_global('admin', {});
set_global('drafts', {});
set_global('favicon', {});
set_global('floating_recipient_bar', {});
set_global('info_overlay', {});
set_global('narrow', {});
set_global('overlays', {});
set_global('settings', {});
set_global('subs', {});
set_global('ui_util', {});
function blueslip_wrap(f) {
return function (e) {
return f(e);
};
}
set_global('blueslip', {
wrap_function: blueslip_wrap,
});
(function test_operators_round_trip() { (function test_operators_round_trip() {
var operators; var operators;
var hash; var hash;
@ -79,3 +109,229 @@ zrequire('stream_data');
hash = hashchange.operators_to_hash(operators); hash = hashchange.operators_to_hash(operators);
assert.equal(hash, '#narrow/pm-with/42-alice'); assert.equal(hash, '#narrow/pm-with/42-alice');
}()); }());
function stub_trigger(f) {
set_global('$', () => {
return {
trigger: f,
};
});
$.Event = (name) => {
assert.equal(name, 'zuliphashchange.zulip');
};
}
function test_helper() {
var events = [];
var narrow_terms;
function stub(module_name, func_name) {
global[module_name][func_name] = () => {
events.push(module_name + '.' + func_name);
};
}
stub('admin', 'setup_page');
stub('drafts', 'launch');
stub('favicon', 'reset');
stub('floating_recipient_bar', 'update');
stub('narrow', 'deactivate');
stub('overlays', 'close_for_hash_change');
stub('settings', 'setup_page');
stub('subs', 'launch');
stub('ui_util', 'blur_active_element');
stub_trigger(() => { events.push('trigger event'); });
ui_util.change_tab_to = (hash) => {
events.push('change_tab_to ' + hash);
};
narrow.activate = (terms) => {
narrow_terms = terms;
events.push('narrow.activate');
};
info_overlay.show = (name) => {
events.push('info: ' + name);
};
return {
clear_events: () => {
events = [];
},
assert_events: (expected_events) => {
assert.deepEqual(expected_events, events);
},
get_narrow_terms: () => {
return narrow_terms;
},
};
}
(function test_hash_interactions() {
var helper = test_helper();
window.location.hash = '#';
helper.clear_events();
hashchange.initialize();
helper.assert_events([
'overlays.close_for_hash_change',
'trigger event',
'change_tab_to #home',
'narrow.deactivate',
'floating_recipient_bar.update',
]);
helper.clear_events();
window.onhashchange();
helper.assert_events([
'overlays.close_for_hash_change',
'trigger event',
'change_tab_to #home',
'narrow.deactivate',
'floating_recipient_bar.update',
]);
window.location.hash = '#narrow/stream/Denmark';
helper.clear_events();
window.onhashchange();
helper.assert_events([
'overlays.close_for_hash_change',
'trigger event',
'change_tab_to #home',
'narrow.activate',
'floating_recipient_bar.update',
]);
var terms = helper.get_narrow_terms();
assert.equal(terms[0].operand, 'Denmark');
window.location.hash = '#narrow';
helper.clear_events();
window.onhashchange();
helper.assert_events([
'overlays.close_for_hash_change',
'trigger event',
'change_tab_to #home',
'narrow.activate',
'floating_recipient_bar.update',
]);
terms = helper.get_narrow_terms();
assert.equal(terms.length, 0);
window.location.hash = '#streams/whatever';
helper.clear_events();
window.onhashchange();
helper.assert_events([
'overlays.close_for_hash_change',
'subs.launch',
]);
window.location.hash = '#keyboard-shortcuts/whatever';
helper.clear_events();
window.onhashchange();
helper.assert_events([
'overlays.close_for_hash_change',
'trigger event',
'info: keyboard-shortcuts',
]);
window.location.hash = '#markdown-help/whatever';
helper.clear_events();
window.onhashchange();
helper.assert_events([
'overlays.close_for_hash_change',
'trigger event',
'info: markdown-help',
]);
window.location.hash = '#search-operators/whatever';
helper.clear_events();
window.onhashchange();
helper.assert_events([
'overlays.close_for_hash_change',
'trigger event',
'info: search-operators',
]);
window.location.hash = '#drafts';
helper.clear_events();
window.onhashchange();
helper.assert_events([
'overlays.close_for_hash_change',
'drafts.launch',
]);
window.location.hash = '#settings/alert-words';
helper.clear_events();
window.onhashchange();
helper.assert_events([
'overlays.close_for_hash_change',
'settings.setup_page',
'admin.setup_page',
]);
window.location.hash = '#organization/user-list-admin';
helper.clear_events();
window.onhashchange();
helper.assert_events([
'settings.setup_page',
'admin.setup_page',
]);
var called_back;
helper.clear_events();
hashchange.exit_overlay(() => {
called_back = true;
});
helper.assert_events([
'ui_util.blur_active_element',
]);
assert(called_back);
}());
(function test_save_narrow() {
var helper = test_helper();
var operators = [
{operator: 'is', operand: 'private'},
];
hashchange.save_narrow(operators);
helper.assert_events([
'trigger event',
'favicon.reset',
]);
assert.equal(window.location.hash, '#narrow/is/private');
var url_pushed;
global.history.pushState = (state, title, url) => {
url_pushed = url;
};
operators = [
{operator: 'is', operand: 'starred'},
];
helper.clear_events();
hashchange.save_narrow(operators);
helper.assert_events([
'trigger event',
'favicon.reset',
]);
assert.equal(url_pushed, 'http://example.com/#narrow/is/starred');
}());