2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const rm = zrequire("rendered_markdown");
|
|
|
|
zrequire("people");
|
|
|
|
zrequire("user_groups");
|
|
|
|
zrequire("stream_data");
|
|
|
|
zrequire("timerender");
|
|
|
|
set_global("$", global.make_zjquery());
|
|
|
|
|
|
|
|
set_global("rtl", {
|
|
|
|
get_direction: () => "ltr",
|
2020-05-21 05:27:04 +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) => {
|
2020-07-02 01:39:34 +02:00
|
|
|
array.forEach((e) => {
|
2020-05-21 05:27:04 +02:00
|
|
|
func.call(e);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
return {each};
|
|
|
|
};
|
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
set_global("page_params", {emojiset: "apple"});
|
2020-05-21 05:27:04 +02:00
|
|
|
|
|
|
|
const get_content_element = () => {
|
|
|
|
$.clear_all_elements();
|
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-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]));
|
|
|
|
blueslip.expect("error", "Moment 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
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($timestamp.text(), "Thu, Jan 1 1970, 12:00 AM");
|
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.
|
|
|
|
const old_page_params = global.page_params;
|
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
set_global("page_params", {...old_page_params, twenty_four_hour_time: true});
|
2020-07-15 23:54:22 +02:00
|
|
|
rm.update_elements($content);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($timestamp.text(), "Wed, Jul 15 2020, 20:40");
|
2020-07-15 23:54:22 +02:00
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
set_global("page_params", {...old_page_params, twenty_four_hour_time: false});
|
2020-07-15 23:54:22 +02:00
|
|
|
rm.update_elements($content);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal($timestamp.text(), "Wed, Jul 15 2020, 8:40 PM");
|
2020-07-15 23:54:22 +02:00
|
|
|
|
|
|
|
// Set page_params back to its original value.
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("page_params", old_page_params);
|
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
|
|
|
|
|
|
|
// Set page paramaters 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);
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(toggle_button_html + "<p>translated: Spoiler</p>", $header.html());
|
2020-05-21 06:10:49 +02:00
|
|
|
});
|