Files
zulip/frontend_tests/node_tests/typing_data.js
Steve Howell 9ab07d1038 util.js: Remove util from window.
We now treat util like a leaf module and
use "require" to import it everywhere it's used.

An earlier version of this commit moved
util into our "shared" library, but we
decided to wait on that.  Once we're ready
to do that, we should only need to do a
simple search/replace on various
require/zrequire statements plus a small
tweak to one of the custom linter checks.

It turns out we don't really need util.js
for our most immediate code-sharing goal,
which is to reuse our markdown code on
mobile.  There's a little bit of cleanup
still remaining to break the dependency,
but it's minor.

The util module still calls the global
blueslip module in one place, but that
code is about to be removed in the next
few commits.

I am pretty confident that once we start
sharing things like the typeahead code
more aggressively, we'll start having
dependencies on util.  The module is barely
more than 300 lines long, so we'll probably
just move the whole thing into shared
rather than break it apart.  Also, we
can continue to nibble away at the
cruftier parts of the module.
2020-02-15 12:20:20 -08:00

121 lines
3.3 KiB
JavaScript

zrequire("typing_data");
run_test('basics', () => {
// The typing_data needs to be robust with lists of
// user ids being in arbitrary sorting order and
// possibly in string form instead of integer. So all
// the apparent randomness in these tests has a purpose.
typing_data.add_typist([5, 10, 15], 15);
assert.deepEqual(typing_data.get_group_typists([15, 10, 5]), [15]);
// test that you can add twice
typing_data.add_typist([5, 10, 15], 15);
// add another id to our first group
typing_data.add_typist([5, 10, 15], "10");
assert.deepEqual(typing_data.get_group_typists([10, 15, 5]), [10, 15]);
// start adding to a new group
typing_data.add_typist([7, 15], 7);
typing_data.add_typist([7, "15"], 15);
// test get_all_typists
assert.deepEqual(typing_data.get_all_typists(), [7, 10, 15]);
// test basic removal
assert(typing_data.remove_typist([15, 7], "7"));
assert.deepEqual(typing_data.get_group_typists([7, 15]), [15]);
// test removing an id that is not there
assert(!typing_data.remove_typist([15, 7], 7));
assert.deepEqual(typing_data.get_group_typists([7, 15]), [15]);
assert.deepEqual(typing_data.get_all_typists(), [10, 15]);
// remove user from one group, but "15" will still be among
// "all typists"
assert(typing_data.remove_typist(["15", 7], "15"));
assert.deepEqual(typing_data.get_all_typists(), [10, 15]);
// now remove from the other group
assert(typing_data.remove_typist([5, 15, 10], 15));
assert.deepEqual(typing_data.get_all_typists(), [10]);
// test duplicate ids in a groups
typing_data.add_typist([20, 40, 20], 20);
assert.deepEqual(typing_data.get_group_typists([20, 40]), [20]);
});
run_test('timers', () => {
const events = {};
const stub_timer_id = 'timer_id_stub';
const stub_group = [5, 10, 15];
const stub_delay = 99;
const stub_f = 'function';
function set_timeout(f, delay) {
assert.equal(delay, stub_delay);
events.f = f;
events.timer_set = true;
return stub_timer_id;
}
function clear_timeout(timer) {
assert.equal(timer, stub_timer_id);
events.timer_cleared = true;
}
function reset_events() {
events.f = undefined;
events.timer_cleared = false;
events.timer_set = false;
}
function kickstart() {
reset_events();
typing_data.kickstart_inbound_timer(stub_group, stub_delay, stub_f);
}
function clear() {
reset_events();
typing_data.clear_inbound_timer(stub_group);
}
global.patch_builtin('setTimeout', set_timeout);
global.patch_builtin('clearTimeout', clear_timeout);
// first time, we set
kickstart();
assert.deepEqual(events, {
f: stub_f,
timer_cleared: false,
timer_set: true,
});
// second time we clear and set
kickstart();
assert.deepEqual(events, {
f: stub_f,
timer_cleared: true,
timer_set: true,
});
// first time clearing, we clear
clear();
assert.deepEqual(events, {
f: undefined,
timer_cleared: true,
timer_set: false,
});
// second time clearing, we noop
clear();
assert.deepEqual(events, {
f: undefined,
timer_cleared: false,
timer_set: false,
});
});