Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

zjquery: Move is method to FakeElement.

Anders Kaseorg21615e87bb01
diff --git a/web/tests/input_pill.test.cjs b/web/tests/input_pill.test.cjs
index 54ecbb64bd4..50ae7a4191b 100644
--- a/web/tests/input_pill.test.cjs
+++ b/web/tests/input_pill.test.cjs
@@ -544,10 +544,7 @@ run_test("misc things", () => {
const $stub = $.create("the-pill-container");
$stub.set_find_results(".input", $pill_input);
- $stub.is = (sel) => {
- assert.equal(sel, ".pill-container");
- return true;
- };
+ $stub.set_matches(".pill-container", true);
const this_ = {
to_$: () => $stub,
diff --git a/web/tests/lib/zjquery_element.cjs b/web/tests/lib/zjquery_element.cjs
index 0f7a3a0dd34..a518920764d 100644
--- a/web/tests/lib/zjquery_element.cjs
+++ b/web/tests/lib/zjquery_element.cjs
@@ -91,6 +91,7 @@ class FakeElementState {
$jquery_parent = undefined;
jquery_parents_results = new Map();
jquery_prev_results = new Map();
+ match_results = new Map([["*", true]]);
selector = undefined;
shown = false;
}
@@ -187,6 +188,15 @@ class FakeElement extends RejectMissing {
getAttributeNames() {
return this.#attributes.keys();
}
+ matches(selector) {
+ const state = fake_element_state.get(this);
+ if (!state.match_results.has(selector)) {
+ throw new Error(
+ `You need to call $(${JSON.stringify(state.selector)}).set_matches(${JSON.stringify(selector)}, boolean)`,
+ );
+ }
+ return state.match_results.get(selector);
+ }
removeAttribute(name) {
this.#attributes.delete(normalize_attribute(name));
}
@@ -500,7 +510,7 @@ exports.FakeJQuery = function (selector, opts) {
return this.is_focused();
/* istanbul ignore next */
default:
- throw new Error("zjquery does not support this is() call");
+ return [...this].some((element) => element.matches(arg));
}
},
is_focused() {
@@ -724,6 +734,10 @@ exports.FakeJQuery = function (selector, opts) {
);
}
},
+ set_matches(selector, value) {
+ assert.equal(this.length, 1);
+ fake_element_state.get(this[0]).match_results.set(selector, value);
+ },
set_next_results(selector, $result) {
assert.equal(this.length, 1);
fake_element_state.get(this[0]).jquery_next_results.set(selector, $result);
diff --git a/web/tests/notifications.test.cjs b/web/tests/notifications.test.cjs
index 8f68d954f50..257099b42cf 100644
--- a/web/tests/notifications.test.cjs
+++ b/web/tests/notifications.test.cjs
@@ -353,10 +353,7 @@ test("message_is_notifiable", ({override}) => {
test("basic_notifications", () => {
const $emoji_stub = $.create("emoji-stub");
- $emoji_stub.is = (selector) => {
- assert.equal(selector, "img");
- return false;
- };
+ $emoji_stub.set_matches("img", false);
$emoji_stub.contents = () => ({unwrap() {}});
const $katex_stub = $.set_results("katex-stub", []);
$("<div>").set_find_results(".emoji", $emoji_stub);
diff --git a/web/tests/ui_util.test.cjs b/web/tests/ui_util.test.cjs
index 2f44537a47b..fd3e92e0e09 100644
--- a/web/tests/ui_util.test.cjs
+++ b/web/tests/ui_util.test.cjs
@@ -72,7 +72,7 @@ run_test("potentially_collapse_quotes", ({override_rewire}) => {
run_test("replace_emoji_name_with_emoji_unicode", () => {
const $emoji = $.create("span").attr("class", "emoji emoji-1f419");
- $emoji.is = () => false;
+ $emoji.set_matches("img", false);
const octopus_emoji = "🐙";
assert.equal(octopus_emoji, ui_util.convert_emoji_element_to_unicode($emoji));