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");
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2020-12-01 00:12:33 +01:00
|
|
|
const {make_zjquery} = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-12-01 00:12:33 +01:00
|
|
|
set_global("$", make_zjquery());
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2020-07-15 01:29:15 +02:00
|
|
|
zrequire("buddy_data");
|
|
|
|
zrequire("buddy_list");
|
|
|
|
zrequire("ui");
|
2018-04-22 14:12:08 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("padded_widget", {
|
2018-07-26 19:50:15 +02:00
|
|
|
update_padding: () => {},
|
|
|
|
});
|
|
|
|
|
2018-07-16 15:16:33 +02:00
|
|
|
function init_simulated_scrolling() {
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("message_viewport", {
|
2018-07-16 15:16:33 +02:00
|
|
|
height: () => 550,
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("get_items", () => {
|
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
|
|
|
const items = buddy_list.get_items();
|
|
|
|
|
|
|
|
assert.deepEqual(items, [alice_li]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("basics", () => {
|
2018-07-16 15:16:33 +02:00
|
|
|
init_simulated_scrolling();
|
|
|
|
|
|
|
|
buddy_list.get_data_from_keys = (opts) => {
|
|
|
|
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";
|
2018-07-16 15:16:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
buddy_list.items_to_html = (opts) => {
|
|
|
|
const items = opts.items;
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(items, "data-stub");
|
2018-07-16 15:16:33 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
return "html-stub";
|
2018-07-16 15:16:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let appended;
|
|
|
|
buddy_list.container.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);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const alice_li = $.create("alice-li-stub");
|
2018-07-16 15:16:33 +02:00
|
|
|
alice_li.length = 1;
|
|
|
|
|
|
|
|
buddy_list.get_li_from_key = (opts) => {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
const li = buddy_list.find_li({
|
|
|
|
key: alice.user_id,
|
|
|
|
});
|
|
|
|
assert.equal(li, alice_li);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("big_list", () => {
|
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
|
|
|
|
|
|
|
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 = {
|
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);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("force_render", () => {
|
2018-07-16 15:16:33 +02:00
|
|
|
buddy_list.render_count = 50;
|
|
|
|
|
|
|
|
let num_rendered = 0;
|
|
|
|
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.
|
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,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("find_li w/force_render", () => {
|
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";
|
|
|
|
const stub_li = $.create("nada");
|
2018-07-16 15:16:33 +02:00
|
|
|
|
|
|
|
stub_li.length = 0;
|
|
|
|
|
|
|
|
buddy_list.get_li_from_key = (opts) => {
|
|
|
|
assert.equal(opts.key, key);
|
|
|
|
return stub_li;
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
buddy_list.force_render = (opts) => {
|
|
|
|
assert.equal(opts.pos, 2);
|
|
|
|
shown = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2020-07-02 01:41:40 +02:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
|
|
|
|
// very hacky:
|
|
|
|
assert.equal(undefined_li.length, 0);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("scrolling", () => {
|
2018-07-16 15:16:33 +02:00
|
|
|
buddy_list.populate({
|
|
|
|
keys: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
let tried_to_fill;
|
|
|
|
|
|
|
|
buddy_list.fill_screen_with_content = () => {
|
|
|
|
tried_to_fill = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
// You have to be careful here about where you place tests,
|
|
|
|
// since there is lots of stubbing in this module.
|