2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
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 $ = require("./lib/zjquery");
|
2018-04-09 15:22:26 +02:00
|
|
|
|
2023-05-03 07:06:19 +02:00
|
|
|
const list_widget = mock_esm("../src/list_widget", {
|
|
|
|
generic_sort_functions: noop,
|
|
|
|
});
|
2021-02-28 00:41:32 +01:00
|
|
|
|
2023-04-16 06:33:06 +02:00
|
|
|
const settings_user_topics = zrequire("settings_user_topics");
|
2021-02-10 04:53:22 +01:00
|
|
|
const stream_data = zrequire("stream_data");
|
2022-08-14 15:33:13 +02:00
|
|
|
const user_topics = zrequire("user_topics");
|
2018-04-09 15:22:26 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const frontend = {
|
2018-12-13 22:26:10 +01:00
|
|
|
stream_id: 101,
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "frontend",
|
2018-12-13 22:26:10 +01:00
|
|
|
};
|
2020-02-09 22:02:55 +01:00
|
|
|
stream_data.add_sub(frontend);
|
2018-12-13 22:26:10 +01:00
|
|
|
|
2023-08-04 23:40:48 +02:00
|
|
|
run_test("settings", ({override, override_rewire}) => {
|
2023-04-11 07:48:13 +02:00
|
|
|
user_topics.update_user_topics(
|
|
|
|
frontend.stream_id,
|
2024-03-30 23:39:51 +01:00
|
|
|
frontend.name,
|
2023-04-11 07:48:13 +02:00
|
|
|
"js",
|
|
|
|
user_topics.all_visibility_policies.MUTED,
|
|
|
|
1577836800,
|
|
|
|
);
|
2021-04-27 12:09:08 +02:00
|
|
|
let populate_list_called = false;
|
2023-06-29 21:59:08 +02:00
|
|
|
override(list_widget, "create", (_$container, list) => {
|
2022-07-10 01:06:33 +02:00
|
|
|
assert.deepEqual(list, [
|
2020-02-05 13:14:24 +01:00
|
|
|
{
|
2023-04-12 11:01:25 +02:00
|
|
|
date_updated: 1577836800000,
|
|
|
|
date_updated_str: "Jan 1, 2020",
|
2020-02-05 13:14:24 +01:00
|
|
|
stream: frontend.name,
|
|
|
|
stream_id: frontend.stream_id,
|
2020-07-15 01:29:15 +02:00
|
|
|
topic: "js",
|
2023-04-24 15:40:26 +02:00
|
|
|
visibility_policy: user_topics.all_visibility_policies.MUTED,
|
2020-07-15 00:34:28 +02:00
|
|
|
},
|
|
|
|
]);
|
2021-04-27 12:09:08 +02:00
|
|
|
populate_list_called = true;
|
|
|
|
});
|
2018-04-09 15:22:26 +02:00
|
|
|
|
2023-04-16 06:33:06 +02:00
|
|
|
settings_user_topics.reset();
|
|
|
|
assert.equal(settings_user_topics.loaded, false);
|
2020-07-12 00:13:47 +02:00
|
|
|
|
2023-04-16 06:33:06 +02:00
|
|
|
settings_user_topics.set_up();
|
|
|
|
assert.equal(settings_user_topics.loaded, true);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(populate_list_called);
|
2018-04-09 15:22:26 +02:00
|
|
|
|
2023-04-14 18:13:13 +02:00
|
|
|
const topic_change_handler = $("body").get_on_handler(
|
|
|
|
"change",
|
2024-05-03 23:19:27 +02:00
|
|
|
"select.settings_user_topic_visibility_policy",
|
2023-04-14 18:13:13 +02:00
|
|
|
);
|
|
|
|
assert.equal(typeof topic_change_handler, "function");
|
2018-04-09 15:22:26 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const event = {
|
2021-04-17 13:59:43 +02:00
|
|
|
stopPropagation: noop,
|
2018-04-09 15:22:26 +02:00
|
|
|
};
|
|
|
|
|
2023-04-14 18:13:13 +02:00
|
|
|
const $topic_fake_this = $.create("fake.settings_user_topic_visibility_policy");
|
2022-01-25 11:36:19 +01:00
|
|
|
const $topic_tr_html = $('tr[data-topic="js"]');
|
|
|
|
$topic_fake_this.closest = (opts) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(opts, "tr");
|
2022-01-25 11:36:19 +01:00
|
|
|
return $topic_tr_html;
|
2018-04-09 15:22:26 +02:00
|
|
|
};
|
2023-07-24 09:20:52 +02:00
|
|
|
const $topics_panel_header = $.create("fake.topic_panel_header").attr(
|
|
|
|
"id",
|
|
|
|
"user-topic-settings",
|
|
|
|
);
|
|
|
|
const $status_element = $.create("fake.topics_panel_status_element").addClass(
|
|
|
|
"alert-notification",
|
|
|
|
);
|
|
|
|
$topics_panel_header.set_find_results(".alert-notification", $status_element);
|
|
|
|
$topic_tr_html.closest = (opts) => {
|
|
|
|
assert.equal(opts, "#user-topic-settings");
|
|
|
|
return $topics_panel_header;
|
|
|
|
};
|
2018-04-09 15:22:26 +02:00
|
|
|
|
2021-01-03 12:28:09 +01:00
|
|
|
let topic_data_called = 0;
|
2022-01-25 11:36:19 +01:00
|
|
|
$topic_tr_html.attr = (opts) => {
|
2022-04-09 23:44:38 +02:00
|
|
|
topic_data_called += 1;
|
|
|
|
switch (opts) {
|
|
|
|
case "data-stream-id":
|
|
|
|
return frontend.stream_id;
|
|
|
|
case "data-topic":
|
|
|
|
return "js";
|
|
|
|
/* istanbul ignore next */
|
|
|
|
default:
|
|
|
|
throw new Error(`Unknown attribute ${opts}`);
|
2018-04-09 15:22:26 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-14 18:13:13 +02:00
|
|
|
let user_topic_visibility_policy_changed = false;
|
2023-08-04 23:40:48 +02:00
|
|
|
override_rewire(
|
|
|
|
user_topics,
|
|
|
|
"set_user_topic_visibility_policy",
|
|
|
|
(stream_id, topic, visibility_policy) => {
|
|
|
|
assert.equal(stream_id, frontend.stream_id);
|
|
|
|
assert.equal(topic, "js");
|
|
|
|
assert.equal(visibility_policy, user_topics.all_visibility_policies.UNMUTED);
|
|
|
|
user_topic_visibility_policy_changed = true;
|
|
|
|
},
|
|
|
|
);
|
2024-05-03 23:19:27 +02:00
|
|
|
const topic_fake_this = {
|
|
|
|
to_$: () => $topic_fake_this,
|
|
|
|
value: user_topics.all_visibility_policies.UNMUTED,
|
|
|
|
};
|
|
|
|
topic_change_handler.call(topic_fake_this, event);
|
2023-04-14 18:13:13 +02:00
|
|
|
assert.ok(user_topic_visibility_policy_changed);
|
2021-01-03 12:28:09 +01:00
|
|
|
assert.equal(topic_data_called, 2);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|