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-08-23 02:18:53 +02:00
|
|
|
const {
|
|
|
|
clear_buddy_list,
|
|
|
|
override_user_matches_narrow,
|
|
|
|
buddy_list_add_user_matching_view,
|
|
|
|
buddy_list_add_other_user,
|
|
|
|
stub_buddy_list_elements,
|
|
|
|
} = require("./lib/buddy_list");
|
2023-02-22 23:04:10 +01:00
|
|
|
const {mock_esm, zrequire} = require("./lib/namespace");
|
2023-12-14 23:51:33 +01:00
|
|
|
const {run_test, noop} = require("./lib/test");
|
2023-02-22 23:04:10 +01:00
|
|
|
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
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
const buddy_data = zrequire("buddy_data");
|
2021-03-18 15:12:47 +01:00
|
|
|
const {BuddyList} = zrequire("buddy_list");
|
2023-08-23 02:18:53 +02:00
|
|
|
const people = zrequire("people");
|
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);
|
2023-08-23 02:18:53 +02:00
|
|
|
const bob = {
|
|
|
|
email: "bob@zulip.com",
|
|
|
|
user_id: 15,
|
|
|
|
full_name: "Bob Smith",
|
|
|
|
};
|
|
|
|
people.add_active_user(bob);
|
|
|
|
const chris = {
|
|
|
|
email: "chris@zulip.com",
|
|
|
|
user_id: 20,
|
|
|
|
full_name: "Chris Smith",
|
|
|
|
};
|
|
|
|
people.add_active_user(chris);
|
|
|
|
const $alice_li = $.create("alice-stub");
|
|
|
|
const $bob_li = $.create("bob-stub");
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
run_test("basics", ({override, mock_template}) => {
|
2021-03-18 15:12:47 +01:00
|
|
|
const buddy_list = new BuddyList();
|
2023-08-23 02:18:53 +02:00
|
|
|
init_simulated_scrolling();
|
|
|
|
|
|
|
|
override(buddy_list, "items_to_html", () => "html-stub");
|
|
|
|
override(message_viewport, "height", () => 550);
|
|
|
|
override(padded_widget, "update_padding", noop);
|
|
|
|
stub_buddy_list_elements();
|
|
|
|
mock_template("buddy_list/view_all_users.hbs", false, noop);
|
|
|
|
|
|
|
|
let appended_to_users_matching_view;
|
|
|
|
$("#buddy-list-users-matching-view").append = (html) => {
|
|
|
|
assert.equal(html, "html-stub");
|
|
|
|
appended_to_users_matching_view = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
buddy_list.populate({
|
|
|
|
all_user_ids: [alice.user_id],
|
|
|
|
});
|
|
|
|
assert.ok(appended_to_users_matching_view);
|
|
|
|
|
|
|
|
const $alice_li = "alice-stub";
|
2021-03-18 15:12:47 +01:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
override(buddy_list, "get_li_from_user_id", (opts) => {
|
|
|
|
const user_id = opts.user_id;
|
|
|
|
|
|
|
|
assert.equal(user_id, alice.user_id);
|
|
|
|
return $alice_li;
|
2018-04-22 14:12:08 +02:00
|
|
|
});
|
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
const $li = buddy_list.find_li({
|
|
|
|
key: alice.user_id,
|
|
|
|
});
|
|
|
|
assert.equal($li, $alice_li);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
run_test("split list", ({override, override_rewire, mock_template}) => {
|
2021-03-18 15:12:47 +01:00
|
|
|
const buddy_list = new BuddyList();
|
2018-07-16 15:16:33 +02:00
|
|
|
init_simulated_scrolling();
|
2023-08-23 02:18:53 +02:00
|
|
|
stub_buddy_list_elements();
|
|
|
|
mock_template("buddy_list/view_all_users.hbs", false, noop);
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
override_rewire(buddy_data, "user_matches_narrow", override_user_matches_narrow);
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2021-02-19 14:52:41 +01:00
|
|
|
override(buddy_list, "items_to_html", (opts) => {
|
2023-08-23 02:18:53 +02:00
|
|
|
if (opts.items.length > 0) {
|
|
|
|
return "html-stub";
|
|
|
|
}
|
|
|
|
return "empty-list";
|
2021-02-19 14:52:41 +01:00
|
|
|
});
|
2021-05-18 20:58:41 +02:00
|
|
|
override(message_viewport, "height", () => 550);
|
2023-12-14 23:51:33 +01:00
|
|
|
override(padded_widget, "update_padding", noop);
|
2021-05-18 20:58:41 +02:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
let appended_to_users_matching_view = false;
|
2023-08-23 07:54:50 +02:00
|
|
|
$("#buddy-list-users-matching-view").append = (html) => {
|
2023-08-23 02:18:53 +02:00
|
|
|
if (html === "html-stub") {
|
|
|
|
appended_to_users_matching_view = true;
|
|
|
|
}
|
2018-07-16 15:16:33 +02:00
|
|
|
};
|
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
let appended_to_other_users = false;
|
|
|
|
$("#buddy-list-other-users").append = (html) => {
|
|
|
|
if (html === "html-stub") {
|
|
|
|
appended_to_other_users = true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// one user matching the view
|
|
|
|
buddy_list_add_user_matching_view(alice.user_id, $alice_li);
|
2018-07-16 15:16:33 +02:00
|
|
|
buddy_list.populate({
|
2023-11-21 06:08:32 +01:00
|
|
|
all_user_ids: [alice.user_id],
|
2018-07-16 15:16:33 +02:00
|
|
|
});
|
2023-08-23 02:18:53 +02:00
|
|
|
assert.ok(appended_to_users_matching_view);
|
|
|
|
assert.ok(!appended_to_other_users);
|
|
|
|
appended_to_users_matching_view = false;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
// one other user
|
|
|
|
clear_buddy_list(buddy_list);
|
|
|
|
buddy_list_add_other_user(alice.user_id, $alice_li);
|
|
|
|
buddy_list.populate({
|
|
|
|
all_user_ids: [alice.user_id],
|
|
|
|
});
|
|
|
|
assert.ok(!appended_to_users_matching_view);
|
|
|
|
assert.ok(appended_to_other_users);
|
|
|
|
appended_to_other_users = false;
|
|
|
|
|
|
|
|
// a user matching the view, and an other user
|
|
|
|
clear_buddy_list(buddy_list);
|
|
|
|
buddy_list_add_user_matching_view(alice.user_id, $alice_li);
|
|
|
|
buddy_list_add_other_user(bob.user_id, $bob_li);
|
|
|
|
buddy_list.populate({
|
|
|
|
all_user_ids: [alice.user_id, bob.user_id],
|
|
|
|
});
|
|
|
|
assert.ok(appended_to_users_matching_view);
|
|
|
|
assert.ok(appended_to_other_users);
|
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
run_test("find_li", ({override, mock_template}) => {
|
|
|
|
const buddy_list = new BuddyList();
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
override(buddy_list, "fill_screen_with_content", noop);
|
|
|
|
mock_template("buddy_list/view_all_users.hbs", false, noop);
|
|
|
|
stub_buddy_list_elements();
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
clear_buddy_list(buddy_list);
|
|
|
|
buddy_list_add_user_matching_view(alice.user_id, $alice_li);
|
|
|
|
buddy_list_add_other_user(bob.user_id, $bob_li);
|
|
|
|
|
|
|
|
let $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);
|
2023-08-23 02:18:53 +02:00
|
|
|
|
|
|
|
$li = buddy_list.find_li({
|
|
|
|
key: bob.user_id,
|
|
|
|
});
|
|
|
|
assert.equal($li, $bob_li);
|
2018-07-16 15:16:33 +02:00
|
|
|
});
|
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
run_test("fill_screen_with_content early break on big list", ({override, mock_template}) => {
|
|
|
|
stub_buddy_list_elements();
|
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();
|
2023-08-23 02:18:53 +02:00
|
|
|
stub_buddy_list_elements();
|
|
|
|
mock_template("buddy_list/view_all_users.hbs", false, noop);
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let chunks_inserted = 0;
|
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
|
2023-08-23 02:18:53 +02:00
|
|
|
// only do 6 chunks of data (20 users per chunk)
|
|
|
|
// because of exiting early from fill_screen_with_content
|
|
|
|
// because of not scrolling enough to fetch more users.
|
2018-07-16 15:16:33 +02:00
|
|
|
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({
|
2023-11-21 06:08:32 +01:00
|
|
|
all_user_ids: user_ids,
|
2018-07-16 15:16:33 +02:00
|
|
|
});
|
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
// Only 6 chunks, even though that's 120 users instead of the full 300.
|
2018-07-16 15:16:33 +02:00
|
|
|
assert.equal(chunks_inserted, 6);
|
|
|
|
});
|
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
run_test("big_list", ({override, override_rewire, mock_template}) => {
|
|
|
|
const buddy_list = new BuddyList();
|
|
|
|
init_simulated_scrolling();
|
|
|
|
|
|
|
|
stub_buddy_list_elements();
|
|
|
|
override(padded_widget, "update_padding", noop);
|
|
|
|
override(message_viewport, "height", () => 550);
|
|
|
|
override_rewire(buddy_data, "user_matches_narrow", override_user_matches_narrow);
|
|
|
|
mock_template("buddy_list/view_all_users.hbs", false, noop);
|
|
|
|
|
|
|
|
let items_to_html_call_count = 0;
|
|
|
|
override(buddy_list, "items_to_html", () => {
|
|
|
|
items_to_html_call_count += 1;
|
|
|
|
return "html-stub";
|
|
|
|
});
|
|
|
|
|
|
|
|
const num_users = 300;
|
|
|
|
const user_ids = [];
|
|
|
|
|
|
|
|
// This isn't a great way of testing this, but this is here for
|
|
|
|
// the sake of code coverage. Essentially, for a very long list,
|
|
|
|
// these buddy list sections can collect empty messages in the middle
|
|
|
|
// of populating (i.e. once a chunk is rendered) which later might need
|
|
|
|
// to be removed to add users from future chunks.
|
|
|
|
//
|
|
|
|
// For example: chunk1 populates only users in the list of users matching,
|
|
|
|
// the view and the empty list says "None", but chunk2 adds users to the
|
|
|
|
// other list so the "None" message should be removed.
|
|
|
|
//
|
|
|
|
// Here we're just saying both lists are rendered as empty from start,
|
|
|
|
// which doesn't actually happen, since I don't know how to properly
|
|
|
|
// get it set in the middle of buddy_list.populate().
|
|
|
|
$("#buddy-list-users-matching-view .empty-list-message").length = 1;
|
|
|
|
$("#buddy-list-other-users .empty-list-message").length = 1;
|
|
|
|
|
|
|
|
_.times(num_users, (i) => {
|
|
|
|
const person = {
|
|
|
|
email: "foo" + i + "@zulip.com",
|
|
|
|
user_id: 100 + i,
|
|
|
|
full_name: "Somebody " + i,
|
|
|
|
};
|
|
|
|
people.add_active_user(person);
|
|
|
|
if (i < 100 || i % 2 === 0) {
|
|
|
|
buddy_list_add_user_matching_view(person.user_id, $.create("stub" + i));
|
|
|
|
} else {
|
|
|
|
buddy_list_add_other_user(person.user_id, $.create("stub" + i));
|
|
|
|
}
|
|
|
|
user_ids.push(person.user_id);
|
|
|
|
});
|
|
|
|
|
|
|
|
buddy_list.populate({
|
|
|
|
all_user_ids: user_ids,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Chunks are default size 20, so there should be 300/20 = 15 chunks
|
|
|
|
const expected_chunks_inserted = 15;
|
|
|
|
// Two calls per chunk: one for users_matching_view and one for other_users.
|
|
|
|
assert.equal(items_to_html_call_count, 2 * expected_chunks_inserted);
|
|
|
|
});
|
|
|
|
|
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-11-21 06:08:32 +01:00
|
|
|
blueslip.expect("error", "cannot show user id 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
|
2023-08-23 02:18:53 +02:00
|
|
|
// user_id is not already rendered in DOM, then the
|
|
|
|
// widget will force-render it.
|
2023-11-21 06:08:32 +01:00
|
|
|
const user_id = "999";
|
2023-08-23 02:18:53 +02:00
|
|
|
const $stub_li = "stub-li";
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2023-11-21 06:08:32 +01:00
|
|
|
override(buddy_list, "get_li_from_user_id", (opts) => {
|
|
|
|
assert.equal(opts.user_id, user_id);
|
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
|
|
|
|
2023-11-21 06:08:32 +01:00
|
|
|
buddy_list.all_user_ids = ["foo", "bar", user_id, "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
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
const $hidden_li = buddy_list.find_li({
|
2023-11-21 06:08:32 +01:00
|
|
|
key: user_id,
|
2018-07-16 15:16:33 +02:00
|
|
|
});
|
2023-08-23 02:18:53 +02:00
|
|
|
assert.equal($hidden_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({
|
2023-11-21 06:08:32 +01:00
|
|
|
key: user_id,
|
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();
|
2023-08-23 02:18:53 +02:00
|
|
|
override(buddy_list, "get_li_from_user_id", () => "stub-li");
|
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,
|
|
|
|
});
|
|
|
|
|
2024-02-21 22:36:33 +01:00
|
|
|
assert.deepEqual($undefined_li, undefined);
|
2018-07-16 15:16:33 +02:00
|
|
|
});
|
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
run_test("scrolling", ({override, mock_template}) => {
|
2021-03-18 15:12:47 +01:00
|
|
|
const buddy_list = new BuddyList();
|
2018-07-16 15:16:33 +02:00
|
|
|
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
|
|
|
});
|
2023-08-23 02:18:53 +02:00
|
|
|
mock_template("buddy_list/view_all_users.hbs", false, noop);
|
|
|
|
stub_buddy_list_elements();
|
|
|
|
init_simulated_scrolling();
|
|
|
|
stub_buddy_list_elements();
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
clear_buddy_list(buddy_list);
|
|
|
|
assert.ok(tried_to_fill);
|
|
|
|
tried_to_fill = false;
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
buddy_list.start_scroll_handler();
|
2023-11-16 01:07:23 +01:00
|
|
|
$(buddy_list.scroll_container_selector).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
|
|
|
});
|