zulip/frontend_tests/node_tests/buddy_list.js

231 lines
5.2 KiB
JavaScript
Raw Normal View History

"use strict";
const {strict: assert} = require("assert");
const _ = require("lodash");
const rewiremock = require("rewiremock/node");
const {zrequire} = require("../zjsunit/namespace");
const {run_test} = require("../zjsunit/test");
const $ = require("../zjsunit/zjquery");
rewiremock("../../static/js/padded_widget").with({
update_padding: () => {},
});
rewiremock("../../static/js/message_viewport").with({
height: () => 550,
});
rewiremock.enable();
2020-08-20 21:24:06 +02:00
const people = zrequire("people");
const {buddy_list} = zrequire("buddy_list");
zrequire("ui");
function init_simulated_scrolling() {
const elem = {
dataset: {},
scrollTop: 0,
scrollHeight: 0,
};
$("#buddy_list_wrapper")[0] = elem;
2021-02-08 16:27:29 +01:00
$("#buddy_list_wrapper_padding").set_height(0);
return elem;
}
const alice = {
email: "alice@zulip.com",
user_id: 10,
full_name: "Alice Smith",
};
people.add_active_user(alice);
function populate_list_with_just_alice() {
// We don't make alice_li an actual jQuery stub,
// because our test only cares that it comes
// back from get_items.
const alice_li = "alice stub";
const sel = "li.user_sidebar_entry";
const container = $.create("get_items container", {
children: [{to_$: () => alice_li}],
});
buddy_list.container.set_find_results(sel, container);
return alice_li;
}
run_test("get_items", () => {
const alice_li = populate_list_with_just_alice();
const items = buddy_list.get_items();
assert.deepEqual(items, [alice_li]);
});
run_test("basics", (override) => {
init_simulated_scrolling();
override(buddy_list, "get_data_from_keys", (opts) => {
const keys = opts.keys;
assert.deepEqual(keys, [alice.user_id]);
return "data-stub";
});
override(buddy_list, "items_to_html", (opts) => {
const items = opts.items;
assert.equal(items, "data-stub");
return "html-stub";
});
let appended;
$("#user_presences").append = (html) => {
assert.equal(html, "html-stub");
appended = true;
};
buddy_list.populate({
keys: [alice.user_id],
});
assert(appended);
const alice_li = {length: 1};
override(buddy_list, "get_li_from_key", (opts) => {
const key = opts.key;
assert.equal(key, alice.user_id);
return alice_li;
});
const li = buddy_list.find_li({
key: alice.user_id,
});
assert.equal(li, alice_li);
});
run_test("big_list", (override) => {
const elem = init_simulated_scrolling();
// Don't actually render, but do simulate filling up
// the screen.
let chunks_inserted = 0;
override(buddy_list, "render_more", () => {
elem.scrollHeight += 100;
chunks_inserted += 1;
});
// We will have more than enough users, but still
// only do 6 chunks of data.
const num_users = 300;
const user_ids = [];
_.times(num_users, (i) => {
const person = {
email: "foo" + i + "@zulip.com",
user_id: 100 + i,
full_name: "Somebody " + i,
};
people.add_active_user(person);
user_ids.push(person.user_id);
});
buddy_list.populate({
keys: user_ids,
});
assert.equal(chunks_inserted, 6);
});
run_test("force_render", (override) => {
buddy_list.render_count = 50;
let num_rendered = 0;
override(buddy_list, "render_more", (opts) => {
num_rendered += opts.chunk_size;
});
buddy_list.force_render({
pos: 60,
});
assert.equal(num_rendered, 60 - 50 + 3);
// Force a contrived error case for line coverage.
blueslip.expect("error", "cannot show key at this position: 10");
buddy_list.force_render({
pos: 10,
});
});
run_test("find_li w/force_render", (override) => {
// If we call find_li w/force_render set, and the
// key is not already rendered in DOM, then the
// widget will call show_key to force-render it.
const key = "999";
const stub_li = {length: 0};
override(buddy_list, "get_li_from_key", (opts) => {
assert.equal(opts.key, key);
return stub_li;
});
buddy_list.keys = ["foo", "bar", key, "baz"];
let shown;
override(buddy_list, "force_render", (opts) => {
assert.equal(opts.pos, 2);
shown = true;
});
const empty_li = buddy_list.find_li({
key,
});
assert.equal(empty_li, stub_li);
assert(!shown);
const li = buddy_list.find_li({
key,
force_render: true,
});
assert.equal(li, stub_li);
assert(shown);
});
run_test("find_li w/bad key", (override) => {
override(buddy_list, "get_li_from_key", () => ({length: 0}));
const undefined_li = buddy_list.find_li({
key: "not-there",
force_render: true,
});
assert.deepEqual(undefined_li, []);
});
run_test("scrolling", (override) => {
init_simulated_scrolling();
buddy_list.populate({
keys: [],
});
let tried_to_fill;
override(buddy_list, "fill_screen_with_content", () => {
tried_to_fill = true;
});
assert(!tried_to_fill);
buddy_list.start_scroll_handler();
$(buddy_list.scroll_container_sel).trigger("scroll");
assert(tried_to_fill);
});
rewiremock.disable();