From ffe1043bcce262503229572f2217fa354f2a3f21 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Sun, 21 Feb 2021 22:03:05 +0000 Subject: [PATCH] zjsunit: Remove stub_out_jquery. We now either use zjquery or do whatever gross things we need to do to work past document.trigger(...) calls or $.now calls explicitly in the tests. --- frontend_tests/node_tests/message_edit.js | 4 +--- frontend_tests/node_tests/message_list.js | 15 +++++++++++---- frontend_tests/node_tests/server_events.js | 19 ++++++++++++++++--- frontend_tests/zjsunit/namespace.js | 11 ----------- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/frontend_tests/node_tests/message_edit.js b/frontend_tests/node_tests/message_edit.js index 38619a2d64..79ca458b3c 100644 --- a/frontend_tests/node_tests/message_edit.js +++ b/frontend_tests/node_tests/message_edit.js @@ -2,7 +2,7 @@ const {strict: assert} = require("assert"); -const {set_global, stub_out_jquery, zrequire} = require("../zjsunit/namespace"); +const {set_global, zrequire} = require("../zjsunit/namespace"); const {run_test} = require("../zjsunit/test"); set_global("document", null); @@ -10,8 +10,6 @@ set_global("page_params", { realm_community_topic_editing_limit_seconds: 259200, }); -stub_out_jquery(); - const message_edit = zrequire("message_edit"); const get_editability = message_edit.get_editability; diff --git a/frontend_tests/node_tests/message_list.js b/frontend_tests/node_tests/message_list.js index 24e06ede13..df8ec5d9db 100644 --- a/frontend_tests/node_tests/message_list.js +++ b/frontend_tests/node_tests/message_list.js @@ -2,9 +2,10 @@ const {strict: assert} = require("assert"); -const {set_global, stub_out_jquery, zrequire} = require("../zjsunit/namespace"); +const {set_global, zrequire} = require("../zjsunit/namespace"); const {make_stub} = require("../zjsunit/stub"); const {run_test} = require("../zjsunit/test"); +const $ = require("../zjsunit/zjquery"); // These unit tests for static/js/message_list.js emphasize the model-ish // aspects of the MessageList class. We have to stub out a few functions @@ -13,8 +14,14 @@ const {run_test} = require("../zjsunit/test"); const noop = function () {}; set_global("Filter", noop); -stub_out_jquery(); -set_global("document", null); +set_global("document", { + to_$() { + return { + trigger() {}, + }; + }, +}); + const narrow_state = set_global("narrow_state", {}); const stream_data = set_global("stream_data", {}); @@ -75,7 +82,7 @@ run_test("basics", (override) => { assert.deepEqual(list.all_messages(), messages); - override(global.$, "Event", (ev) => { + override($, "Event", (ev) => { assert.equal(ev, "message_selected.zulip"); }); list.select_id(50); diff --git a/frontend_tests/node_tests/server_events.js b/frontend_tests/node_tests/server_events.js index fe496f78a2..e532dac394 100644 --- a/frontend_tests/node_tests/server_events.js +++ b/frontend_tests/node_tests/server_events.js @@ -2,19 +2,32 @@ const {strict: assert} = require("assert"); -const {set_global, stub_out_jquery, zrequire} = require("../zjsunit/namespace"); +const {set_global, zrequire} = require("../zjsunit/namespace"); const {run_test} = require("../zjsunit/test"); +const $ = require("../zjsunit/zjquery"); const noop = function () {}; -set_global("document", {}); +set_global("document", { + to_$() { + return { + trigger() {}, + }; + }, +}); set_global("addEventListener", noop); -stub_out_jquery(); + +// Turn off $.now so we can import server_events. +set_global("$", { + now() {}, +}); zrequire("message_store"); const server_events = zrequire("server_events"); zrequire("sent_messages"); +set_global("$", $); + const channel = set_global("channel", {}); set_global("home_msg_list", { select_id: noop, diff --git a/frontend_tests/zjsunit/namespace.js b/frontend_tests/zjsunit/namespace.js index 962d009b10..60601ea6cb 100644 --- a/frontend_tests/zjsunit/namespace.js +++ b/frontend_tests/zjsunit/namespace.js @@ -72,17 +72,6 @@ exports.restore = function () { new_globals.clear(); }; -exports.stub_out_jquery = function () { - const $ = exports.set_global("$", () => ({ - on() {}, - trigger() {}, - hide() {}, - removeClass() {}, - })); - $.fn = {}; - $.now = function () {}; -}; - exports.with_field = function (obj, field, val, f) { const old_val = obj[field]; obj[field] = val;