2020-07-24 17:44:09 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const assert = require("node:assert/strict");
|
2020-11-30 23:46:45 +01:00
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
2024-01-24 14:03:43 +01:00
|
|
|
const {current_user} = require("./lib/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-01-21 15:55:49 +01:00
|
|
|
const peer_data = zrequire("peer_data");
|
2024-01-24 14:03:43 +01:00
|
|
|
const people = zrequire("people");
|
2021-02-10 04:53:22 +01:00
|
|
|
const stream_data = zrequire("stream_data");
|
|
|
|
const stream_pill = zrequire("stream_pill");
|
2020-07-24 17:44:09 +02:00
|
|
|
|
|
|
|
const denmark = {
|
2022-02-22 15:25:13 +01:00
|
|
|
stream_id: 101,
|
2020-07-24 17:44:09 +02:00
|
|
|
name: "Denmark",
|
|
|
|
subscribed: true,
|
|
|
|
};
|
|
|
|
const sweden = {
|
2022-02-22 15:25:13 +01:00
|
|
|
stream_id: 102,
|
2020-07-24 17:44:09 +02:00
|
|
|
name: "Sweden",
|
|
|
|
subscribed: false,
|
|
|
|
};
|
2024-01-24 14:03:43 +01:00
|
|
|
const germany = {
|
|
|
|
stream_id: 103,
|
|
|
|
name: "Germany",
|
|
|
|
subscribed: false,
|
|
|
|
invite_only: true,
|
|
|
|
};
|
2020-07-24 17:44:09 +02:00
|
|
|
|
2024-07-30 06:13:31 +02:00
|
|
|
peer_data.set_subscribers(denmark.stream_id, [1, 2, 77]);
|
2021-01-21 15:55:49 +01:00
|
|
|
peer_data.set_subscribers(sweden.stream_id, [1, 2, 3, 4, 5]);
|
|
|
|
|
2020-07-24 17:44:09 +02:00
|
|
|
const denmark_pill = {
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "stream",
|
2024-07-31 00:46:26 +02:00
|
|
|
stream_id: denmark.stream_id,
|
2024-07-30 06:13:31 +02:00
|
|
|
show_subscriber_count: true,
|
2020-07-24 17:44:09 +02:00
|
|
|
};
|
|
|
|
const sweden_pill = {
|
2021-04-27 16:56:20 +02:00
|
|
|
type: "stream",
|
2024-07-31 00:46:26 +02:00
|
|
|
stream_id: sweden.stream_id,
|
2024-07-30 06:13:31 +02:00
|
|
|
show_subscriber_count: true,
|
2020-07-24 17:44:09 +02:00
|
|
|
};
|
|
|
|
|
2024-01-24 14:03:43 +01:00
|
|
|
const subs = [denmark, sweden, germany];
|
2020-07-24 17:44:09 +02:00
|
|
|
for (const sub of subs) {
|
|
|
|
stream_data.add_sub(sub);
|
|
|
|
}
|
|
|
|
|
2024-01-24 14:03:43 +01:00
|
|
|
const me = {
|
|
|
|
email: "me@example.com",
|
|
|
|
user_id: 5,
|
|
|
|
full_name: "Me Myself",
|
|
|
|
};
|
|
|
|
|
|
|
|
people.add_active_user(me);
|
|
|
|
people.initialize_current_user(me.user_id);
|
|
|
|
|
2024-10-09 21:21:41 +02:00
|
|
|
run_test("create_item", ({override}) => {
|
|
|
|
override(current_user, "user_id", me.user_id);
|
|
|
|
override(current_user, "is_admin", true);
|
2024-01-24 14:03:43 +01:00
|
|
|
function test_create_item(
|
|
|
|
stream_name,
|
|
|
|
current_items,
|
|
|
|
expected_item,
|
|
|
|
stream_prefix_required = true,
|
|
|
|
get_allowed_streams = stream_data.get_unsorted_subs,
|
|
|
|
) {
|
|
|
|
const item = stream_pill.create_item_from_stream_name(
|
|
|
|
stream_name,
|
|
|
|
current_items,
|
|
|
|
stream_prefix_required,
|
|
|
|
get_allowed_streams,
|
|
|
|
);
|
2020-07-24 17:44:09 +02:00
|
|
|
assert.deepEqual(item, expected_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
test_create_item("sweden", [], undefined);
|
|
|
|
test_create_item("#sweden", [sweden_pill], undefined);
|
|
|
|
test_create_item(" #sweden", [], sweden_pill);
|
|
|
|
test_create_item("#test", [], undefined);
|
2024-01-24 14:03:43 +01:00
|
|
|
test_create_item("#germany", [], undefined, true, stream_data.get_invite_stream_data);
|
2020-07-24 17:44:09 +02:00
|
|
|
});
|
|
|
|
|
2024-07-30 06:13:31 +02:00
|
|
|
run_test("display_value", () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
stream_pill.get_display_value_from_item(denmark_pill),
|
|
|
|
"translated: Denmark: 3 users",
|
|
|
|
);
|
|
|
|
assert.deepEqual(
|
|
|
|
stream_pill.get_display_value_from_item(sweden_pill),
|
|
|
|
"translated: Sweden: 5 users",
|
|
|
|
);
|
|
|
|
sweden_pill.show_subscriber_count = false;
|
|
|
|
assert.deepEqual(stream_pill.get_display_value_from_item(sweden_pill), "Sweden");
|
|
|
|
});
|
|
|
|
|
2020-07-24 17:44:09 +02:00
|
|
|
run_test("get_stream_id", () => {
|
|
|
|
assert.equal(stream_pill.get_stream_name_from_item(denmark_pill), denmark.name);
|
|
|
|
});
|
2022-02-22 15:25:13 +01:00
|
|
|
|
|
|
|
run_test("get_user_ids", () => {
|
|
|
|
const items = [denmark_pill, sweden_pill];
|
|
|
|
const widget = {items: () => items};
|
|
|
|
|
|
|
|
const user_ids = stream_pill.get_user_ids(widget);
|
|
|
|
assert.deepEqual(user_ids, [1, 2, 3, 4, 5, 77]);
|
|
|
|
});
|
|
|
|
|
|
|
|
run_test("get_stream_ids", () => {
|
|
|
|
const items = [denmark_pill, sweden_pill];
|
|
|
|
const widget = {items: () => items};
|
|
|
|
|
|
|
|
const stream_ids = stream_pill.get_stream_ids(widget);
|
|
|
|
assert.deepEqual(stream_ids, [101, 102]);
|
|
|
|
});
|
2024-07-31 00:46:26 +02:00
|
|
|
|
|
|
|
run_test("generate_pill_html", () => {
|
|
|
|
assert.deepEqual(
|
|
|
|
stream_pill.generate_pill_html(denmark_pill),
|
2024-09-10 18:30:14 +02:00
|
|
|
"<div class='pill 'data-stream-id=\"101\" tabindex=0>\n" +
|
2024-07-31 00:46:26 +02:00
|
|
|
' <span class="pill-label">\n' +
|
|
|
|
' <span class="pill-value">\n' +
|
|
|
|
'<i class="zulip-icon zulip-icon-hashtag stream-privacy-type-icon" aria-hidden="true"></i> translated: Denmark: 3 users\n' +
|
|
|
|
" </span></span>\n" +
|
|
|
|
' <div class="exit">\n' +
|
|
|
|
' <a role="button" class="zulip-icon zulip-icon-close pill-close-button"></a>\n' +
|
|
|
|
" </div>\n" +
|
|
|
|
"</div>\n",
|
|
|
|
);
|
|
|
|
});
|