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");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const blueslip = require("./lib/zblueslip");
|
|
|
|
const $ = require("./lib/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const padded_widget = mock_esm("../src/padded_widget");
|
|
|
|
const message_viewport = mock_esm("../src/message_viewport");
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2021-03-18 15:12:47 +01:00
|
|
|
const {BuddyList} = 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,
|
|
|
|
};
|
|
|
|
|
2021-05-18 21:06:03 +02:00
|
|
|
$.create("#buddy_list_wrapper", {children: [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-03-18 15:12:47 +01:00
|
|
|
run_test("get_items", () => {
|
|
|
|
const buddy_list = new BuddyList();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
// We don't make $alice_li an actual jQuery stub,
|
2021-02-08 15:48:50 +01:00
|
|
|
// because our test only cares that it comes
|
|
|
|
// back from get_items.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $alice_li = "alice stub";
|
2020-07-15 01:29:15 +02:00
|
|
|
const sel = "li.user_sidebar_entry";
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = $.create("get_items container", {
|
|
|
|
children: [{to_$: () => $alice_li}],
|
2018-04-22 14:12:08 +02:00
|
|
|
});
|
2022-01-25 11:36:19 +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
|
|
|
const items = buddy_list.get_items();
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual(items, [$alice_li]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("basics", ({override}) => {
|
2021-03-18 15:12:47 +01:00
|
|
|
const buddy_list = new BuddyList();
|
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
|
|
|
|
2021-05-18 20:58:41 +02:00
|
|
|
override(message_viewport, "height", () => 550);
|
|
|
|
override(padded_widget, "update_padding", () => {});
|
|
|
|
|
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],
|
|
|
|
});
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(appended);
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2022-01-25 11:36:19 +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);
|
2022-01-25 11:36:19 +01:00
|
|
|
return $alice_li;
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $li = buddy_list.find_li({
|
2018-07-16 15:16:33 +02:00
|
|
|
key: alice.user_id,
|
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($li, $alice_li);
|
2018-07-16 15:16:33 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("big_list", ({override}) => {
|
2021-03-18 15:12:47 +01:00
|
|
|
const buddy_list = new BuddyList();
|
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
|
|
|
});
|
2021-05-18 20:58:41 +02:00
|
|
|
override(message_viewport, "height", () => 550);
|
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-06-16 14:38:37 +02:00
|
|
|
run_test("force_render", ({override}) => {
|
2021-03-18 15:12:47 +01:00
|
|
|
const buddy_list = new BuddyList();
|
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.
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.expect("error", "cannot show key at this position");
|
2018-07-16 15:16:33 +02:00
|
|
|
buddy_list.force_render({
|
|
|
|
pos: 10,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("find_li w/force_render", ({override}) => {
|
2021-03-18 15:12:47 +01:00
|
|
|
const buddy_list = new BuddyList();
|
|
|
|
|
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";
|
2022-01-25 11:36:19 +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);
|
2022-01-25 11:36:19 +01:00
|
|
|
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
|
|
|
|
2022-01-25 11:36:19 +01: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
|
|
|
});
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($empty_li, $stub_li);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!shown);
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($li, $stub_li);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(shown);
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("find_li w/bad key", ({override}) => {
|
2021-03-18 15:12:47 +01:00
|
|
|
const buddy_list = new BuddyList();
|
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
|
|
|
|
2022-01-25 11:36:19 +01: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,
|
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($undefined_li, []);
|
2018-07-16 15:16:33 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("scrolling", ({override}) => {
|
2021-03-18 15:12:47 +01:00
|
|
|
const buddy_list = new BuddyList();
|
2021-02-20 15:40:17 +01:00
|
|
|
init_simulated_scrolling();
|
|
|
|
|
2021-05-18 20:58:41 +02:00
|
|
|
override(message_viewport, "height", () => 550);
|
|
|
|
|
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
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!tried_to_fill);
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(tried_to_fill);
|
2018-07-16 15:16:33 +02:00
|
|
|
});
|