2020-07-20 23:53:59 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
const rewiremock = require("rewiremock/node");
|
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
const {stub_templates} = require("../zjsunit/handlebars");
|
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");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-07-20 23:53:59 +02:00
|
|
|
const noop = () => {};
|
2020-12-01 00:19:42 +01:00
|
|
|
stub_templates(() => noop);
|
2020-07-20 23:53:59 +02:00
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
rewiremock("../../static/js/channel").with({});
|
2021-02-28 01:07:47 +01:00
|
|
|
rewiremock("../../static/js/hashchange").with({update_browser_history: noop});
|
2021-02-28 01:07:13 +01:00
|
|
|
rewiremock("../../static/js/hash_util").with({
|
2020-07-20 23:53:59 +02:00
|
|
|
stream_edit_uri: noop,
|
|
|
|
by_stream_uri: noop,
|
|
|
|
});
|
2021-02-28 00:57:45 +01:00
|
|
|
rewiremock("../../static/js/list_widget").with({
|
2020-07-20 23:53:59 +02:00
|
|
|
create: () => ({init: noop}),
|
|
|
|
});
|
2021-02-23 03:54:07 +01:00
|
|
|
const page_params = set_global("page_params", {});
|
2020-07-20 23:53:59 +02:00
|
|
|
set_global("settings_notifications", {
|
|
|
|
get_notifications_table_row_data: noop,
|
|
|
|
});
|
2021-02-28 00:53:05 +01:00
|
|
|
rewiremock("../../static/js/stream_color").with({
|
2020-07-20 23:53:59 +02:00
|
|
|
set_colorpicker_color: noop,
|
|
|
|
});
|
2021-02-28 01:04:15 +01:00
|
|
|
const typeahead_helper = {__esModule: true};
|
|
|
|
rewiremock("../../static/js/typeahead_helper").with(typeahead_helper);
|
2021-02-10 04:53:22 +01:00
|
|
|
const ui = set_global("ui", {
|
2020-07-20 23:53:59 +02:00
|
|
|
get_scroll_element: noop,
|
|
|
|
});
|
|
|
|
|
2021-02-28 00:39:51 +01:00
|
|
|
rewiremock.enable();
|
|
|
|
|
2020-07-20 23:53:59 +02:00
|
|
|
zrequire("input_pill");
|
2021-01-12 21:38:01 +01:00
|
|
|
const peer_data = zrequire("peer_data");
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2020-07-20 23:53:59 +02:00
|
|
|
zrequire("subs");
|
2021-02-10 04:53:22 +01:00
|
|
|
const stream_edit = zrequire("stream_edit");
|
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const stream_pill = zrequire("stream_pill");
|
|
|
|
const user_pill = zrequire("user_pill");
|
2020-07-20 23:53:59 +02:00
|
|
|
|
2021-02-28 00:55:38 +01:00
|
|
|
stream_edit.__Rewire__("sort_but_pin_current_user_on_top", noop);
|
2020-07-20 23:53:59 +02:00
|
|
|
|
|
|
|
const jill = {
|
|
|
|
email: "jill@zulip.com",
|
|
|
|
user_id: 10,
|
|
|
|
full_name: "Jill Hill",
|
|
|
|
};
|
|
|
|
const mark = {
|
|
|
|
email: "mark@zulip.com",
|
|
|
|
user_id: 20,
|
|
|
|
full_name: "Marky Mark",
|
|
|
|
};
|
|
|
|
const fred = {
|
|
|
|
email: "fred@zulip.com",
|
|
|
|
user_id: 30,
|
|
|
|
full_name: "Fred Flintstone",
|
|
|
|
};
|
|
|
|
const me = {
|
|
|
|
email: "me@example.com",
|
|
|
|
user_id: 40,
|
|
|
|
full_name: "me",
|
|
|
|
};
|
|
|
|
|
|
|
|
page_params.user_id = me.user_id;
|
|
|
|
const persons = [jill, mark, fred, me];
|
|
|
|
for (const person of persons) {
|
|
|
|
people.add_active_user(person);
|
|
|
|
}
|
|
|
|
|
|
|
|
const denmark = {
|
|
|
|
stream_id: 1,
|
|
|
|
name: "Denmark",
|
|
|
|
subscribed: true,
|
|
|
|
render_subscribers: true,
|
|
|
|
should_display_subscription_button: true,
|
|
|
|
};
|
2021-01-13 22:03:25 +01:00
|
|
|
peer_data.set_subscribers(denmark.stream_id, [me.user_id, mark.user_id]);
|
2021-01-12 16:05:24 +01:00
|
|
|
|
2020-07-20 23:53:59 +02:00
|
|
|
const sweden = {
|
|
|
|
stream_id: 2,
|
|
|
|
name: "Sweden",
|
|
|
|
subscribed: false,
|
|
|
|
};
|
2021-01-13 22:03:25 +01:00
|
|
|
peer_data.set_subscribers(sweden.stream_id, [mark.user_id, jill.user_id]);
|
2020-07-20 23:53:59 +02:00
|
|
|
|
|
|
|
const subs = [denmark, sweden];
|
|
|
|
for (const sub of subs) {
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
}
|
|
|
|
|
2021-02-21 13:30:55 +01:00
|
|
|
function test_ui(label, f) {
|
|
|
|
run_test(label, (override) => {
|
|
|
|
stream_edit.initialize();
|
|
|
|
f(override);
|
|
|
|
});
|
|
|
|
}
|
2020-07-20 23:53:59 +02:00
|
|
|
|
2021-02-21 13:30:55 +01:00
|
|
|
test_ui("subscriber_pills", () => {
|
|
|
|
const subscriptions_table_selector = "#subscriptions_table";
|
|
|
|
const input_field_stub = $.create(".input");
|
|
|
|
|
2021-02-22 14:41:35 +01:00
|
|
|
input_field_stub.before = () => {};
|
|
|
|
|
2021-02-21 13:30:55 +01:00
|
|
|
const sub_settings_selector = `#subscription_overlay .subscription_settings[data-stream-id='${CSS.escape(
|
|
|
|
denmark.stream_id,
|
|
|
|
)}']`;
|
|
|
|
const $sub_settings_container = $.create(sub_settings_selector);
|
|
|
|
$sub_settings_container.find = noop;
|
2021-02-23 14:37:26 +01:00
|
|
|
$sub_settings_container.find = () => input_field_stub;
|
2020-07-20 23:53:59 +02:00
|
|
|
|
2021-02-21 13:30:55 +01:00
|
|
|
const pill_container_stub = $.create(sub_settings_selector + " .pill-container");
|
2021-02-23 14:37:26 +01:00
|
|
|
pill_container_stub.find = () => input_field_stub;
|
2020-07-20 23:53:59 +02:00
|
|
|
|
2021-02-21 13:30:55 +01:00
|
|
|
const $subscription_settings = $.create(".subscription_settings");
|
|
|
|
$subscription_settings.addClass = noop;
|
|
|
|
$subscription_settings.closest = () => $subscription_settings;
|
|
|
|
$subscription_settings.attr("data-stream-id", denmark.stream_id);
|
|
|
|
$subscription_settings.length = 0;
|
2020-07-20 23:53:59 +02:00
|
|
|
|
2021-02-21 13:30:55 +01:00
|
|
|
const $add_subscribers_form = $.create(".subscriber_list_add form");
|
|
|
|
$add_subscribers_form.closest = () => $subscription_settings;
|
2020-07-20 23:53:59 +02:00
|
|
|
|
|
|
|
let template_rendered = false;
|
|
|
|
ui.get_content_element = () => {
|
|
|
|
template_rendered = true;
|
|
|
|
return {html: noop};
|
|
|
|
};
|
|
|
|
|
|
|
|
let expected_user_ids = [];
|
|
|
|
let input_typeahead_called = false;
|
|
|
|
let add_subscribers_request = false;
|
2021-02-28 00:55:38 +01:00
|
|
|
stream_edit.__Rewire__("invite_user_to_stream", (user_ids, sub) => {
|
2020-07-20 23:53:59 +02:00
|
|
|
assert.equal(sub.stream_id, denmark.stream_id);
|
2021-01-12 16:05:24 +01:00
|
|
|
assert.deepEqual(user_ids.sort(), expected_user_ids.sort());
|
2020-07-20 23:53:59 +02:00
|
|
|
add_subscribers_request = true;
|
2021-02-28 00:55:38 +01:00
|
|
|
});
|
2020-07-20 23:53:59 +02:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
input_field_stub.typeahead = (config) => {
|
2020-07-20 23:53:59 +02:00
|
|
|
assert.equal(config.items, 5);
|
|
|
|
assert(config.fixed);
|
|
|
|
assert(config.dropup);
|
|
|
|
assert(config.stopAdvance);
|
|
|
|
|
|
|
|
assert.equal(typeof config.source, "function");
|
|
|
|
assert.equal(typeof config.highlighter, "function");
|
|
|
|
assert.equal(typeof config.matcher, "function");
|
|
|
|
assert.equal(typeof config.sorter, "function");
|
|
|
|
assert.equal(typeof config.updater, "function");
|
|
|
|
|
|
|
|
const fake_this = {
|
|
|
|
query: "#Denmark",
|
|
|
|
};
|
|
|
|
|
|
|
|
(function test_highlighter() {
|
|
|
|
const fake_stream = $.create("fake-stream");
|
2021-02-23 14:37:26 +01:00
|
|
|
typeahead_helper.render_stream = () => fake_stream;
|
2020-07-20 23:53:59 +02:00
|
|
|
assert.equal(config.highlighter.call(fake_this, denmark), fake_stream);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function test_matcher() {
|
|
|
|
let result = config.matcher.call(fake_this, denmark);
|
|
|
|
assert(result);
|
|
|
|
|
|
|
|
result = config.matcher.call(fake_this, sweden);
|
|
|
|
assert(!result);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function test_sorter() {
|
|
|
|
let sort_streams_called = false;
|
2021-02-23 14:37:26 +01:00
|
|
|
typeahead_helper.sort_streams = () => {
|
2020-07-20 23:53:59 +02:00
|
|
|
sort_streams_called = true;
|
|
|
|
};
|
|
|
|
config.sorter.call(fake_this);
|
|
|
|
assert(sort_streams_called);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function test_updater() {
|
|
|
|
function number_of_pills() {
|
|
|
|
const pills = stream_edit.pill_widget.items();
|
|
|
|
return pills.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.equal(number_of_pills(), 0);
|
|
|
|
config.updater.call(fake_this, denmark);
|
|
|
|
assert.equal(number_of_pills(), 1);
|
|
|
|
fake_this.query = me.email;
|
|
|
|
config.updater.call(fake_this, me);
|
|
|
|
assert.equal(number_of_pills(), 2);
|
|
|
|
fake_this.query = "#Denmark";
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function test_source() {
|
|
|
|
const result = config.source.call(fake_this);
|
|
|
|
const taken_ids = stream_pill.get_stream_ids(stream_edit.pill_widget);
|
2021-01-12 16:05:24 +01:00
|
|
|
const stream_ids = Array.from(result, (stream) => stream.stream_id).sort();
|
|
|
|
let expected_ids = Array.from(subs, (stream) => stream.stream_id).sort();
|
2020-07-20 23:53:59 +02:00
|
|
|
expected_ids = expected_ids.filter((id) => !taken_ids.includes(id));
|
|
|
|
assert.deepEqual(stream_ids, expected_ids);
|
|
|
|
})();
|
|
|
|
|
|
|
|
input_typeahead_called = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize pill widget upon displaying subscription settings page.
|
|
|
|
const stream_row_handler = $(subscriptions_table_selector).get_on_handler(
|
|
|
|
"click",
|
|
|
|
".stream-row",
|
|
|
|
);
|
|
|
|
|
|
|
|
let fake_this = $subscription_settings;
|
|
|
|
let event = {target: fake_this};
|
|
|
|
stream_row_handler.call(fake_this, event);
|
|
|
|
assert(template_rendered);
|
|
|
|
assert(input_typeahead_called);
|
|
|
|
|
|
|
|
let add_subscribers_handler = $(subscriptions_table_selector).get_on_handler(
|
|
|
|
"submit",
|
|
|
|
".subscriber_list_add form",
|
|
|
|
);
|
|
|
|
|
|
|
|
fake_this = $add_subscribers_form;
|
|
|
|
fake_this.closest = () => $subscription_settings;
|
|
|
|
event = {
|
|
|
|
target: fake_this,
|
|
|
|
preventDefault: () => {},
|
|
|
|
};
|
|
|
|
|
2020-10-10 11:12:46 +02:00
|
|
|
// We cannot subscribe ourselves (`me`) as
|
|
|
|
// we are already subscribed to denmark stream.
|
2021-01-12 16:05:24 +01:00
|
|
|
const potential_denmark_stream_subscribers = Array.from(
|
2021-01-12 21:38:01 +01:00
|
|
|
peer_data.get_subscribers(denmark.stream_id),
|
2021-01-12 16:05:24 +01:00
|
|
|
).filter((id) => id !== me.user_id);
|
2020-10-10 11:12:46 +02:00
|
|
|
|
2020-07-20 23:53:59 +02:00
|
|
|
// denmark.stream_id is stubbed. Thus request is
|
|
|
|
// sent to add all subscribers of stream Denmark.
|
2020-10-10 11:12:46 +02:00
|
|
|
expected_user_ids = potential_denmark_stream_subscribers;
|
2020-07-20 23:53:59 +02:00
|
|
|
add_subscribers_handler(event);
|
|
|
|
|
|
|
|
add_subscribers_handler = $(subscriptions_table_selector).get_on_handler(
|
|
|
|
"keyup",
|
|
|
|
".subscriber_list_add form",
|
|
|
|
);
|
|
|
|
event.which = 13;
|
|
|
|
|
|
|
|
// Only Denmark stream pill is created and a
|
|
|
|
// request is sent to add all it's subscribers.
|
2021-02-28 00:38:33 +01:00
|
|
|
user_pill.__Rewire__("get_user_ids", () => []);
|
2020-10-10 11:12:46 +02:00
|
|
|
expected_user_ids = potential_denmark_stream_subscribers;
|
2020-07-20 23:53:59 +02:00
|
|
|
add_subscribers_handler(event);
|
|
|
|
|
|
|
|
// No request is sent when there are no users to subscribe.
|
|
|
|
stream_pill.get_user_ids = () => [];
|
|
|
|
add_subscribers_request = false;
|
|
|
|
add_subscribers_handler(event);
|
|
|
|
assert(!add_subscribers_request);
|
|
|
|
|
|
|
|
// No request is sent if we try to subscribe ourselves
|
|
|
|
// only and are already subscribed to the stream.
|
2021-02-28 00:38:33 +01:00
|
|
|
user_pill.__Rewire__("get_user_ids", () => [me.user_id]);
|
2020-07-20 23:53:59 +02:00
|
|
|
add_subscribers_handler(event);
|
|
|
|
assert(!add_subscribers_request);
|
|
|
|
|
|
|
|
// Denmark stream pill and fred and mark user pills are created.
|
|
|
|
// But only one request for mark is sent even though a mark user
|
|
|
|
// pill is created and mark is also a subscriber of Denmark stream.
|
2021-02-28 00:38:33 +01:00
|
|
|
user_pill.__Rewire__("get_user_ids", () => [mark.user_id, fred.user_id]);
|
2021-01-12 21:38:01 +01:00
|
|
|
stream_pill.get_user_ids = () => peer_data.get_subscribers(denmark.stream_id);
|
2020-10-10 11:12:46 +02:00
|
|
|
expected_user_ids = potential_denmark_stream_subscribers.concat(fred.user_id);
|
2020-07-20 23:53:59 +02:00
|
|
|
add_subscribers_handler(event);
|
|
|
|
});
|
2021-02-28 00:39:51 +01:00
|
|
|
rewiremock.disable();
|