mirror of
https://github.com/zulip/zulip.git
synced 2025-10-30 03:23:50 +00:00
We now just use a module._load hook to inject stubs into our code. For conversion purposes I temporarily maintain the API of rewiremock, apart from the enable/disable pieces, but I will make a better wrapper in an upcoming commit. We can detect when rewiremock is called after zrequire now, and I fix all the violations in this commit, mostly by using override. We can also detect when a mock is needlessly created, and I fix all the violations in this commit. The one minor nuisance that this commit introduces is that you can only stub out modules in the Zulip source tree, which is now static/js. This should not really be a problem--there are usually better techniques to deal with third party depenencies. In the prior commit I show a typical workaround, which is to create a one-line wrapper in your test code. It's often the case that you can simply use override(), as well. In passing I kill off `reset_modules`, and I eliminated the second argument to zrequire, which dates back to pre-es6 days.
180 lines
4.7 KiB
JavaScript
180 lines
4.7 KiB
JavaScript
"use strict";
|
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
const {rewiremock, set_global, zrequire} = require("../zjsunit/namespace");
|
|
const {run_test} = require("../zjsunit/test");
|
|
const $ = require("../zjsunit/zjquery");
|
|
|
|
const events = [
|
|
{
|
|
data: {
|
|
option: "First option",
|
|
idx: 1,
|
|
type: "new_option",
|
|
},
|
|
sender_id: 101,
|
|
},
|
|
{
|
|
data: {
|
|
option: "Second option",
|
|
idx: 1,
|
|
type: "new_option",
|
|
},
|
|
sender_id: 102,
|
|
},
|
|
{
|
|
data: {
|
|
option: "Third option",
|
|
idx: 1,
|
|
type: "new_option",
|
|
},
|
|
sender_id: 102,
|
|
},
|
|
];
|
|
|
|
let widget_elem;
|
|
let is_event_handled;
|
|
let is_widget_activated;
|
|
rewiremock("../../static/js/poll_widget").with({
|
|
activate(data) {
|
|
is_widget_activated = true;
|
|
widget_elem = data.elem;
|
|
assert(widget_elem.hasClass("widget-content"));
|
|
widget_elem.handle_events = (e) => {
|
|
is_event_handled = true;
|
|
assert.notDeepStrictEqual(e, events);
|
|
events.shift();
|
|
assert.deepStrictEqual(e, events);
|
|
};
|
|
data.callback("test_data");
|
|
},
|
|
});
|
|
set_global("document", "document-stub");
|
|
|
|
const narrow_state = {__esModule: true};
|
|
rewiremock("../../static/js/narrow_state").with(narrow_state);
|
|
set_global("current_msg_list", {});
|
|
|
|
const widgetize = zrequire("widgetize");
|
|
|
|
run_test("activate", (override) => {
|
|
// Both widgetize.activate and widgetize.handle_event are tested
|
|
// here to use the "caching" of widgets
|
|
const row = $.create("<stub message row>");
|
|
row.attr("id", "zhome2909");
|
|
const message_content = $.create("#zhome2909");
|
|
row.set_find_results(".message_content", message_content);
|
|
|
|
let narrow_active;
|
|
override(narrow_state, "active", () => narrow_active);
|
|
|
|
const opts = {
|
|
events: events.slice(),
|
|
extra_data: "",
|
|
message: {
|
|
id: 2001,
|
|
},
|
|
post_to_server: (data) => {
|
|
assert.equal(data.msg_type, "widget");
|
|
assert.equal(data.data, "test_data");
|
|
},
|
|
row,
|
|
widget_type: "poll",
|
|
};
|
|
|
|
let is_widget_elem_inserted;
|
|
|
|
message_content.append = (elem) => {
|
|
is_widget_elem_inserted = true;
|
|
assert.equal(elem, widget_elem);
|
|
assert(elem.hasClass("widget-content"));
|
|
};
|
|
|
|
is_widget_elem_inserted = false;
|
|
is_widget_activated = false;
|
|
is_event_handled = false;
|
|
assert(!widgetize.widget_contents.has(opts.message.id));
|
|
|
|
widgetize.activate(opts);
|
|
|
|
assert(is_widget_elem_inserted);
|
|
assert(is_widget_activated);
|
|
assert(is_event_handled);
|
|
assert.equal(widgetize.widget_contents.get(opts.message.id), widget_elem);
|
|
|
|
is_widget_elem_inserted = false;
|
|
is_widget_activated = false;
|
|
is_event_handled = false;
|
|
|
|
widgetize.activate(opts);
|
|
|
|
assert(is_widget_elem_inserted);
|
|
assert(!is_widget_activated);
|
|
assert(!is_event_handled);
|
|
|
|
narrow_active = true;
|
|
is_widget_elem_inserted = false;
|
|
is_widget_activated = false;
|
|
is_event_handled = false;
|
|
|
|
widgetize.activate(opts);
|
|
|
|
assert(!is_widget_elem_inserted);
|
|
assert(!is_widget_activated);
|
|
assert(!is_event_handled);
|
|
|
|
blueslip.expect("warn", "unknown widget_type");
|
|
narrow_active = false;
|
|
is_widget_elem_inserted = false;
|
|
is_widget_activated = false;
|
|
is_event_handled = false;
|
|
opts.widget_type = "invalid_widget";
|
|
|
|
widgetize.activate(opts);
|
|
assert(!is_widget_elem_inserted);
|
|
assert(!is_widget_activated);
|
|
assert(!is_event_handled);
|
|
assert.equal(blueslip.get_test_logs("warn")[0].more_info, "invalid_widget");
|
|
|
|
opts.widget_type = "tictactoe";
|
|
|
|
widgetize.activate(opts);
|
|
assert(!is_widget_elem_inserted);
|
|
assert(!is_widget_activated);
|
|
assert(!is_event_handled);
|
|
|
|
/* Testing widgetize.handle_events */
|
|
const post_activate_event = {
|
|
data: {
|
|
idx: 1,
|
|
type: "new_option",
|
|
},
|
|
message_id: 2001,
|
|
sender_id: 102,
|
|
};
|
|
widget_elem.handle_events = (e) => {
|
|
is_event_handled = true;
|
|
assert.deepEqual(e, [post_activate_event]);
|
|
};
|
|
is_event_handled = false;
|
|
widgetize.handle_event(post_activate_event);
|
|
assert(is_event_handled);
|
|
|
|
is_event_handled = false;
|
|
post_activate_event.message_id = 1000;
|
|
widgetize.handle_event(post_activate_event);
|
|
assert(!is_event_handled);
|
|
|
|
/* Test narrow change message update */
|
|
override(current_msg_list, "get", (idx) => {
|
|
assert.equal(idx, 2001);
|
|
return {};
|
|
});
|
|
override(current_msg_list, "get_row", (idx) => {
|
|
assert.equal(idx, 2001);
|
|
return row;
|
|
});
|
|
widgetize.set_widgets_for_list();
|
|
});
|