Files
zulip/frontend_tests/node_tests/recent_senders.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

117 lines
3.1 KiB
JavaScript

const rs = zrequire('recent_senders');
let next_id = 0;
run_test('process_message_for_senders', () => {
const stream1 = 1;
const stream2 = 2;
const stream3 = 3;
const topic1 = "topic-1";
const topic2 = "topic-2";
const topic3 = "topic-3";
const sender1 = 1;
const sender2 = 2;
const sender3 = 3;
// New stream
const message1 = {
stream_id: stream1,
id: next_id += 1,
topic: topic1,
sender_id: sender1,
};
const message2 = {
stream_id: stream2,
id: next_id += 1,
topic: topic1,
sender_id: sender2,
};
rs.process_message_for_senders(message1);
rs.process_message_for_senders(message2);
// Users have posted in only one of the streams
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) < 0,
true);
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream2, topic1) > 0,
true);
// Users haven't posted in this stream, return zero
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream3, undefined) === 0,
true);
// New topic
const message3 = {
stream_id: stream1,
id: next_id += 1,
topic: topic2,
sender_id: sender3,
};
rs.process_message_for_senders(message3);
assert.equal(
rs.compare_by_recency({user_id: sender3}, {user_id: sender2}, stream1, topic2) < 0,
true);
// New sender
const message4 = {
stream_id: stream1,
id: next_id += 1,
topic: topic1,
sender_id: sender2,
};
rs.process_message_for_senders(message4);
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) > 0,
true);
// More recent message
const message5 = {
stream_id: stream1,
id: next_id += 1,
topic: topic1,
sender_id: sender1,
};
rs.process_message_for_senders(message5);
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream1, topic1) < 0,
true);
// Same stream, but different topics
const message6 = {
stream_id: stream3,
id: next_id += 1,
topic: topic1,
sender_id: sender1,
};
const message7 = {
stream_id: stream3,
id: next_id += 1,
topic: topic2,
sender_id: sender2,
};
const message8 = {
stream_id: stream3,
id: next_id += 1,
topic: topic3,
sender_id: sender3,
};
rs.process_message_for_senders(message6);
rs.process_message_for_senders(message7);
rs.process_message_for_senders(message8);
// topic3 has a message in it, but sender1 nor sender2 have participated, so sort by stream
assert.equal(
rs.compare_by_recency({user_id: sender1}, {user_id: sender2}, stream3, topic3) > 0,
true);
assert.equal(
rs.compare_by_recency({user_id: sender2}, {user_id: sender1}, stream3, topic3) < 0,
true);
assert.equal(rs.compare_by_recency({}, {}, next_id += 1, ''), 0);
});