Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
zjquery: Use elements for find accessor.
Anders Kaseorg26ca99c15164
diff --git a/web/tests/compose_validate.test.cjs b/web/tests/compose_validate.test.cjsindex 127ee7a106b..8baf5318fb0 100644--- a/web/tests/compose_validate.test.cjs+++ b/web/tests/compose_validate.test.cjs@@ -844,17 +844,12 @@ test_ui("test_warn_if_guest_in_dm_recipient", ({mock_template, override}) => {compose_state.private_message_recipient_emails("[email protected], [email protected]");$.reset_selector(`#compose_banners .${CSS.escape(classname)}`);$banner = $(`#compose_banners .${CSS.escape(classname)}`);- let is_updated = false;- $banner.set_find_results(".banner_content", {- text(content) {- assert.equal(- content,- $t({defaultMessage: "Guest and New Guest are guests in this organization."}),- );- is_updated = true;- },- });+ const $banner_content = $(`#compose_banners .${CSS.escape(classname)} .banner_content`);+ $banner.set_find_results(".banner_content", $banner_content);compose_validate.warn_if_guest_in_dm_recipient();- assert.ok(is_updated);+ assert.equal(+ $banner_content.text(),+ $t({defaultMessage: "Guest and New Guest are guests in this organization."}),+ );assert.deepEqual(compose_state.get_recipient_guest_ids_for_dm_warning(), [33, 34]);});diff --git a/web/tests/filter.test.cjs b/web/tests/filter.test.cjsindex d53644f9768..ebbc1df8427 100644--- a/web/tests/filter.test.cjs+++ b/web/tests/filter.test.cjs@@ -1451,7 +1451,7 @@ test("predicate_basics", ({override}) => {assert.ok(has_link(non_img_attachment_msg));set_find_results_for_msg_content(link_msg, "a", ["stub"]);assert.ok(has_link(link_msg));- set_find_results_for_msg_content(no_has_filter_matching_msg, "a", false);+ set_find_results_for_msg_content(no_has_filter_matching_msg, "a", []);assert.ok(!has_link(no_has_filter_matching_msg));const has_attachment = get_predicate([["has", "attachment"]]);@@ -1459,19 +1459,19 @@ test("predicate_basics", ({override}) => {assert.ok(has_attachment(img_msg));set_find_results_for_msg_content(non_img_attachment_msg, "a[href^='/user_uploads']", ["stub"]);assert.ok(has_attachment(non_img_attachment_msg));- set_find_results_for_msg_content(link_msg, "a[href^='/user_uploads']", false);+ set_find_results_for_msg_content(link_msg, "a[href^='/user_uploads']", []);assert.ok(!has_attachment(link_msg));- set_find_results_for_msg_content(no_has_filter_matching_msg, "a[href^='/user_uploads']", false);+ set_find_results_for_msg_content(no_has_filter_matching_msg, "a[href^='/user_uploads']", []);assert.ok(!has_attachment(no_has_filter_matching_msg));const has_image = get_predicate([["has", "image"]]);set_find_results_for_msg_content(img_msg, ".message_inline_image", ["stub"]);assert.ok(has_image(img_msg));- set_find_results_for_msg_content(non_img_attachment_msg, ".message_inline_image", false);+ set_find_results_for_msg_content(non_img_attachment_msg, ".message_inline_image", []);assert.ok(!has_image(non_img_attachment_msg));- set_find_results_for_msg_content(link_msg, ".message_inline_image", false);+ set_find_results_for_msg_content(link_msg, ".message_inline_image", []);assert.ok(!has_image(link_msg));- set_find_results_for_msg_content(no_has_filter_matching_msg, ".message_inline_image", false);+ set_find_results_for_msg_content(no_has_filter_matching_msg, ".message_inline_image", []);assert.ok(!has_image(no_has_filter_matching_msg));const has_reaction = get_predicate([["has", "reaction"]]);diff --git a/web/tests/hotkey.test.cjs b/web/tests/hotkey.test.cjsindex 7a0e7583246..50f033f5c0c 100644--- a/web/tests/hotkey.test.cjs+++ b/web/tests/hotkey.test.cjs@@ -106,11 +106,19 @@ message_lists.current = {return 42;},selected_row() {+ const $emoji_message_control_button_container = $.create(+ "emoji-message-control-button-container-stub",+ );+ $emoji_message_control_button_container.set_closest_results(+ ".message_control_button",+ $.create("emoji-message-control-button-stub"),+ );const $row = $.create("selected-row-stub");- $row.set_find_results(".message-actions-menu-button", ["<menu-button-stub>"]);- $row.set_find_results(".message_controls .emoji-message-control-button-container", {- closest: () => ({css: () => "none"}),- });+ $row.set_find_results(".message-actions-menu-button", $.create("menu-button-stub"));+ $row.set_find_results(+ ".message_controls .emoji-message-control-button-container",+ $emoji_message_control_button_container,+ );return $row;},selected_message() {diff --git a/web/tests/lib/zjquery_element.cjs b/web/tests/lib/zjquery_element.cjsindex eabcd88b81b..c8910fe659b 100644--- a/web/tests/lib/zjquery_element.cjs+++ b/web/tests/lib/zjquery_element.cjs@@ -87,12 +87,12 @@ class FakeElementState {jquery_closest_results = new Map();$jquery_contents = undefined;jquery_data = new Map();- jquery_find_results = new Map();jquery_next_results = new Map();$jquery_parent = undefined;jquery_parents_results = new Map();jquery_prev_results = new Map();match_results = new Map([["*", true]]);+ query_results = new Map();selector = undefined;shown = false;}@@ -198,6 +198,16 @@ class FakeElement extends RejectMissing {}return state.match_results.get(selector);}+ querySelectorAll(selector) {+ const state = fake_element_state.get(this);+ const results = state.query_results.get(selector);+ if (results) {+ return results;+ }+ throw new Error(+ `You need to call $(${JSON.stringify(state.selector)}).set_find_results(${JSON.stringify(selector)}, ...).`,+ );+ }removeAttribute(name) {this.#attributes.delete(normalize_attribute(name));}@@ -407,7 +417,7 @@ function dom_args(args) {}empty() {for (const element of this) {- fake_element_state.get(element).jquery_find_results.clear();+ fake_element_state.get(element).query_results.clear();element.innerHTML = "";}return this;@@ -440,31 +450,10 @@ function dom_args(args) {),);}- find(child_selector) {- assert.equal(this.length, 1);- const $child = fake_element_state.get(this[0]).jquery_find_results.get(child_selector);- if ($child) {- return $child;- }- if ($child === false) {- // This is deliberately set to simulate missing find results.- // Return an empty array, the most common check is- // if ($.find().length) { //success }- return [];- }- /* istanbul ignore next */- throw new Error(`- We need you to simulate the results of $(...).find(...)- by using set_find_results. You want something like this:-- const $container = ...;- const $child = ...;- $container.set_find_results("${child_selector}", $child);-- Then calling $container.find("${child_selector}") will return- the "$child" zjquery element.-- `);+ find(selector) {+ return new exports.FakeJQuery(+ [...this].flatMap((element) => [...element.querySelectorAll(selector)]),+ );}get(index) {return index === undefined ? [...this] : this[index];@@ -762,14 +751,9 @@ function dom_args(args) {assert.equal(this.length, 1);fake_element_state.get(this[0]).$jquery_contents = $contents;}- set_find_results(find_selector, $jquery_object) {+ set_find_results(selector, elements) {assert.equal(this.length, 1);- assert.notEqual(- $jquery_object,- undefined,- "Please make the 'find result' be something like $.create('unused')",- );- fake_element_state.get(this[0]).jquery_find_results.set(find_selector, $jquery_object);+ fake_element_state.get(this[0]).query_results.set(selector, [...elements]);}set_height(fake_height) {for (const element of this) {diff --git a/web/tests/rendered_markdown.test.cjs b/web/tests/rendered_markdown.test.cjsindex 315d9ca5172..64e504c4f34 100644--- a/web/tests/rendered_markdown.test.cjs+++ b/web/tests/rendered_markdown.test.cjs@@ -101,15 +101,6 @@ const stream = make_stream({});stream_data.add_sub_for_tests(stream);-const $array = (array) => {- const each = (func) => {- for (const e of array) {- func.call(e);- }- };- return {each};-};-function set_message_for_message_content($content, value) {// no message row foundif (value === undefined) {@@ -133,18 +124,18 @@ function set_message_for_message_content($content, value) {const get_content_element = () => {const $content = $.create("content-stub");- $content.set_find_results(".user-mention", $array([]));- $content.set_find_results(".topic-mention", $array([]));- $content.set_find_results(".user-group-mention", $array([]));- $content.set_find_results("a.stream", $array([]));- $content.set_find_results("a.stream-topic, a.message-link", $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([]));- $content.set_find_results("div.codehilite", $array([]));- $content.set_find_results(".message_inline_video video", $array([]));- $content.set_find_results("audio", $array([]));+ $content.set_find_results(".user-mention", []);+ $content.set_find_results(".topic-mention", []);+ $content.set_find_results(".user-group-mention", []);+ $content.set_find_results("a.stream", []);+ $content.set_find_results("a.stream-topic, a.message-link", []);+ $content.set_find_results("time", []);+ $content.set_find_results("span.timestamp-error", []);+ $content.set_find_results(".emoji", []);+ $content.set_find_results("div.spoiler-header", []);+ $content.set_find_results("div.codehilite", []);+ $content.set_find_results(".message_inline_video video", []);+ $content.set_find_results("audio", []);set_message_for_message_content($content, undefined);@@ -193,7 +184,7 @@ run_test("message_inline_video", () => {load_called = true;};- $content.set_find_results(".message_inline_video video", $array([$elem[0]]));+ $content.set_find_results(".message_inline_video video", $elem);window.GestureEvent = true;rm.update_elements($content);assert.equal(load_called, true);@@ -204,15 +195,15 @@ run_test("user-mention", ({override}) => {// Setupconst $content = get_content_element();const $iago = $.create("user-mention(iago)");- $iago.set_find_results(".highlight", false);+ $iago.set_find_results(".highlight", []);$iago.attr("data-user-id", iago.user_id);const $cordelia = $.create("user-mention(cordelia)");- $cordelia.set_find_results(".highlight", false);+ $cordelia.set_find_results(".highlight", []);$cordelia.attr("data-user-id", cordelia.user_id);const $polonius = $.create("user-mention(polonius)");- $polonius.set_find_results(".highlight", false);+ $polonius.set_find_results(".highlight", []);$polonius.attr("data-user-id", polonius.user_id);- $content.set_find_results(".user-mention", $array([$iago, $cordelia, $polonius]));+ $content.set_find_results(".user-mention", [$iago[0], $cordelia[0], $polonius[0]]);override(realm, "realm_enable_guest_user_indicator", true);// Initial assertsassert.ok(!$iago.hasClass("user-mention-me"));@@ -236,9 +227,9 @@ run_test("user-mention", ({override}) => {run_test("user-mention without guest indicator", ({override}) => {const $content = get_content_element();const $polonius = $.create("user-mention(polonius-again)");- $polonius.set_find_results(".highlight", false);+ $polonius.set_find_results(".highlight", []);$polonius.attr("data-user-id", polonius.user_id);- $content.set_find_results(".user-mention", $array([$polonius]));+ $content.set_find_results(".user-mention", $polonius);override(realm, "realm_enable_guest_user_indicator", false);rm.update_elements($content);@@ -248,10 +239,10 @@ run_test("user-mention without guest indicator", ({override}) => {run_test("user-mention of inaccessible users", () => {const $content = get_content_element();const $othello = $.create("user-mention(othello)");- $othello.set_find_results(".highlight", false);+ $othello.set_find_results(".highlight", []);$othello.attr("data-user-id", inaccessible_user_id);$othello.text("@Othello");- $content.set_find_results(".user-mention", $array([$othello]));+ $content.set_find_results(".user-mention", $othello);rm.update_elements($content);assert.equal($othello.text(), "@Othello");@@ -259,10 +250,10 @@ run_test("user-mention of inaccessible users", () => {// Test inaccessible user id with no user object.const $cordelia = $.create("user-mention(cordelia)");- $cordelia.set_find_results(".highlight", false);+ $cordelia.set_find_results(".highlight", []);$cordelia.attr("data-user-id", 40);$cordelia.text("@Cordelia");- $content.set_find_results(".user-mention", $array([$cordelia]));+ $content.set_find_results(".user-mention", $cordelia);rm.update_elements($content);assert.equal($cordelia.text(), "@Cordelia");@@ -273,7 +264,7 @@ run_test("user-mention (stream wildcard)", () => {const $content = get_content_element();const $mention = $.create("mention");$mention.attr("data-user-id", "*");- $content.set_find_results(".user-mention", $array([$mention]));+ $content.set_find_results(".user-mention", $mention);const message = {stream_wildcard_mentioned: true};set_message_for_message_content($content, message);@@ -287,8 +278,8 @@ run_test("user-mention (email)", () => {const $content = get_content_element();const $mention = $.create("mention");$mention.attr("data-user-email", cordelia.email);- $mention.set_find_results(".highlight", false);- $content.set_find_results(".user-mention", $array([$mention]));+ $mention.set_find_results(".highlight", []);+ $content.set_find_results(".user-mention", $mention);rm.update_elements($content);assert.ok(!$mention.hasClass("user-mention-me"));@@ -298,7 +289,7 @@ run_test("user-mention (email)", () => {run_test("user-mention (missing)", () => {const $content = get_content_element();const $mention = $.create("mention");- $content.set_find_results(".user-mention", $array([$mention]));+ $content.set_find_results(".user-mention", $mention);rm.update_elements($content);assert.ok(!$mention.hasClass("user-mention-me"));@@ -308,7 +299,7 @@ run_test("topic-mention", () => {// Setupconst $content = get_content_element();const $mention = $.create("mention");- $content.set_find_results(".topic-mention", $array([$mention]));+ $content.set_find_results(".topic-mention", $mention);// when no message row foundassert.ok(!$mention.hasClass("user-mention-me"));@@ -330,7 +321,7 @@ run_test("topic-mention not topic participant", () => {// Setupconst $content = get_content_element();const $mention = $.create("mention");- $content.set_find_results(".topic-mention", $array([$mention]));+ $content.set_find_results(".topic-mention", $mention);const message = {topic_wildcard_mentioned: false,@@ -346,12 +337,12 @@ run_test("user-group-mention", () => {// Setupconst $content = get_content_element();const $group_me = $.create("user-group-mention(me)");- $group_me.set_find_results(".highlight", false);+ $group_me.set_find_results(".highlight", []);$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.set_find_results(".highlight", []);$group_other.attr("data-user-group-id", group_other.id);- $content.set_find_results(".user-group-mention", $array([$group_me, $group_other]));+ $content.set_find_results(".user-group-mention", [$group_me[0], $group_other[0]]);// Initial assertsassert.ok(!$group_me.hasClass("user-mention-me"));@@ -370,15 +361,12 @@ run_test("user-group-mention", () => {// Setupconst $content = get_content_element();const $group_me_via_subgroup = $.create("user-group-mention(me_via_subgroup)");- $group_me_via_subgroup.set_find_results(".highlight", false);+ $group_me_via_subgroup.set_find_results(".highlight", []);$group_me_via_subgroup.attr("data-user-group-id", group_me_via_subgroup.id);const $group_other = $.create("user-group-mention(other)");- $group_other.set_find_results(".highlight", false);+ $group_other.set_find_results(".highlight", []);$group_other.attr("data-user-group-id", group_other.id);- $content.set_find_results(- ".user-group-mention",- $array([$group_me_via_subgroup, $group_other]),- );+ $content.set_find_results(".user-group-mention", [$group_me_via_subgroup[0], $group_other[0]]);// Initial assertsassert.ok(!$group_me_via_subgroup.hasClass("user-mention-me"));@@ -397,7 +385,7 @@ run_test("user-group-mention (error)", () => {const $content = get_content_element();const $group = $.create("user-group-mention(bogus)");$group.attr("data-user-group-id", "not-even-a-number");- $content.set_find_results(".user-group-mention", $array([$group]));+ $content.set_find_results(".user-group-mention", $group);rm.update_elements($content);@@ -408,11 +396,11 @@ run_test("stream-links", ({mock_template}) => {// Setupconst $content = get_content_element();const $stream = $.create("a.stream");- $stream.set_find_results(".highlight", false);+ $stream.set_find_results(".highlight", []);$stream.attr("data-stream-id", stream.stream_id);const $stream_topic = $.create("a.stream-topic");- $stream_topic.set_find_results(".highlight", false);+ $stream_topic.set_find_results(".highlight", []);$stream_topic.attr("href",`/#narrow/channel/${stream.stream_id}-random/topic/topic.20name.20.3E.20still.20the.20topic.20name`,@@ -421,8 +409,8 @@ run_test("stream-links", ({mock_template}) => {$stream_topic.addClass("stream-topic");$stream_topic.text("#random > topic name > still the topic name");- $content.set_find_results("a.stream", $array([$stream]));- $content.set_find_results("a.stream-topic, a.message-link", $array([$stream_topic]));+ $content.set_find_results("a.stream", $stream);+ $content.set_find_results("a.stream-topic, a.message-link", $stream_topic);let topic_link_context;let topic_link_rendered_html;@@ -454,12 +442,12 @@ run_test("topic-link (empty string topic)", ({mock_template}) => {// Setupconst $content = get_content_element();const $channel_topic = $.create("a.stream-topic(empty-string-topic)");- $channel_topic.set_find_results(".highlight", false);+ $channel_topic.set_find_results(".highlight", []);$channel_topic.attr("href", `/#narrow/channel/${stream.stream_id}-random/topic/`);$channel_topic[0].replaceWith = noop;$channel_topic.addClass("stream-topic");$channel_topic.html(`#random > <em>${REALM_EMPTY_TOPIC_DISPLAY_NAME}</em>`);- $content.set_find_results("a.stream-topic, a.message-link", $array([$channel_topic]));+ $content.set_find_results("a.stream-topic, a.message-link", $channel_topic);let topic_link_context;let topic_link_rendered_html;@@ -489,7 +477,7 @@ run_test("message-links", ({mock_template}) => {// Setupconst $content = get_content_element();const $channel_topic_message = $.create("a.message-link");- $channel_topic_message.set_find_results(".highlight", false);+ $channel_topic_message.set_find_results(".highlight", []);$channel_topic_message.attr("href",`/#narrow/channel/${stream.stream_id}-${stream.name}/topic//near/123`,@@ -499,7 +487,7 @@ run_test("message-links", ({mock_template}) => {$channel_topic_message.html(`#${stream.name} > <em>${REALM_EMPTY_TOPIC_DISPLAY_NAME}</em> @ 💬`,);- $content.set_find_results("a.stream-topic, a.message-link", $array([$channel_topic_message]));+ $content.set_find_results("a.stream-topic, a.message-link", $channel_topic_message);let channel_message_link_context;let channel_message_link_rendered_html;@@ -527,7 +515,7 @@ run_test("message-links", ({mock_template}) => {run_test("timestamp without time", () => {const $content = get_content_element();const $timestamp = $.create("timestamp without actual time");- $content.set_find_results("time", $array([$timestamp]));+ $content.set_find_results("time", $timestamp);rm.update_elements($content);assert.equal($timestamp.text(), "never-been-set");@@ -543,7 +531,7 @@ run_test("audio", ({mock_template}) => {$audio.attr("src", audio_src);$audio.attr("title", audio_title);- $content.set_find_results("audio", $array([$audio]));+ $content.set_find_results("audio", $audio);let audio_html;mock_template("markdown_audio.hbs", true, (data, html) => {@@ -578,7 +566,7 @@ run_test("timestamp", ({mock_template}) => {$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]));+ $content.set_find_results("time", [$timestamp[0], $timestamp_invalid[0]]);blueslip.expect("error", "Could not parse datetime supplied by backend");// Initial asserts@@ -605,7 +593,7 @@ run_test("timestamp-twenty-four-hour-time", ({mock_template, override}) => {const $content = get_content_element();const $timestamp = $.create("timestamp");$timestamp.attr("datetime", "2020-07-15T20:40:00Z");- $content.set_find_results("time", $array([$timestamp]));+ $content.set_find_results("time", $timestamp);// We will temporarily change the 24h setting for this test.override(user_settings, "twenty_four_hour_time", true);@@ -628,7 +616,7 @@ run_test("timestamp-error", () => {const $content = get_content_element();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]));+ $content.set_find_results("span.timestamp-error", $timestamp_error);// Initial assertassert.equal($timestamp_error.text(), "Invalid time format: the-time-format");@@ -660,7 +648,7 @@ run_test("spoiler-header", () => {// Setupconst $content = get_content_element();const $header = $.create("div.spoiler-header");- $content.set_find_results("div.spoiler-header", $array([$header]));+ $content.set_find_results("div.spoiler-header", $header);let appended;$header[0].append = (element) => {appended = element;@@ -681,7 +669,7 @@ run_test("spoiler-header-empty-fill", () => {// Setupconst $content = get_content_element();const $header = $.create("div.spoiler-header");- $content.set_find_results("div.spoiler-header", $array([$header]));+ $content.set_find_results("div.spoiler-header", $header);const appended = [];$header[0].append = (element) => {appended.push(element);@@ -719,7 +707,7 @@ function test_code_playground(mock_template, viewing_code) {const $content = get_content_element();const $hilite = $.create("div.codehilite");const $pre = $.create("hilite-pre");- $content.set_find_results("div.codehilite", $array([$hilite]));+ $content.set_find_results("div.codehilite", $hilite);$hilite.set_find_results("pre", $pre);$hilite.attr("data-code-language", "javascript");diff --git a/web/tests/spoilers.test.cjs b/web/tests/spoilers.test.cjsindex c8b91b10b78..dd0ab2f592c 100644--- a/web/tests/spoilers.test.cjs+++ b/web/tests/spoilers.test.cjs@@ -8,16 +8,6 @@ const $ = require("./lib/zjquery.cjs");const spoilers = zrequire("spoilers");-// This function is taken from rendered_markdown.test.ts and slightly modified.-const $array = (array) => {- const each = (func) => {- for (const [index, $elem] of array.entries()) {- func.call($elem, index, $elem);- }- };- return {each};-};-const get_spoiler_elem = (title) => {const $block = $.create(`block-${title}`);const $header = $.create(`header-${title}`);@@ -33,7 +23,7 @@ run_test("hide spoilers in notifications", () => {const $root = $.create("root element");const $spoiler_1 = get_spoiler_elem("this is the title");const $spoiler_2 = get_spoiler_elem("");- $root.set_find_results(".spoiler-block", $array([$spoiler_1, $spoiler_2]));+ $root.set_find_results(".spoiler-block", [$spoiler_1[0], $spoiler_2[0]]);spoilers.hide_spoilers_in_notification($root);assert.equal($spoiler_1.find(".spoiler-header").text(), "this is the title (…)");assert.equal($spoiler_2.find(".spoiler-header").text(), "(…)");diff --git a/web/tests/zjquery.test.cjs b/web/tests/zjquery.test.cjsindex 1ec064669a2..46c46575b86 100644--- a/web/tests/zjquery.test.cjs+++ b/web/tests/zjquery.test.cjs@@ -92,8 +92,8 @@ run_test("finding_related_objects", () => {assert.equal($emoji.attr("src"), "foo.png");// Sometimes you want to deliberately test paths that do not find an- // element. You can pass 'false' as the result for those cases.- $emoji.set_find_results(".random", false);+ // element. You can pass [] as the result for those cases.+ $emoji.set_find_results(".random", []);assert.equal($emoji.find(".random").length, 0);/*An important thing to understand is that zjquery doesn't truly