mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 06:23:38 +00:00
zjsunit: Do not run $(...) automatically.
We have generally gone away from using $(...) initialization in modules that we test with zjsunit, but there are a few remaining special cases related to our billing and portico codebases.
This commit is contained in:
committed by
Steve Howell
parent
bc8647539c
commit
fbd3669461
@@ -22,6 +22,8 @@ const helpers = mock_esm("../../static/js/billing/helpers", {
|
||||
set_tab: () => {},
|
||||
});
|
||||
|
||||
zrequire("billing/billing");
|
||||
|
||||
run_test("initialize", (override) => {
|
||||
let token_func;
|
||||
|
||||
@@ -69,7 +71,7 @@ run_test("initialize", (override) => {
|
||||
$("#payment-method").data = (key) =>
|
||||
document.querySelector("#payment-method").getAttribute("data-" + key);
|
||||
|
||||
zrequire("billing/billing");
|
||||
$.get_initialize_function()();
|
||||
|
||||
assert(set_tab_called);
|
||||
assert(stripe_checkout_configure_called);
|
||||
|
||||
@@ -16,7 +16,9 @@ const common = zrequire("common");
|
||||
|
||||
run_test("basics", () => {
|
||||
common.autofocus("#home");
|
||||
$.get_initialize_function()();
|
||||
assert($("#home").is_focused());
|
||||
$.clear_initialize_function();
|
||||
});
|
||||
|
||||
run_test("phrase_match", () => {
|
||||
|
||||
@@ -8,7 +8,7 @@ const {run_test} = require("../zjsunit/test");
|
||||
const $ = require("../zjsunit/zjquery");
|
||||
|
||||
const noop = () => {};
|
||||
stub_templates(() => noop);
|
||||
stub_templates(() => "<stub>");
|
||||
|
||||
const page_params = set_global("page_params", {});
|
||||
mock_cjs("jquery", $);
|
||||
|
||||
@@ -14,9 +14,10 @@ const dom = new JSDOM(template, {pretendToBeVisual: true});
|
||||
const document = dom.window.document;
|
||||
|
||||
mock_cjs("jquery", $);
|
||||
zrequire("../js/analytics/support");
|
||||
|
||||
run_test("scrub_realm", () => {
|
||||
zrequire("../js/analytics/support");
|
||||
$.get_initialize_function()();
|
||||
const click_handler = $("body").get_on_handler("click", ".scrub-realm-button");
|
||||
|
||||
const fake_this = $.create("fake-.scrub-realm-button");
|
||||
|
||||
@@ -28,6 +28,7 @@ set_global("page_params", {
|
||||
mock_cjs("jquery", $);
|
||||
|
||||
const helpers = zrequire("../js/billing/helpers");
|
||||
zrequire("../js/billing/upgrade");
|
||||
|
||||
run_test("initialize", (override) => {
|
||||
let token_func;
|
||||
@@ -103,7 +104,7 @@ run_test("initialize", (override) => {
|
||||
$("#autopay-form").data = (key) =>
|
||||
document.querySelector("#autopay-form").getAttribute("data-" + key);
|
||||
|
||||
zrequire("../js/billing/upgrade");
|
||||
$.get_initialize_function()();
|
||||
|
||||
const e = {
|
||||
preventDefault: noop,
|
||||
|
||||
@@ -70,6 +70,7 @@ function short_tb(tb) {
|
||||
}
|
||||
|
||||
function run_one_module(file) {
|
||||
zjquery.clear_initialize_function();
|
||||
zjquery.clear_all_elements();
|
||||
console.info("running test " + path.basename(file, ".js"));
|
||||
test.set_current_file_name(file);
|
||||
|
||||
@@ -486,13 +486,21 @@ function make_zjquery() {
|
||||
return proxy;
|
||||
}
|
||||
|
||||
let initialize_function;
|
||||
|
||||
const zjquery = function (arg, arg2) {
|
||||
if (typeof arg === "function") {
|
||||
// If somebody is passing us a function, we emulate
|
||||
// jQuery's behavior of running this function after
|
||||
// page load time. But there are no pages to load,
|
||||
// so we just call it right away.
|
||||
arg();
|
||||
if (initialize_function) {
|
||||
throw new Error(`
|
||||
We are trying to avoid the $(...) mechanism
|
||||
for initializing modules in our codebase,
|
||||
and the code that you are compiling/running
|
||||
has tried to do this twice. Please either
|
||||
clean up the real code or reduce the scope
|
||||
of what you are testing in this test module.
|
||||
`);
|
||||
}
|
||||
initialize_function = arg;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -533,6 +541,14 @@ function make_zjquery() {
|
||||
return elems.get(selector);
|
||||
};
|
||||
|
||||
zjquery.get_initialize_function = function () {
|
||||
return initialize_function;
|
||||
};
|
||||
|
||||
zjquery.clear_initialize_function = function () {
|
||||
initialize_function = undefined;
|
||||
};
|
||||
|
||||
zjquery.create = function (name, opts) {
|
||||
assert(!elems.has(name), "You already created an object with this name!!");
|
||||
const elem = new_elem(name, opts);
|
||||
|
||||
@@ -7,6 +7,7 @@ if (module.hot) {
|
||||
|
||||
export const status_classes = "alert-error alert-success alert-info alert-warning";
|
||||
|
||||
// TODO: Move this to the portico codebase.
|
||||
export function autofocus(selector) {
|
||||
$(() => {
|
||||
$(selector).trigger("focus");
|
||||
|
||||
Reference in New Issue
Block a user