mirror of
https://github.com/zulip/zulip.git
synced 2025-10-28 02:23:57 +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.
54 lines
1.5 KiB
JavaScript
54 lines
1.5 KiB
JavaScript
"use strict";
|
|
|
|
const {strict: assert} = require("assert");
|
|
const fs = require("fs");
|
|
|
|
const {JSDOM} = require("jsdom");
|
|
|
|
const {zrequire} = require("../zjsunit/namespace");
|
|
const {run_test} = require("../zjsunit/test");
|
|
const $ = require("../zjsunit/zjquery");
|
|
|
|
const template = fs.readFileSync("templates/analytics/realm_details.html", "utf-8");
|
|
const dom = new JSDOM(template, {pretendToBeVisual: true});
|
|
const document = dom.window.document;
|
|
|
|
run_test("scrub_realm", () => {
|
|
zrequire("../js/analytics/support");
|
|
const click_handler = $("body").get_on_handler("click", ".scrub-realm-button");
|
|
|
|
const fake_this = $.create("fake-.scrub-realm-button");
|
|
fake_this.data = (name) => {
|
|
assert.equal(name, "string-id");
|
|
return "zulip";
|
|
};
|
|
|
|
let submit_form_called = false;
|
|
fake_this.form = {
|
|
submit: () => {
|
|
submit_form_called = true;
|
|
},
|
|
};
|
|
const event = {
|
|
preventDefault: () => {},
|
|
};
|
|
|
|
window.prompt = () => "zulip";
|
|
click_handler.call(fake_this, event);
|
|
assert(submit_form_called);
|
|
|
|
submit_form_called = false;
|
|
window.prompt = () => "invalid-string-id";
|
|
let alert_called = false;
|
|
window.alert = () => {
|
|
alert_called = true;
|
|
};
|
|
click_handler.call(fake_this, event);
|
|
assert(!submit_form_called);
|
|
assert(alert_called);
|
|
|
|
assert.equal(typeof click_handler, "function");
|
|
|
|
assert.equal(document.querySelectorAll(".scrub-realm-button").length, 1);
|
|
});
|