2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2020-07-25 02:02:35 +02:00
|
|
|
const _ = require("lodash");
|
2021-02-28 00:42:00 +01:00
|
|
|
const rewiremock = require("rewiremock/node");
|
2020-07-25 02:02:35 +02:00
|
|
|
|
2021-02-28 01:12:54 +01:00
|
|
|
const {zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-28 01:12:54 +01:00
|
|
|
rewiremock("../../static/js/padded_widget").with({
|
2020-12-01 23:21:38 +01:00
|
|
|
update_padding: () => {},
|
|
|
|
});
|
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
rewiremock("../../static/js/message_viewport").with({
|
2020-12-01 23:21:38 +01:00
|
|
|
height: () => 550,
|
|
|
|
});
|
|
|
|
|
2021-02-28 00:42:00 +01:00
|
|
|
rewiremock.enable();
|
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2021-02-28 01:13:13 +01:00
|
|
|
const {buddy_list} = zrequire("buddy_list");
|
2018-04-22 14:12:08 +02:00
|
|
|
|
2018-07-16 15:16:33 +02:00
|
|
|
function init_simulated_scrolling() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const elem = {
|
2019-07-26 00:22:43 +02:00
|
|
|
dataset: {},
|
2018-07-16 15:16:33 +02:00
|
|
|
scrollTop: 0,
|
|
|
|
scrollHeight: 0,
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
$("#buddy_list_wrapper")[0] = elem;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2021-02-08 16:27:29 +01:00
|
|
|
$("#buddy_list_wrapper_padding").set_height(0);
|
2018-07-26 19:50:15 +02:00
|
|
|
|
2018-07-16 15:16:33 +02:00
|
|
|
return elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
const alice = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "alice@zulip.com",
|
2018-07-16 15:16:33 +02:00
|
|
|
user_id: 10,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Alice Smith",
|
2018-07-16 15:16:33 +02:00
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(alice);
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2021-02-20 15:40:17 +01:00
|
|
|
function populate_list_with_just_alice() {
|
2021-02-08 15:48:50 +01:00
|
|
|
// 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";
|
2020-07-15 01:29:15 +02:00
|
|
|
const sel = "li.user_sidebar_entry";
|
2021-02-08 15:48:50 +01:00
|
|
|
const container = $.create("get_items container", {
|
|
|
|
children: [{to_$: () => alice_li}],
|
2018-04-22 14:12:08 +02:00
|
|
|
});
|
2021-02-08 15:48:50 +01:00
|
|
|
buddy_list.container.set_find_results(sel, container);
|
2018-04-22 14:12:08 +02:00
|
|
|
|
2021-02-20 15:40:17 +01:00
|
|
|
return alice_li;
|
|
|
|
}
|
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("get_items", () => {
|
2021-02-20 15:40:17 +01:00
|
|
|
const alice_li = populate_list_with_just_alice();
|
|
|
|
const items = buddy_list.get_items();
|
2018-04-22 14:12:08 +02:00
|
|
|
assert.deepEqual(items, [alice_li]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("basics", (override) => {
|
2018-07-16 15:16:33 +02:00
|
|
|
init_simulated_scrolling();
|
|
|
|
|
2021-02-19 14:52:41 +01:00
|
|
|
override(buddy_list, "get_data_from_keys", (opts) => {
|
2018-07-16 15:16:33 +02:00
|
|
|
const keys = opts.keys;
|
2020-02-12 11:49:02 +01:00
|
|
|
assert.deepEqual(keys, [alice.user_id]);
|
2020-07-15 01:29:15 +02:00
|
|
|
return "data-stub";
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2021-02-19 14:52:41 +01:00
|
|
|
override(buddy_list, "items_to_html", (opts) => {
|
2018-07-16 15:16:33 +02:00
|
|
|
const items = opts.items;
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(items, "data-stub");
|
|
|
|
return "html-stub";
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
let appended;
|
2021-02-20 15:40:17 +01:00
|
|
|
$("#user_presences").append = (html) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(html, "html-stub");
|
2018-07-16 15:16:33 +02:00
|
|
|
appended = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
buddy_list.populate({
|
|
|
|
keys: [alice.user_id],
|
|
|
|
});
|
|
|
|
assert(appended);
|
|
|
|
|
2021-02-07 18:30:58 +01:00
|
|
|
const alice_li = {length: 1};
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2021-02-19 14:52:41 +01:00
|
|
|
override(buddy_list, "get_li_from_key", (opts) => {
|
2018-07-16 15:16:33 +02:00
|
|
|
const key = opts.key;
|
|
|
|
|
2020-02-12 11:49:02 +01:00
|
|
|
assert.equal(key, alice.user_id);
|
2018-07-16 15:16:33 +02:00
|
|
|
return alice_li;
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
const li = buddy_list.find_li({
|
|
|
|
key: alice.user_id,
|
|
|
|
});
|
|
|
|
assert.equal(li, alice_li);
|
|
|
|
});
|
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("big_list", (override) => {
|
2018-07-16 15:16:33 +02:00
|
|
|
const elem = init_simulated_scrolling();
|
|
|
|
|
|
|
|
// Don't actually render, but do simulate filling up
|
|
|
|
// the screen.
|
2019-11-02 00:06:25 +01:00
|
|
|
let chunks_inserted = 0;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2021-02-19 14:52:41 +01:00
|
|
|
override(buddy_list, "render_more", () => {
|
2018-07-16 15:16:33 +02:00
|
|
|
elem.scrollHeight += 100;
|
|
|
|
chunks_inserted += 1;
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
// 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 = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "foo" + i + "@zulip.com",
|
2018-07-16 15:16:33 +02:00
|
|
|
user_id: 100 + i,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Somebody " + i,
|
2018-07-16 15:16:33 +02:00
|
|
|
};
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(person);
|
2018-07-16 15:16:33 +02:00
|
|
|
user_ids.push(person.user_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
buddy_list.populate({
|
|
|
|
keys: user_ids,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(chunks_inserted, 6);
|
|
|
|
});
|
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("force_render", (override) => {
|
2018-07-16 15:16:33 +02:00
|
|
|
buddy_list.render_count = 50;
|
|
|
|
|
|
|
|
let num_rendered = 0;
|
2021-02-19 14:52:41 +01:00
|
|
|
override(buddy_list, "render_more", (opts) => {
|
2018-07-16 15:16:33 +02:00
|
|
|
num_rendered += opts.chunk_size;
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
buddy_list.force_render({
|
|
|
|
pos: 60,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(num_rendered, 60 - 50 + 3);
|
|
|
|
|
|
|
|
// Force a contrived error case for line coverage.
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "cannot show key at this position: 10");
|
2018-07-16 15:16:33 +02:00
|
|
|
buddy_list.force_render({
|
|
|
|
pos: 10,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("find_li w/force_render", (override) => {
|
2018-07-16 15:16:33 +02:00
|
|
|
// 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.
|
2020-07-15 01:29:15 +02:00
|
|
|
const key = "999";
|
2021-02-07 18:30:58 +01:00
|
|
|
const stub_li = {length: 0};
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2021-02-19 14:52:41 +01:00
|
|
|
override(buddy_list, "get_li_from_key", (opts) => {
|
2018-07-16 15:16:33 +02:00
|
|
|
assert.equal(opts.key, key);
|
|
|
|
return stub_li;
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
buddy_list.keys = ["foo", "bar", key, "baz"];
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
let shown;
|
|
|
|
|
2021-02-19 14:52:41 +01:00
|
|
|
override(buddy_list, "force_render", (opts) => {
|
2018-07-16 15:16:33 +02:00
|
|
|
assert.equal(opts.pos, 2);
|
|
|
|
shown = true;
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
const empty_li = buddy_list.find_li({
|
2020-07-20 22:18:43 +02:00
|
|
|
key,
|
2018-07-16 15:16:33 +02:00
|
|
|
});
|
|
|
|
assert.equal(empty_li, stub_li);
|
|
|
|
assert(!shown);
|
|
|
|
|
|
|
|
const li = buddy_list.find_li({
|
2020-07-20 22:18:43 +02:00
|
|
|
key,
|
2018-07-16 15:16:33 +02:00
|
|
|
force_render: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(li, stub_li);
|
|
|
|
assert(shown);
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("find_li w/bad key", (override) => {
|
2021-02-19 14:52:41 +01:00
|
|
|
override(buddy_list, "get_li_from_key", () => ({length: 0}));
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
const undefined_li = buddy_list.find_li({
|
2020-07-15 01:29:15 +02:00
|
|
|
key: "not-there",
|
2018-07-16 15:16:33 +02:00
|
|
|
force_render: true,
|
|
|
|
});
|
|
|
|
|
2021-02-19 14:52:41 +01:00
|
|
|
assert.deepEqual(undefined_li, []);
|
2018-07-16 15:16:33 +02:00
|
|
|
});
|
|
|
|
|
2021-02-23 13:14:54 +01:00
|
|
|
run_test("scrolling", (override) => {
|
2021-02-20 15:40:17 +01:00
|
|
|
init_simulated_scrolling();
|
|
|
|
|
2018-07-16 15:16:33 +02:00
|
|
|
buddy_list.populate({
|
|
|
|
keys: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
let tried_to_fill;
|
|
|
|
|
2021-02-19 14:52:41 +01:00
|
|
|
override(buddy_list, "fill_screen_with_content", () => {
|
2018-07-16 15:16:33 +02:00
|
|
|
tried_to_fill = true;
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
assert(!tried_to_fill);
|
|
|
|
|
|
|
|
buddy_list.start_scroll_handler();
|
2020-07-21 00:23:06 +02:00
|
|
|
$(buddy_list.scroll_container_sel).trigger("scroll");
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
assert(tried_to_fill);
|
|
|
|
});
|
2021-02-28 00:42:00 +01:00
|
|
|
rewiremock.disable();
|