Files
zulip/frontend_tests/node_tests/stream_sort.js
Anders Kaseorg c520890f54 node_tests: Remove low-hanging uses of __Rewire__.
When we were preparing the conversion to ES modules in 2019, the
primary obstacle was that the Node tests extensively relied on the
ability to reach into modules and mutate their CommonJS exports in
order to mock things.  ES module bindings are not mutable, so in
commit 173c9cee42 we added
babel-plugin-rewire-ts as a kludgy transpilation-based workaround for
this to unblock the conversion.

However, babel-plugin-rewire-ts is slow, buggy, nonstandard,
confusing, and unmaintained.  It’s incompatible with running our ES
modules as native ES modules, and prevents us from taking advantage of
modern tools for ES modules.  So we want to excise all use of
__Rewire__ (and the disallow_rewire, override_rewire helper functions
that rely on it) from the tests and remove babel-plugin-rewire-ts.

Commits 64abdc199e and
e17ba5260a (#20730) prepared for this by
letting us see where __Rewire__ is being used.  Now we go through and
remove most of the uses that are easy to remove without modifying the
production code at all.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-07-13 16:27:30 -07:00

117 lines
3.5 KiB
JavaScript

"use strict";
const {strict: assert} = require("assert");
const {zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const stream_data = zrequire("stream_data");
const stream_sort = zrequire("stream_sort");
const scalene = {
subscribed: true,
name: "scalene",
stream_id: 1,
pin_to_top: true,
};
const fast_tortoise = {
subscribed: true,
name: "fast tortoise",
stream_id: 2,
pin_to_top: false,
};
const pneumonia = {
subscribed: true,
name: "pneumonia",
stream_id: 3,
pin_to_top: false,
};
const clarinet = {
subscribed: true,
name: "clarinet",
stream_id: 4,
pin_to_top: false,
};
const weaving = {
subscribed: false,
name: "weaving",
stream_id: 5,
pin_to_top: false,
};
function sort_groups(query) {
const streams = stream_data.subscribed_stream_ids();
return stream_sort.sort_groups(streams, query);
}
function test(label, f) {
run_test(label, (helpers) => {
stream_data.clear_subscriptions();
f(helpers);
});
}
test("no_subscribed_streams", () => {
const sorted = sort_groups("");
assert.deepEqual(sorted, {
dormant_streams: [],
normal_streams: [],
pinned_streams: [],
same_as_before: sorted.same_as_before,
});
assert.equal(stream_sort.first_stream_id(), undefined);
});
test("basics", ({override_rewire}) => {
stream_data.add_sub(scalene);
stream_data.add_sub(fast_tortoise);
stream_data.add_sub(pneumonia);
stream_data.add_sub(clarinet);
stream_data.add_sub(weaving);
override_rewire(stream_data, "is_active", (sub) => sub.name !== "pneumonia");
// Test sorting into categories/alphabetized
let sorted = sort_groups("");
assert.deepEqual(sorted.pinned_streams, [scalene.stream_id]);
assert.deepEqual(sorted.normal_streams, [clarinet.stream_id, fast_tortoise.stream_id]);
assert.deepEqual(sorted.dormant_streams, [pneumonia.stream_id]);
// Test cursor helpers.
assert.equal(stream_sort.first_stream_id(), scalene.stream_id);
assert.equal(stream_sort.prev_stream_id(scalene.stream_id), undefined);
assert.equal(stream_sort.prev_stream_id(clarinet.stream_id), scalene.stream_id);
assert.equal(stream_sort.next_stream_id(fast_tortoise.stream_id), pneumonia.stream_id);
assert.equal(stream_sort.next_stream_id(pneumonia.stream_id), undefined);
// Test filtering
sorted = sort_groups("s");
assert.deepEqual(sorted.pinned_streams, [scalene.stream_id]);
assert.deepEqual(sorted.normal_streams, []);
assert.deepEqual(sorted.dormant_streams, []);
assert.equal(stream_sort.prev_stream_id(clarinet.stream_id), undefined);
assert.equal(stream_sort.next_stream_id(clarinet.stream_id), undefined);
// Test searching entire word, case-insensitive
sorted = sort_groups("PnEuMoNiA");
assert.deepEqual(sorted.pinned_streams, []);
assert.deepEqual(sorted.normal_streams, []);
assert.deepEqual(sorted.dormant_streams, [pneumonia.stream_id]);
// Test searching part of word
sorted = sort_groups("tortoise");
assert.deepEqual(sorted.pinned_streams, []);
assert.deepEqual(sorted.normal_streams, [fast_tortoise.stream_id]);
assert.deepEqual(sorted.dormant_streams, []);
// Test searching stream with spaces
sorted = sort_groups("fast t");
assert.deepEqual(sorted.pinned_streams, []);
assert.deepEqual(sorted.normal_streams, [fast_tortoise.stream_id]);
assert.deepEqual(sorted.dormant_streams, []);
});