From 5bdb734cdea5b6b454652b4bf9c13d9fd79527ca Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Fri, 25 Oct 2024 17:43:15 -0700 Subject: [PATCH] support: Avoid $(callback) to reduce zjquery complexity. Signed-off-by: Anders Kaseorg --- web/src/support/support.ts | 6 ++++-- web/tests/lib/index.cjs | 1 - web/tests/lib/zjquery.cjs | 26 +------------------------- web/tests/support.test.cjs | 5 ++--- 4 files changed, 7 insertions(+), 31 deletions(-) diff --git a/web/src/support/support.ts b/web/src/support/support.ts index a278ee534c..b6ccf86c41 100644 --- a/web/src/support/support.ts +++ b/web/src/support/support.ts @@ -2,7 +2,7 @@ import ClipboardJS from "clipboard"; import $ from "jquery"; import assert from "minimalistic-assert"; -$(() => { +function initialize(): void { $("body").on("click", "button.scrub-realm-button", function (this: HTMLButtonElement, e) { e.preventDefault(); const message = @@ -100,4 +100,6 @@ $(() => { } }, ); -}); +} + +initialize(); diff --git a/web/tests/lib/index.cjs b/web/tests/lib/index.cjs index 391d030780..9477b076dc 100644 --- a/web/tests/lib/index.cjs +++ b/web/tests/lib/index.cjs @@ -87,7 +87,6 @@ const noop = function () {}; require("../../src/templates.ts"); // register Zulip extensions async function run_one_module(file) { - zjquery.clear_initialize_function(); zjquery.clear_all_elements(); console.info("running test " + path.basename(file, ".test.cjs")); test.set_current_file_name(file); diff --git a/web/tests/lib/zjquery.cjs b/web/tests/lib/zjquery.cjs index 3a2c7e2c47..852f2cf652 100644 --- a/web/tests/lib/zjquery.cjs +++ b/web/tests/lib/zjquery.cjs @@ -82,24 +82,8 @@ function make_zjquery() { return proxy; } - let initialize_function; - const zjquery = function (arg, arg2) { - if (typeof arg === "function") { - assert.ok( - !initialize_function, - ` - 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; - } + assert.ok(typeof arg !== "function", "zjquery does not support $(callback)"); // If somebody is passing us an element, we return // the element itself if it's been created with @@ -141,14 +125,6 @@ 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.ok(!elems.has(name), "You already created an object with this name!!"); const $elem = new_elem(name, opts); diff --git a/web/tests/support.test.cjs b/web/tests/support.test.cjs index b73e7aaea6..bf6310091b 100644 --- a/web/tests/support.test.cjs +++ b/web/tests/support.test.cjs @@ -21,10 +21,9 @@ mock_cjs("clipboard", class Clipboard {}); zrequire("../src/support/support"); -run_test("scrub_realm", () => { - $.get_initialize_function()(); - const click_handler = $("body").get_on_handler("click", "button.scrub-realm-button"); +const click_handler = $("body").get_on_handler("click", "button.scrub-realm-button"); +run_test("scrub_realm", () => { const $fake_this = $.create("fake-.scrub-realm-button"); $fake_this.attr = (name) => { assert.equal(name, "data-string-id");