2018-05-31 23:55:28 +02:00
|
|
|
global.patch_builtin('window', {
|
|
|
|
location: {
|
|
|
|
protocol: 'http:',
|
|
|
|
host: 'example.com',
|
|
|
|
},
|
|
|
|
});
|
2017-11-08 17:22:58 +01:00
|
|
|
zrequire('people');
|
|
|
|
zrequire('hash_util');
|
|
|
|
zrequire('hashchange');
|
2018-02-15 21:02:47 +01:00
|
|
|
zrequire('stream_data');
|
2017-03-18 18:29:20 +01:00
|
|
|
|
2018-04-11 13:14:39 +02:00
|
|
|
set_global('document', 'document-stub');
|
|
|
|
set_global('history', {});
|
|
|
|
|
|
|
|
set_global('admin', {});
|
|
|
|
set_global('drafts', {});
|
|
|
|
set_global('favicon', {});
|
|
|
|
set_global('floating_recipient_bar', {});
|
|
|
|
set_global('info_overlay', {});
|
2018-12-04 19:08:26 +01:00
|
|
|
set_global('message_viewport', {});
|
2018-04-11 13:14:39 +02:00
|
|
|
set_global('narrow', {});
|
|
|
|
set_global('overlays', {});
|
|
|
|
set_global('settings', {});
|
|
|
|
set_global('subs', {});
|
|
|
|
set_global('ui_util', {});
|
2018-07-09 20:08:09 +02:00
|
|
|
set_global('blueslip', global.make_zblueslip());
|
2018-04-11 13:14:39 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('operators_round_trip', () => {
|
2014-02-19 22:19:16 +01:00
|
|
|
var operators;
|
|
|
|
var hash;
|
2017-01-05 22:04:33 +01:00
|
|
|
var narrow;
|
2014-02-19 22:19:16 +01:00
|
|
|
|
|
|
|
operators = [
|
|
|
|
{operator: 'stream', operand: 'devel'},
|
2016-12-03 23:17:57 +01:00
|
|
|
{operator: 'topic', operand: 'algol'},
|
2014-02-19 22:19:16 +01:00
|
|
|
];
|
2018-08-04 16:33:28 +02:00
|
|
|
hash = hash_util.operators_to_hash(operators);
|
2014-02-19 22:19:16 +01:00
|
|
|
assert.equal(hash, '#narrow/stream/devel/topic/algol');
|
|
|
|
|
2018-12-04 23:24:03 +01:00
|
|
|
narrow = hash_util.parse_narrow(hash.split('/'));
|
2017-01-05 22:04:33 +01:00
|
|
|
assert.deepEqual(narrow, [
|
|
|
|
{operator: 'stream', operand: 'devel', negated: false},
|
|
|
|
{operator: 'topic', operand: 'algol', negated: false},
|
|
|
|
]);
|
|
|
|
|
2014-02-19 22:19:16 +01:00
|
|
|
operators = [
|
|
|
|
{operator: 'stream', operand: 'devel'},
|
2016-12-03 23:17:57 +01:00
|
|
|
{operator: 'topic', operand: 'visual c++', negated: true},
|
2014-02-19 22:19:16 +01:00
|
|
|
];
|
2018-08-04 16:33:28 +02:00
|
|
|
hash = hash_util.operators_to_hash(operators);
|
2014-02-19 22:19:16 +01:00
|
|
|
assert.equal(hash, '#narrow/stream/devel/-topic/visual.20c.2B.2B');
|
2017-01-05 22:04:33 +01:00
|
|
|
|
2018-12-04 23:24:03 +01:00
|
|
|
narrow = hash_util.parse_narrow(hash.split('/'));
|
2017-01-05 22:04:33 +01:00
|
|
|
assert.deepEqual(narrow, [
|
|
|
|
{operator: 'stream', operand: 'devel', negated: false},
|
|
|
|
{operator: 'topic', operand: 'visual c++', negated: true},
|
|
|
|
]);
|
|
|
|
|
2018-02-15 21:02:47 +01:00
|
|
|
// test new encodings, where we have a stream id
|
|
|
|
var florida_stream = {
|
|
|
|
name: 'Florida, USA',
|
|
|
|
stream_id: 987,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(florida_stream.name, florida_stream);
|
|
|
|
operators = [
|
|
|
|
{operator: 'stream', operand: 'Florida, USA'},
|
|
|
|
];
|
2018-08-04 16:33:28 +02:00
|
|
|
hash = hash_util.operators_to_hash(operators);
|
2018-02-15 21:02:47 +01:00
|
|
|
assert.equal(hash, '#narrow/stream/987-Florida.2C-USA');
|
2018-12-04 23:24:03 +01:00
|
|
|
narrow = hash_util.parse_narrow(hash.split('/'));
|
2018-02-15 21:02:47 +01:00
|
|
|
assert.deepEqual(narrow, [
|
|
|
|
{operator: 'stream', operand: 'Florida, USA', negated: false},
|
2018-05-15 15:59:50 +02:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test('operators_trailing_slash', () => {
|
|
|
|
var hash;
|
|
|
|
var narrow;
|
|
|
|
|
|
|
|
hash = '#narrow/stream/devel/topic/algol/';
|
2018-12-04 23:24:03 +01:00
|
|
|
narrow = hash_util.parse_narrow(hash.split('/'));
|
2018-05-15 15:59:50 +02:00
|
|
|
assert.deepEqual(narrow, [
|
|
|
|
{operator: 'stream', operand: 'devel', negated: false},
|
|
|
|
{operator: 'topic', operand: 'algol', negated: false},
|
2018-02-15 21:02:47 +01:00
|
|
|
]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2017-01-19 03:53:50 +01:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('people_slugs', () => {
|
2017-01-19 03:53:50 +01:00
|
|
|
var operators;
|
|
|
|
var hash;
|
|
|
|
var narrow;
|
|
|
|
|
|
|
|
var alice = {
|
|
|
|
email: 'alice@example.com',
|
|
|
|
user_id: 42,
|
|
|
|
full_name: 'Alice Smith',
|
|
|
|
};
|
|
|
|
|
|
|
|
people.add(alice);
|
|
|
|
operators = [
|
|
|
|
{operator: 'sender', operand: 'alice@example.com'},
|
|
|
|
];
|
2018-08-04 16:33:28 +02:00
|
|
|
hash = hash_util.operators_to_hash(operators);
|
2017-01-19 03:53:50 +01:00
|
|
|
assert.equal(hash, '#narrow/sender/42-alice');
|
2018-12-04 23:24:03 +01:00
|
|
|
narrow = hash_util.parse_narrow(hash.split('/'));
|
2017-01-19 03:53:50 +01:00
|
|
|
assert.deepEqual(narrow, [
|
|
|
|
{operator: 'sender', operand: 'alice@example.com', negated: false},
|
|
|
|
]);
|
|
|
|
|
|
|
|
operators = [
|
|
|
|
{operator: 'pm-with', operand: 'alice@example.com'},
|
|
|
|
];
|
2018-08-04 16:33:28 +02:00
|
|
|
hash = hash_util.operators_to_hash(operators);
|
2017-01-19 03:53:50 +01:00
|
|
|
assert.equal(hash, '#narrow/pm-with/42-alice');
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-06 20:18:56 +01:00
|
|
|
stub('admin', 'launch');
|
2018-04-11 13:14:39 +02:00
|
|
|
stub('drafts', 'launch');
|
|
|
|
stub('favicon', 'reset');
|
|
|
|
stub('floating_recipient_bar', 'update');
|
2018-12-04 19:08:26 +01:00
|
|
|
stub('message_viewport', 'stop_auto_scrolling');
|
2018-04-11 13:14:39 +02:00
|
|
|
stub('narrow', 'deactivate');
|
|
|
|
stub('overlays', 'close_for_hash_change');
|
2018-12-06 20:18:56 +01:00
|
|
|
stub('settings', 'launch');
|
2018-04-11 13:14:39 +02:00
|
|
|
stub('subs', 'launch');
|
|
|
|
stub('ui_util', 'blur_active_element');
|
|
|
|
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('hash_interactions', () => {
|
2018-04-11 13:14:39 +02:00
|
|
|
var helper = test_helper();
|
|
|
|
|
|
|
|
window.location.hash = '#';
|
|
|
|
|
|
|
|
helper.clear_events();
|
|
|
|
hashchange.initialize();
|
|
|
|
helper.assert_events([
|
|
|
|
'overlays.close_for_hash_change',
|
2018-12-04 19:08:26 +01:00
|
|
|
'message_viewport.stop_auto_scrolling',
|
2018-04-11 13:14:39 +02:00
|
|
|
'change_tab_to #home',
|
|
|
|
'narrow.deactivate',
|
|
|
|
'floating_recipient_bar.update',
|
|
|
|
]);
|
|
|
|
|
|
|
|
helper.clear_events();
|
|
|
|
window.onhashchange();
|
|
|
|
helper.assert_events([
|
|
|
|
'overlays.close_for_hash_change',
|
2018-12-04 19:08:26 +01:00
|
|
|
'message_viewport.stop_auto_scrolling',
|
2018-04-11 13:14:39 +02:00
|
|
|
'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',
|
2018-12-04 19:08:26 +01:00
|
|
|
'message_viewport.stop_auto_scrolling',
|
2018-04-11 13:14:39 +02:00
|
|
|
'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',
|
2018-12-04 19:08:26 +01:00
|
|
|
'message_viewport.stop_auto_scrolling',
|
2018-04-11 13:14:39 +02:00
|
|
|
'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',
|
2018-12-04 19:08:26 +01:00
|
|
|
'message_viewport.stop_auto_scrolling',
|
2018-04-11 13:14:39 +02:00
|
|
|
'info: keyboard-shortcuts',
|
|
|
|
]);
|
|
|
|
|
2018-08-29 19:15:25 +02:00
|
|
|
window.location.hash = '#message-formatting/whatever';
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
helper.clear_events();
|
|
|
|
window.onhashchange();
|
|
|
|
helper.assert_events([
|
|
|
|
'overlays.close_for_hash_change',
|
2018-12-04 19:08:26 +01:00
|
|
|
'message_viewport.stop_auto_scrolling',
|
2018-08-29 19:15:25 +02:00
|
|
|
'info: message-formatting',
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
window.location.hash = '#search-operators/whatever';
|
|
|
|
|
|
|
|
helper.clear_events();
|
|
|
|
window.onhashchange();
|
|
|
|
helper.assert_events([
|
|
|
|
'overlays.close_for_hash_change',
|
2018-12-04 19:08:26 +01:00
|
|
|
'message_viewport.stop_auto_scrolling',
|
2018-04-11 13:14:39 +02:00
|
|
|
'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',
|
2018-12-06 20:18:56 +01:00
|
|
|
'settings.launch',
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
window.location.hash = '#organization/user-list-admin';
|
|
|
|
|
|
|
|
helper.clear_events();
|
|
|
|
window.onhashchange();
|
|
|
|
helper.assert_events([
|
2018-12-03 18:11:14 +01:00
|
|
|
'overlays.close_for_hash_change',
|
2018-12-06 20:18:56 +01:00
|
|
|
'admin.launch',
|
2018-04-11 13:14:39 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
var called_back;
|
|
|
|
|
|
|
|
helper.clear_events();
|
|
|
|
hashchange.exit_overlay(() => {
|
|
|
|
called_back = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
helper.assert_events([
|
|
|
|
'ui_util.blur_active_element',
|
|
|
|
]);
|
|
|
|
assert(called_back);
|
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-04-11 13:14:39 +02:00
|
|
|
|
2018-05-15 12:40:07 +02:00
|
|
|
run_test('save_narrow', () => {
|
2018-04-11 13:14:39 +02:00
|
|
|
var helper = test_helper();
|
|
|
|
|
|
|
|
var operators = [
|
|
|
|
{operator: 'is', operand: 'private'},
|
|
|
|
];
|
|
|
|
|
2018-12-06 22:59:05 +01:00
|
|
|
blueslip.set_test_data('warn', 'browser does not support pushState');
|
2018-04-11 13:14:39 +02:00
|
|
|
hashchange.save_narrow(operators);
|
2018-12-06 22:59:05 +01:00
|
|
|
blueslip.clear_test_data();
|
|
|
|
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
2018-12-04 19:08:26 +01:00
|
|
|
'message_viewport.stop_auto_scrolling',
|
2018-04-11 13:14:39 +02:00
|
|
|
'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([
|
2018-12-04 19:08:26 +01:00
|
|
|
'message_viewport.stop_auto_scrolling',
|
2018-04-11 13:14:39 +02:00
|
|
|
'favicon.reset',
|
|
|
|
]);
|
|
|
|
assert.equal(url_pushed, 'http://example.com/#narrow/is/starred');
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|