2021-05-18 14:51:01 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2024-04-02 05:27:33 +02:00
|
|
|
const {set_global, 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 $ = require("./lib/zjquery");
|
2021-05-18 14:51:01 +02:00
|
|
|
|
|
|
|
const fake_buddy_list = {
|
2023-11-16 01:07:23 +01:00
|
|
|
scroll_container_selector: "#whatever",
|
2024-09-18 08:28:12 +02:00
|
|
|
$users_matching_view_list: {
|
2024-05-03 02:29:50 +02:00
|
|
|
attr() {},
|
2023-08-23 02:18:53 +02:00
|
|
|
},
|
2024-09-18 08:28:12 +02:00
|
|
|
$other_users_list: {
|
2024-05-03 02:29:50 +02:00
|
|
|
attr() {},
|
2023-08-16 15:58:46 +02:00
|
|
|
},
|
2022-11-17 23:33:43 +01:00
|
|
|
find_li() {},
|
|
|
|
first_key() {},
|
|
|
|
prev_key() {},
|
|
|
|
next_key() {},
|
2021-05-18 14:51:01 +02:00
|
|
|
};
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
mock_esm("../src/buddy_list", {
|
2021-05-18 14:51:01 +02:00
|
|
|
buddy_list: fake_buddy_list,
|
|
|
|
});
|
|
|
|
|
2024-04-02 05:27:33 +02:00
|
|
|
function mock_setTimeout() {
|
|
|
|
let set_timeout_function_called = false;
|
|
|
|
set_global("setTimeout", (func) => {
|
|
|
|
if (set_timeout_function_called) {
|
|
|
|
// This conditional is needed to avoid indefinite calls.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
set_timeout_function_called = true;
|
|
|
|
func();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const popovers = mock_esm("../src/popovers");
|
|
|
|
const presence = mock_esm("../src/presence");
|
|
|
|
const resize = mock_esm("../src/resize");
|
2023-09-28 11:08:09 +02:00
|
|
|
const sidebar_ui = mock_esm("../src/sidebar_ui");
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2023-10-05 10:20:16 +02:00
|
|
|
const activity_ui = zrequire("activity_ui");
|
2021-05-18 14:51:01 +02:00
|
|
|
const buddy_data = zrequire("buddy_data");
|
2021-06-27 21:38:26 +02:00
|
|
|
const muted_users = zrequire("muted_users");
|
2021-05-18 14:51:01 +02:00
|
|
|
const people = zrequire("people");
|
2024-10-09 22:44:13 +02:00
|
|
|
const {set_realm} = zrequire("state_data");
|
|
|
|
|
|
|
|
const realm = {};
|
|
|
|
set_realm(realm);
|
2021-05-18 14:51:01 +02:00
|
|
|
|
|
|
|
const me = {
|
|
|
|
email: "me@zulip.com",
|
|
|
|
user_id: 999,
|
|
|
|
full_name: "Me Myself",
|
|
|
|
};
|
|
|
|
|
|
|
|
const alice = {
|
|
|
|
email: "alice@zulip.com",
|
|
|
|
user_id: 1,
|
|
|
|
full_name: "Alice Smith",
|
|
|
|
};
|
|
|
|
const fred = {
|
|
|
|
email: "fred@zulip.com",
|
|
|
|
user_id: 2,
|
|
|
|
full_name: "Fred Flintstone",
|
|
|
|
};
|
|
|
|
const jill = {
|
|
|
|
email: "jill@zulip.com",
|
|
|
|
user_id: 3,
|
|
|
|
full_name: "Jill Hill",
|
|
|
|
};
|
|
|
|
|
|
|
|
const all_user_ids = [alice.user_id, fred.user_id, jill.user_id, me.user_id];
|
|
|
|
const ordered_user_ids = [me.user_id, alice.user_id, fred.user_id, jill.user_id];
|
|
|
|
|
|
|
|
function test(label, f) {
|
2023-08-23 02:18:53 +02:00
|
|
|
run_test(label, (opts) => {
|
2021-05-18 14:51:01 +02:00
|
|
|
people.init();
|
|
|
|
people.add_active_user(alice);
|
|
|
|
people.add_active_user(fred);
|
|
|
|
people.add_active_user(jill);
|
|
|
|
people.add_active_user(me);
|
|
|
|
people.initialize_current_user(me.user_id);
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.set_muted_users([]);
|
2023-10-05 10:20:16 +02:00
|
|
|
activity_ui.set_cursor_and_filter();
|
2023-08-23 02:18:53 +02:00
|
|
|
f(opts);
|
2021-05-18 14:51:01 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-24 01:33:39 +02:00
|
|
|
function set_input_val(val) {
|
2024-03-19 19:45:03 +01:00
|
|
|
$("input.user-list-filter").val(val);
|
|
|
|
$("input.user-list-filter").trigger("input");
|
2023-08-24 01:33:39 +02:00
|
|
|
}
|
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
function stub_buddy_list_empty_list_message_lengths() {
|
|
|
|
$("#buddy-list-users-matching-view .empty-list-message").length = 0;
|
|
|
|
$("#buddy-list-other-users .empty-list-message").length = 0;
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("clear_search", ({override}) => {
|
2021-05-18 14:51:01 +02:00
|
|
|
override(presence, "get_status", () => "active");
|
|
|
|
override(presence, "get_user_ids", () => all_user_ids);
|
2023-12-14 23:51:33 +01:00
|
|
|
override(popovers, "hide_all", noop);
|
|
|
|
override(resize, "resize_sidebars", noop);
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2023-08-23 02:18:53 +02:00
|
|
|
stub_buddy_list_empty_list_message_lengths();
|
|
|
|
|
2023-08-24 01:33:39 +02:00
|
|
|
// Empty because no users match this search string.
|
|
|
|
override(fake_buddy_list, "populate", (user_ids) => {
|
2023-11-21 06:08:32 +01:00
|
|
|
assert.deepEqual(user_ids, {all_user_ids: []});
|
2023-08-24 01:33:39 +02:00
|
|
|
});
|
|
|
|
set_input_val("somevalue");
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!$("#user_search_section").hasClass("notdisplayed"));
|
2023-08-24 01:33:39 +02:00
|
|
|
|
|
|
|
// Now we're clearing the search string and everyone shows up again.
|
|
|
|
override(fake_buddy_list, "populate", (user_ids) => {
|
2023-11-21 06:08:32 +01:00
|
|
|
assert.deepEqual(user_ids, {all_user_ids: ordered_user_ids});
|
2023-08-24 01:33:39 +02:00
|
|
|
});
|
2021-05-18 14:51:01 +02:00
|
|
|
$("#clear_search_people_button").trigger("click");
|
2024-03-19 19:45:03 +01:00
|
|
|
assert.equal($("input.user-list-filter").val(), "");
|
2021-05-18 14:51:01 +02:00
|
|
|
$("#clear_search_people_button").trigger("click");
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok($("#user_search_section").hasClass("notdisplayed"));
|
2021-05-18 14:51:01 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("escape_search", ({override}) => {
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_presence_disabled", true);
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2023-12-14 23:51:33 +01:00
|
|
|
override(resize, "resize_sidebars", noop);
|
|
|
|
override(popovers, "hide_all", noop);
|
2023-08-23 02:18:53 +02:00
|
|
|
stub_buddy_list_empty_list_message_lengths();
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2023-08-24 01:33:39 +02:00
|
|
|
set_input_val("somevalue");
|
2023-10-05 10:20:16 +02:00
|
|
|
activity_ui.escape_search();
|
2024-03-19 19:45:03 +01:00
|
|
|
assert.equal($("input.user-list-filter").val(), "");
|
2023-10-05 10:20:16 +02:00
|
|
|
activity_ui.escape_search();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok($("#user_search_section").hasClass("notdisplayed"));
|
2023-08-24 01:33:39 +02:00
|
|
|
|
|
|
|
// We need to reset this because the unit tests aren't isolated from each other.
|
|
|
|
set_input_val("");
|
2021-05-18 14:51:01 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("blur search right", ({override}) => {
|
2023-12-14 23:51:33 +01:00
|
|
|
override(sidebar_ui, "show_userlist_sidebar", noop);
|
|
|
|
override(popovers, "hide_all", noop);
|
|
|
|
override(resize, "resize_sidebars", noop);
|
2024-04-02 05:27:33 +02:00
|
|
|
mock_setTimeout();
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2024-03-19 19:45:03 +01:00
|
|
|
$("input.user-list-filter").closest = (selector) => {
|
2021-05-18 14:51:01 +02:00
|
|
|
assert.equal(selector, ".app-main [class^='column-']");
|
|
|
|
return $.create("right-sidebar").addClass("column-right");
|
|
|
|
};
|
|
|
|
|
2024-03-19 19:45:03 +01:00
|
|
|
$("input.user-list-filter").trigger("blur");
|
|
|
|
assert.equal($("input.user-list-filter").is_focused(), false);
|
2023-10-05 10:20:16 +02:00
|
|
|
activity_ui.initiate_search();
|
2024-03-19 19:45:03 +01:00
|
|
|
assert.equal($("input.user-list-filter").is_focused(), true);
|
2021-05-18 14:51:01 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("blur search left", ({override}) => {
|
2023-12-14 23:51:33 +01:00
|
|
|
override(sidebar_ui, "show_streamlist_sidebar", noop);
|
|
|
|
override(popovers, "hide_all", noop);
|
|
|
|
override(resize, "resize_sidebars", noop);
|
2024-04-02 05:27:33 +02:00
|
|
|
mock_setTimeout();
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2024-03-19 19:45:03 +01:00
|
|
|
$("input.user-list-filter").closest = (selector) => {
|
2021-05-18 14:51:01 +02:00
|
|
|
assert.equal(selector, ".app-main [class^='column-']");
|
|
|
|
return $.create("right-sidebar").addClass("column-left");
|
|
|
|
};
|
|
|
|
|
2024-03-19 19:45:03 +01:00
|
|
|
$("input.user-list-filter").trigger("blur");
|
|
|
|
assert.equal($("input.user-list-filter").is_focused(), false);
|
2023-10-05 10:20:16 +02:00
|
|
|
activity_ui.initiate_search();
|
2024-03-19 19:45:03 +01:00
|
|
|
assert.equal($("input.user-list-filter").is_focused(), true);
|
2021-05-18 14:51:01 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("filter_user_ids", ({override}) => {
|
2021-05-18 14:51:01 +02:00
|
|
|
const user_presence = {};
|
|
|
|
user_presence[alice.user_id] = "active";
|
|
|
|
user_presence[fred.user_id] = "active";
|
|
|
|
user_presence[jill.user_id] = "active";
|
|
|
|
user_presence[me.user_id] = "active";
|
|
|
|
|
|
|
|
override(presence, "get_status", (user_id) => user_presence[user_id]);
|
|
|
|
override(presence, "get_user_ids", () => all_user_ids);
|
|
|
|
|
2021-05-18 20:36:39 +02:00
|
|
|
function test_filter(search_text, expected_users) {
|
|
|
|
const expected_user_ids = expected_users.map((user) => user.user_id);
|
2024-03-19 19:45:03 +01:00
|
|
|
$("input.user-list-filter").val(search_text);
|
2023-10-05 10:20:16 +02:00
|
|
|
const filter_text = activity_ui.get_filter_text();
|
2021-05-18 20:36:39 +02:00
|
|
|
assert.deepEqual(
|
|
|
|
buddy_data.get_filtered_and_sorted_user_ids(filter_text),
|
|
|
|
expected_user_ids,
|
|
|
|
);
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2023-11-21 06:08:32 +01:00
|
|
|
override(fake_buddy_list, "populate", ({all_user_ids: user_ids}) => {
|
2021-05-18 20:36:39 +02:00
|
|
|
assert.deepEqual(user_ids, expected_user_ids);
|
|
|
|
});
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2023-10-05 10:20:16 +02:00
|
|
|
activity_ui.build_user_sidebar();
|
2021-05-18 20:36:39 +02:00
|
|
|
}
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2021-05-18 20:36:39 +02:00
|
|
|
// Sanity check data setup.
|
|
|
|
assert.deepEqual(buddy_data.get_filtered_and_sorted_user_ids(), [
|
|
|
|
me.user_id,
|
|
|
|
alice.user_id,
|
|
|
|
fred.user_id,
|
|
|
|
jill.user_id,
|
|
|
|
]);
|
2021-05-18 14:51:01 +02:00
|
|
|
|
|
|
|
// Test no match for muted users even with filter text.
|
2021-05-18 20:36:39 +02:00
|
|
|
test_filter("ji", [jill]);
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.add_muted_user(jill.user_id);
|
2021-05-18 20:36:39 +02:00
|
|
|
test_filter("ji", []);
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2021-06-27 21:38:26 +02:00
|
|
|
muted_users.remove_muted_user(jill.user_id);
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2021-05-18 20:36:39 +02:00
|
|
|
test_filter("abc", []); // no match
|
|
|
|
test_filter("fred", [fred]);
|
|
|
|
test_filter("fred,alice", [alice, fred]);
|
|
|
|
test_filter("fr,al", [alice, fred]); // partials
|
|
|
|
test_filter("fr|al", [alice, fred]); // | as OR-operator
|
2021-05-18 14:51:01 +02:00
|
|
|
|
|
|
|
user_presence[alice.user_id] = "idle";
|
2021-05-18 20:36:39 +02:00
|
|
|
test_filter("fr,al", [fred, alice]);
|
2021-05-18 14:51:01 +02:00
|
|
|
|
|
|
|
user_presence[alice.user_id] = "active";
|
2021-05-18 20:36:39 +02:00
|
|
|
test_filter("fr,al", [alice, fred]);
|
2021-05-18 14:51:01 +02:00
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
test("click on user header to toggle display", ({override}) => {
|
2024-03-19 19:45:03 +01:00
|
|
|
const $user_filter = $("input.user-list-filter");
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2023-12-14 23:51:33 +01:00
|
|
|
override(popovers, "hide_all", noop);
|
|
|
|
override(sidebar_ui, "show_userlist_sidebar", noop);
|
|
|
|
override(resize, "resize_sidebars", noop);
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2024-10-09 22:20:06 +02:00
|
|
|
override(realm, "realm_presence_disabled", true);
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!$("#user_search_section").hasClass("notdisplayed"));
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$user_filter.val("bla");
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2024-10-01 01:07:24 +02:00
|
|
|
$("#userlist-header-search").trigger("click");
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok($("#user_search_section").hasClass("notdisplayed"));
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.equal($user_filter.val(), "");
|
2021-05-18 14:51:01 +02:00
|
|
|
|
2024-03-19 19:45:03 +01:00
|
|
|
$("input.user-list-filter").closest = (selector) => {
|
2021-05-18 14:51:01 +02:00
|
|
|
assert.equal(selector, ".app-main [class^='column-']");
|
|
|
|
return $.create("sidebar").addClass("column-right");
|
|
|
|
};
|
|
|
|
|
2024-10-01 01:07:24 +02:00
|
|
|
$("#userlist-header-search").trigger("click");
|
2021-05-18 14:51:01 +02:00
|
|
|
assert.equal($("#user_search_section").hasClass("notdisplayed"), false);
|
|
|
|
});
|
|
|
|
|
|
|
|
test("searching", () => {
|
2023-10-05 10:20:16 +02:00
|
|
|
assert.equal(activity_ui.searching(), false);
|
2024-03-19 19:45:03 +01:00
|
|
|
$("input.user-list-filter").trigger("focus");
|
2023-10-05 10:20:16 +02:00
|
|
|
assert.equal(activity_ui.searching(), true);
|
2024-03-19 19:45:03 +01:00
|
|
|
$("input.user-list-filter").trigger("blur");
|
2023-10-05 10:20:16 +02:00
|
|
|
assert.equal(activity_ui.searching(), false);
|
2021-05-18 14:51:01 +02:00
|
|
|
});
|