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.

tests: Remove mutation of $.

Anders Kaseorg0d7da771ea2f
diff --git a/web/tests/lib/zjquery.cjs b/web/tests/lib/zjquery.cjs
index 736aec73c01..9bb1ab423fe 100644
--- a/web/tests/lib/zjquery.cjs
+++ b/web/tests/lib/zjquery.cjs
@@ -196,38 +196,5 @@ function make_zjquery() {
return zjquery;
}
-const $ = new Proxy(make_zjquery(), {
- set(obj, prop, value) {
- if (obj[prop] && obj[prop]._patched_with_override) {
- obj[prop] = value;
- return true;
- }
-
- if (value._patched_with_override) {
- obj[prop] = value;
- return true;
- }
-
- /* istanbul ignore next */
- throw new Error(`
- Please don't modify $.${prop} if you are using zjquery.
-
- You can do this instead:
-
- override($, "${prop}", () => {...});
-
- Or you can do this if you don't actually
- need zjquery and just want to simulate one function.
-
- mock_cjs("jquery", {
- ${prop}(...) {...},
- });
-
- It's also possible that you are testing code with
- node tests when it would be a better strategy to
- use puppeteer tests.
- `);
- },
-});
-
+const $ = Object.freeze(make_zjquery());
module.exports = $; // eslint-disable-line no-jquery/variable-pattern
diff --git a/web/tests/message_list.test.cjs b/web/tests/message_list.test.cjs
index 7c906ed7f9b..4c45bbbc0e7 100644
--- a/web/tests/message_list.test.cjs
+++ b/web/tests/message_list.test.cjs
@@ -6,7 +6,6 @@ const {mock_esm, set_global, zrequire} = require("./lib/namespace.cjs");
const {make_stub} = require("./lib/stub.cjs");
const {run_test} = require("./lib/test.cjs");
const blueslip = require("./lib/zblueslip.cjs");
-const $ = require("./lib/zjquery.cjs");
// These unit tests for web/src/message_list.ts emphasize the model-ish
// aspects of the MessageList class. We have to stub out a few functions
@@ -14,10 +13,13 @@ const $ = require("./lib/zjquery.cjs");
const noop = function () {};
+const document_events = [];
set_global("document", {
to_$() {
return {
- trigger() {},
+ trigger(event) {
+ document_events.push(event);
+ },
};
},
});
@@ -98,11 +100,11 @@ run_test("basics", ({override}) => {
assert.deepEqual(list.all_messages(), messages);
- override($, "Event", (ev) => {
- assert.equal(ev, "message_selected.zulip");
- });
+ document_events.length = 0;
list.select_id(50);
+ assert.equal(document_events.length, 1);
+ assert.equal(document_events[0].type, "message_selected.zulip");
assert.equal(list.selected_id(), 50);
assert.equal(list.selected_idx(), 0);
@@ -116,6 +118,8 @@ run_test("basics", ({override}) => {
num_renders += 1;
};
list.reselect_selected_id();
+ assert.equal(document_events.length, 2);
+ assert.equal(document_events[1].type, "message_selected.zulip");
assert.equal(num_renders, 0);
assert.equal(list.selected_id(), 60);