2019-07-09 04:30:51 +02:00
|
|
|
set_global('$', global.make_zjquery());
|
|
|
|
const window_stub = $.create('window-stub');
|
2019-07-25 09:13:22 +02:00
|
|
|
set_global('location', {
|
|
|
|
protocol: 'http:',
|
|
|
|
host: 'example.com',
|
2018-05-31 23:55:28 +02:00
|
|
|
});
|
2019-07-25 09:13:22 +02:00
|
|
|
set_global('to_$', () => window_stub);
|
2020-02-04 21:50:55 +01:00
|
|
|
|
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-05-15 12:40:07 +02:00
|
|
|
run_test('operators_round_trip', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let operators;
|
|
|
|
let hash;
|
|
|
|
let 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
|
2019-11-02 00:06:25 +01:00
|
|
|
const florida_stream = {
|
2018-02-15 21:02:47 +01:00
|
|
|
name: 'Florida, USA',
|
|
|
|
stream_id: 987,
|
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(florida_stream);
|
2018-02-15 21:02:47 +01:00
|
|
|
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', () => {
|
2019-10-26 00:11:05 +02:00
|
|
|
const hash = '#narrow/stream/devel/topic/algol/';
|
|
|
|
const 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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
let operators;
|
|
|
|
let hash;
|
2017-01-19 03:53:50 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const alice = {
|
2017-01-19 03:53:50 +01:00
|
|
|
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');
|
2019-10-26 00:11:05 +02:00
|
|
|
const 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() {
|
2019-11-02 00:06:25 +01:00
|
|
|
let events = [];
|
|
|
|
let narrow_terms;
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const helper = test_helper();
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
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();
|
2019-07-09 04:30:51 +02:00
|
|
|
$(window).trigger($.Event('hashchange', {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
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();
|
2019-07-09 04:30:51 +02:00
|
|
|
$(window).trigger($.Event('hashchange', {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
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',
|
|
|
|
]);
|
2019-11-02 00:06:25 +01:00
|
|
|
let terms = helper.get_narrow_terms();
|
2018-04-11 13:14:39 +02:00
|
|
|
assert.equal(terms[0].operand, 'Denmark');
|
|
|
|
|
|
|
|
window.location.hash = '#narrow';
|
|
|
|
|
|
|
|
helper.clear_events();
|
2019-07-09 04:30:51 +02:00
|
|
|
$(window).trigger($.Event('hashchange', {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
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();
|
2019-07-09 04:30:51 +02:00
|
|
|
$(window).trigger($.Event('hashchange', {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
|
|
|
'overlays.close_for_hash_change',
|
|
|
|
'subs.launch',
|
|
|
|
]);
|
|
|
|
|
|
|
|
window.location.hash = '#keyboard-shortcuts/whatever';
|
|
|
|
|
|
|
|
helper.clear_events();
|
2019-07-09 04:30:51 +02:00
|
|
|
$(window).trigger($.Event('hashchange', {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
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();
|
2019-07-09 04:30:51 +02:00
|
|
|
$(window).trigger($.Event('hashchange', {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
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();
|
2019-07-09 04:30:51 +02:00
|
|
|
$(window).trigger($.Event('hashchange', {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
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();
|
2019-07-09 04:30:51 +02:00
|
|
|
$(window).trigger($.Event('hashchange', {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
helper.assert_events([
|
|
|
|
'overlays.close_for_hash_change',
|
|
|
|
'drafts.launch',
|
|
|
|
]);
|
|
|
|
|
|
|
|
window.location.hash = '#settings/alert-words';
|
|
|
|
|
|
|
|
helper.clear_events();
|
2019-07-09 04:30:51 +02:00
|
|
|
$(window).trigger($.Event('hashchange', {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
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();
|
2019-07-09 04:30:51 +02:00
|
|
|
$(window).trigger($.Event('hashchange', {}));
|
2018-04-11 13:14:39 +02:00
|
|
|
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
|
|
|
]);
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let called_back;
|
2018-04-11 13:14:39 +02:00
|
|
|
|
|
|
|
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', () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const helper = test_helper();
|
2018-04-11 13:14:39 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let operators = [
|
2018-04-11 13:14:39 +02:00
|
|
|
{operator: 'is', operand: 'private'},
|
|
|
|
];
|
|
|
|
|
2020-04-03 17:18:04 +02:00
|
|
|
blueslip.expect('warn', 'browser does not support pushState');
|
2018-04-11 13:14:39 +02:00
|
|
|
hashchange.save_narrow(operators);
|
2020-04-03 17:18:04 +02:00
|
|
|
blueslip.reset();
|
2018-12-06 22:59:05 +01:00
|
|
|
|
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');
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let url_pushed;
|
2018-04-11 13:14:39 +02:00
|
|
|
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
|
|
|
});
|