2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-03-25 22:35:45 +01:00
|
|
|
const {mock_cjs, mock_esm, with_field, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-03-16 23:38:59 +01:00
|
|
|
const blueslip = require("../zjsunit/zblueslip");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2021-03-25 22:35:45 +01:00
|
|
|
const {page_params} = require("../zjsunit/zpage_params");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-03-11 05:43:45 +01:00
|
|
|
mock_cjs("jquery", $);
|
2021-03-10 06:10:32 +01:00
|
|
|
mock_esm("../../static/js/rtl", {
|
2020-12-01 23:21:38 +01:00
|
|
|
get_direction: () => "ltr",
|
|
|
|
});
|
2021-03-25 22:35:45 +01:00
|
|
|
page_params.emojiset = "apple";
|
2020-12-01 23:21:38 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const rm = zrequire("rendered_markdown");
|
2020-08-20 21:24:06 +02:00
|
|
|
const people = zrequire("people");
|
2021-02-10 04:53:22 +01:00
|
|
|
const user_groups = zrequire("user_groups");
|
|
|
|
const stream_data = zrequire("stream_data");
|
2020-07-15 01:29:15 +02:00
|
|
|
|
2020-05-21 05:39:58 +02:00
|
|
|
const iago = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "iago@zulip.com",
|
2020-05-21 05:39:58 +02:00
|
|
|
user_id: 30,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Iago",
|
2020-05-21 05:39:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const cordelia = {
|
2020-07-15 01:29:15 +02:00
|
|
|
email: "cordelia@zulup.com",
|
2020-05-21 05:39:58 +02:00
|
|
|
user_id: 31,
|
2020-07-15 01:29:15 +02:00
|
|
|
full_name: "Cordelia",
|
2020-05-21 05:39:58 +02:00
|
|
|
};
|
|
|
|
people.init();
|
2020-05-26 22:34:15 +02:00
|
|
|
people.add_active_user(iago);
|
|
|
|
people.add_active_user(cordelia);
|
2020-05-21 05:39:58 +02:00
|
|
|
people.initialize_current_user(iago.user_id);
|
|
|
|
|
|
|
|
const group_me = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "my user group",
|
2020-05-21 05:39:58 +02:00
|
|
|
id: 1,
|
|
|
|
members: [iago.user_id, cordelia.user_id],
|
|
|
|
};
|
|
|
|
const group_other = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "other user group",
|
2020-05-21 05:39:58 +02:00
|
|
|
id: 2,
|
|
|
|
members: [cordelia.user_id],
|
|
|
|
};
|
|
|
|
user_groups.initialize({
|
|
|
|
realm_user_groups: [group_me, group_other],
|
|
|
|
});
|
|
|
|
|
2020-05-21 05:57:15 +02:00
|
|
|
const stream = {
|
|
|
|
subscribed: true,
|
2020-07-15 01:29:15 +02:00
|
|
|
color: "yellow",
|
|
|
|
name: "test",
|
2020-05-21 05:57:15 +02:00
|
|
|
stream_id: 3,
|
|
|
|
is_muted: true,
|
|
|
|
invite_only: false,
|
|
|
|
};
|
|
|
|
stream_data.add_sub(stream);
|
|
|
|
|
2020-05-21 05:27:04 +02:00
|
|
|
const $array = (array) => {
|
|
|
|
const each = (func) => {
|
2021-01-22 22:29:08 +01:00
|
|
|
for (const e of array) {
|
2020-05-21 05:27:04 +02:00
|
|
|
func.call(e);
|
2021-01-22 22:29:08 +01:00
|
|
|
}
|
2020-05-21 05:27:04 +02:00
|
|
|
};
|
|
|
|
return {each};
|
|
|
|
};
|
|
|
|
|
|
|
|
const get_content_element = () => {
|
2020-07-15 01:29:15 +02:00
|
|
|
const $content = $.create(".rendered_markdown");
|
|
|
|
$content.set_find_results(".user-mention", $array([]));
|
|
|
|
$content.set_find_results(".user-group-mention", $array([]));
|
|
|
|
$content.set_find_results("a.stream", $array([]));
|
|
|
|
$content.set_find_results("a.stream-topic", $array([]));
|
|
|
|
$content.set_find_results("time", $array([]));
|
|
|
|
$content.set_find_results("span.timestamp-error", $array([]));
|
|
|
|
$content.set_find_results(".emoji", $array([]));
|
|
|
|
$content.set_find_results("div.spoiler-header", $array([]));
|
2020-08-20 16:11:08 +02:00
|
|
|
$content.set_find_results("div.codehilite", $array([]));
|
2020-05-21 05:27:04 +02:00
|
|
|
return $content;
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("misc_helpers", () => {
|
|
|
|
const elem = $.create(".user-mention");
|
|
|
|
rm.set_name_in_mention_element(elem, "Aaron");
|
|
|
|
assert.equal(elem.text(), "@Aaron");
|
|
|
|
elem.addClass("silent");
|
|
|
|
rm.set_name_in_mention_element(elem, "Aaron, but silent");
|
|
|
|
assert.equal(elem.text(), "Aaron, but silent");
|
2020-05-21 04:15:27 +02:00
|
|
|
});
|
2020-05-21 05:27:04 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("user-mention", () => {
|
2020-05-21 05:27:04 +02:00
|
|
|
// Setup
|
|
|
|
const $content = get_content_element();
|
2020-07-15 01:29:15 +02:00
|
|
|
const $iago = $.create(".user-mention(iago)");
|
|
|
|
$iago.set_find_results(".highlight", false);
|
|
|
|
$iago.attr("data-user-id", iago.user_id);
|
|
|
|
const $cordelia = $.create(".user-mention(cordelia)");
|
|
|
|
$cordelia.set_find_results(".highlight", false);
|
|
|
|
$cordelia.attr("data-user-id", cordelia.user_id);
|
|
|
|
$content.set_find_results(".user-mention", $array([$iago, $cordelia]));
|
2020-05-21 05:27:04 +02:00
|
|
|
|
|
|
|
// Initial asserts
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!$iago.hasClass("user-mention-me"));
|
|
|
|
assert.equal($iago.text(), "never-been-set");
|
|
|
|
assert.equal($cordelia.text(), "never-been-set");
|
2020-05-21 05:27:04 +02:00
|
|
|
|
|
|
|
rm.update_elements($content);
|
|
|
|
|
|
|
|
// Final asserts
|
2020-07-15 01:29:15 +02:00
|
|
|
assert($iago.hasClass("user-mention-me"));
|
2020-05-21 05:27:04 +02:00
|
|
|
assert.equal($iago.text(), `@${iago.full_name}`);
|
|
|
|
assert.equal($cordelia.text(), `@${cordelia.full_name}`);
|
|
|
|
});
|
2020-05-21 05:39:58 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("user-group-mention", () => {
|
2020-05-21 05:39:58 +02:00
|
|
|
// Setup
|
|
|
|
const $content = get_content_element();
|
2020-07-15 01:29:15 +02:00
|
|
|
const $group_me = $.create(".user-group-mention(me)");
|
|
|
|
$group_me.set_find_results(".highlight", false);
|
|
|
|
$group_me.attr("data-user-group-id", group_me.id);
|
|
|
|
const $group_other = $.create(".user-group-mention(other)");
|
|
|
|
$group_other.set_find_results(".highlight", false);
|
|
|
|
$group_other.attr("data-user-group-id", group_other.id);
|
|
|
|
$content.set_find_results(".user-group-mention", $array([$group_me, $group_other]));
|
2020-05-21 05:39:58 +02:00
|
|
|
|
|
|
|
// Initial asserts
|
2020-07-15 01:29:15 +02:00
|
|
|
assert(!$group_me.hasClass("user-mention-me"));
|
|
|
|
assert.equal($group_me.text(), "never-been-set");
|
|
|
|
assert.equal($group_other.text(), "never-been-set");
|
2020-05-21 05:39:58 +02:00
|
|
|
|
|
|
|
rm.update_elements($content);
|
|
|
|
|
|
|
|
// Final asserts
|
2020-07-15 01:29:15 +02:00
|
|
|
assert($group_me.hasClass("user-mention-me"));
|
2020-05-21 05:39:58 +02:00
|
|
|
assert.equal($group_me.text(), `@${group_me.name}`);
|
|
|
|
assert.equal($group_other.text(), `@${group_other.name}`);
|
|
|
|
});
|
2020-05-21 05:57:15 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("stream-links", () => {
|
2020-05-21 05:57:15 +02:00
|
|
|
// Setup
|
|
|
|
const $content = get_content_element();
|
2020-07-15 01:29:15 +02:00
|
|
|
const $stream = $.create("a.stream");
|
|
|
|
$stream.set_find_results(".highlight", false);
|
|
|
|
$stream.attr("data-stream-id", stream.stream_id);
|
|
|
|
const $stream_topic = $.create("a.stream-topic");
|
|
|
|
$stream_topic.set_find_results(".highlight", false);
|
|
|
|
$stream_topic.attr("data-stream-id", stream.stream_id);
|
|
|
|
$stream_topic.text("#random>topic name");
|
|
|
|
$content.set_find_results("a.stream", $array([$stream]));
|
|
|
|
$content.set_find_results("a.stream-topic", $array([$stream_topic]));
|
2020-05-21 05:57:15 +02:00
|
|
|
|
|
|
|
// Initial asserts
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($stream.text(), "never-been-set");
|
|
|
|
assert.equal($stream_topic.text(), "#random>topic name");
|
2020-05-21 05:57:15 +02:00
|
|
|
|
|
|
|
rm.update_elements($content);
|
|
|
|
|
|
|
|
// Final asserts
|
|
|
|
assert.equal($stream.text(), `#${stream.name}`);
|
|
|
|
assert.equal($stream_topic.text(), `#${stream.name} > topic name`);
|
|
|
|
});
|
2020-05-21 06:10:49 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("timestamp", () => {
|
2018-02-13 00:33:36 +01:00
|
|
|
// Setup
|
|
|
|
const $content = get_content_element();
|
2020-07-15 01:29:15 +02:00
|
|
|
const $timestamp = $.create("timestamp(valid)");
|
|
|
|
$timestamp.attr("datetime", "1970-01-01T00:00:01Z");
|
|
|
|
const $timestamp_invalid = $.create("timestamp(invalid)");
|
|
|
|
$timestamp_invalid.attr("datetime", "invalid");
|
|
|
|
$content.set_find_results("time", $array([$timestamp, $timestamp_invalid]));
|
2021-02-05 21:04:41 +01:00
|
|
|
blueslip.expect("error", "Could not parse datetime supplied by backend: invalid");
|
2018-02-13 00:33:36 +01:00
|
|
|
|
|
|
|
// Initial asserts
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($timestamp.text(), "never-been-set");
|
|
|
|
assert.equal($timestamp_invalid.text(), "never-been-set");
|
2018-02-13 00:33:36 +01:00
|
|
|
|
|
|
|
rm.update_elements($content);
|
|
|
|
|
|
|
|
// Final asserts
|
2021-03-03 12:18:35 +01:00
|
|
|
assert.equal($timestamp.html(), '<i class="fa fa-clock-o"></i>\nThu, Jan 1 1970, 12:00 AM\n');
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(
|
|
|
|
$timestamp.attr("title"),
|
|
|
|
"This time is in your timezone. Original text was 'never-been-set'.",
|
|
|
|
);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($timestamp_invalid.text(), "never-been-set");
|
2018-02-13 00:33:36 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("timestamp-twenty-four-hour-time", () => {
|
2020-07-15 23:54:22 +02:00
|
|
|
const $content = get_content_element();
|
2020-07-15 01:29:15 +02:00
|
|
|
const $timestamp = $.create("timestamp");
|
|
|
|
$timestamp.attr("datetime", "2020-07-15T20:40:00Z");
|
|
|
|
$content.set_find_results("time", $array([$timestamp]));
|
2020-07-15 23:54:22 +02:00
|
|
|
|
|
|
|
// We will temporarily change the 24h setting for this test.
|
2020-12-01 23:21:38 +01:00
|
|
|
with_field(page_params, "twenty_four_hour_time", true, () => {
|
|
|
|
rm.update_elements($content);
|
2021-03-03 12:18:35 +01:00
|
|
|
assert.equal($timestamp.html(), '<i class="fa fa-clock-o"></i>\nWed, Jul 15 2020, 20:40\n');
|
2020-12-01 23:21:38 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
with_field(page_params, "twenty_four_hour_time", false, () => {
|
|
|
|
rm.update_elements($content);
|
2021-03-03 12:18:35 +01:00
|
|
|
assert.equal(
|
|
|
|
$timestamp.html(),
|
|
|
|
'<i class="fa fa-clock-o"></i>\nWed, Jul 15 2020, 8:40 PM\n',
|
|
|
|
);
|
2020-12-01 23:21:38 +01:00
|
|
|
});
|
2020-07-15 23:54:22 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("timestamp-error", () => {
|
2020-07-06 17:07:44 +02:00
|
|
|
// Setup
|
|
|
|
const $content = get_content_element();
|
2020-07-15 01:29:15 +02:00
|
|
|
const $timestamp_error = $.create("timestamp-error");
|
|
|
|
$timestamp_error.text("Invalid time format: the-time-format");
|
|
|
|
$content.set_find_results("span.timestamp-error", $array([$timestamp_error]));
|
2020-07-06 17:07:44 +02:00
|
|
|
|
|
|
|
// Initial assert
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($timestamp_error.text(), "Invalid time format: the-time-format");
|
2020-07-06 17:07:44 +02:00
|
|
|
|
|
|
|
rm.update_elements($content);
|
|
|
|
|
|
|
|
// Final assert
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($timestamp_error.text(), "translated: Invalid time format: the-time-format");
|
2020-07-06 17:07:44 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("emoji", () => {
|
2020-05-21 06:10:49 +02:00
|
|
|
// Setup
|
|
|
|
const $content = get_content_element();
|
2020-07-15 01:29:15 +02:00
|
|
|
const $emoji = $.create(".emoji");
|
|
|
|
$emoji.attr("title", "tada");
|
2020-05-21 06:10:49 +02:00
|
|
|
let called = false;
|
|
|
|
$emoji.replaceWith = (f) => {
|
|
|
|
const text = f.call($emoji);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(":tada:", text);
|
2020-05-21 06:10:49 +02:00
|
|
|
called = true;
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
$content.set_find_results(".emoji", $emoji);
|
|
|
|
page_params.emojiset = "text";
|
2020-05-21 06:10:49 +02:00
|
|
|
|
|
|
|
rm.update_elements($content);
|
|
|
|
|
|
|
|
assert(called);
|
2020-04-04 22:14:34 +02:00
|
|
|
|
2020-08-11 01:47:44 +02:00
|
|
|
// Set page parameters back so that test run order is independent
|
2020-07-15 01:29:15 +02:00
|
|
|
page_params.emojiset = "apple";
|
2020-04-04 22:14:34 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("spoiler-header", () => {
|
2020-04-04 22:14:34 +02:00
|
|
|
// Setup
|
|
|
|
const $content = get_content_element();
|
2020-07-15 01:29:15 +02:00
|
|
|
const $header = $.create("div.spoiler-header");
|
|
|
|
$content.set_find_results("div.spoiler-header", $array([$header]));
|
2020-04-04 22:14:34 +02:00
|
|
|
|
2020-07-06 17:11:58 +02:00
|
|
|
// Test that the show/hide button gets added to a spoiler header.
|
2020-07-15 01:29:15 +02:00
|
|
|
const label = "My Spoiler Header";
|
2020-07-15 00:34:28 +02:00
|
|
|
const toggle_button_html =
|
|
|
|
'<span class="spoiler-button" aria-expanded="false"><span class="spoiler-arrow"></span></span>';
|
2020-04-04 22:14:34 +02:00
|
|
|
$header.html(label);
|
|
|
|
rm.update_elements($content);
|
|
|
|
assert.equal(toggle_button_html + label, $header.html());
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("spoiler-header-empty-fill", () => {
|
2020-04-04 22:14:34 +02:00
|
|
|
// Setup
|
|
|
|
const $content = get_content_element();
|
2020-07-15 01:29:15 +02:00
|
|
|
const $header = $.create("div.spoiler-header");
|
|
|
|
$content.set_find_results("div.spoiler-header", $array([$header]));
|
2020-04-04 22:14:34 +02:00
|
|
|
|
2020-07-06 17:11:58 +02:00
|
|
|
// Test that an empty header gets the default text applied (through i18n filter).
|
2020-07-15 00:34:28 +02:00
|
|
|
const toggle_button_html =
|
|
|
|
'<span class="spoiler-button" aria-expanded="false"><span class="spoiler-arrow"></span></span>';
|
2020-07-15 01:29:15 +02:00
|
|
|
$header.html("");
|
2020-04-04 22:14:34 +02:00
|
|
|
rm.update_elements($content);
|
2021-04-13 05:48:41 +02:00
|
|
|
assert.equal(toggle_button_html + "<p>translated HTML: Spoiler</p>", $header.html());
|
2020-05-21 06:10:49 +02:00
|
|
|
});
|