Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

zjquery: Move css accessor to FakeElement.

Anders Kaseorg082477f9fc7c
diff --git a/tools/linter_lib/custom_check.py b/tools/linter_lib/custom_check.py
index 14089fc620b..1ace6f34197 100644
--- a/tools/linter_lib/custom_check.py
+++ b/tools/linter_lib/custom_check.py
@@ -215,6 +215,7 @@ js_rules = RuleList(
"exclude": {
"web/tests/compose_paste.test.cjs",
"web/tests/postprocess_content.test.cjs",
+ "web/tests/lib/zjquery_element.cjs",
},
"good_lines": ["#my-style {color: blue;}", "const style =", 'some_style = "test"'],
"bad_lines": ['<p style="color: blue;">Foo</p>', 'style = "color: blue;"'],
diff --git a/web/tests/compose_actions.test.cjs b/web/tests/compose_actions.test.cjs
index 414d5b4c8b2..ec1a697a4a1 100644
--- a/web/tests/compose_actions.test.cjs
+++ b/web/tests/compose_actions.test.cjs
@@ -150,10 +150,6 @@ function override_private_message_recipient_ids({override}) {
function test(label, f) {
run_test(label, (helpers) => {
- // We don't test the css calls; we just skip over them.
- $("#compose").css = noop;
- $(".new_message_textarea").css = noop;
-
people.init();
compose_state.set_message_type(undefined);
compose_recipient.initialize();
diff --git a/web/tests/compose_ui.test.cjs b/web/tests/compose_ui.test.cjs
index e0aa46382a1..983b3d3f22d 100644
--- a/web/tests/compose_ui.test.cjs
+++ b/web/tests/compose_ui.test.cjs
@@ -560,18 +560,12 @@ run_test("quote_message", ({override, override_rewire}) => {
});
run_test("set_compose_box_top", () => {
- let compose_top = "";
- $("#compose").css = (arg, val) => {
- assert.equal(arg, "top");
- compose_top = val;
- };
-
$("#navbar-fixed-container").set_height(50);
compose_ui.set_compose_box_top(true);
- assert.equal(compose_top, "50px");
+ assert.equal($("#compose")[0].style.getPropertyValue("top"), "50px");
compose_ui.set_compose_box_top(false);
- assert.equal(compose_top, "");
+ assert.equal($("#compose")[0].style.getPropertyValue("top"), "");
});
run_test("test_compose_height_changes", ({override, override_rewire}) => {
diff --git a/web/tests/compose_validate.test.cjs b/web/tests/compose_validate.test.cjs
index c039ea3b4db..2c645d2168a 100644
--- a/web/tests/compose_validate.test.cjs
+++ b/web/tests/compose_validate.test.cjs
@@ -159,10 +159,6 @@ function stub_message_row($textarea) {
function initialize_pm_pill(mock_template) {
$.clear_all_elements();
- $(".message_comp").css = (property) => {
- assert.equal(property, "display");
- return "block";
- };
$("#compose-send-button").trigger("focus");
$("#compose-send-button .loader").hide();
diff --git a/web/tests/drafts.test.cjs b/web/tests/drafts.test.cjs
index b13279e52dd..aa04ef6b4ee 100644
--- a/web/tests/drafts.test.cjs
+++ b/web/tests/drafts.test.cjs
@@ -155,7 +155,6 @@ const short_msg = {
function test(label, f) {
run_test(label, (helpers) => {
- $("#draft_overlay").css = noop;
$(".top_left_drafts").set_find_results(".unread_count", $("<unread-count-stub>"));
$(".compose-drafts-count-container").set_find_results(
".compose-drafts-count",
diff --git a/web/tests/example7.test.cjs b/web/tests/example7.test.cjs
index 5a43d620476..edd9d2fc0e3 100644
--- a/web/tests/example7.test.cjs
+++ b/web/tests/example7.test.cjs
@@ -75,10 +75,6 @@ const denmark_stream = make_stream({
});
run_test("unread_ops", ({override}) => {
- $("#message_feed_container").css = (property) => {
- assert.equal(property, "display");
- return "block";
- };
stream_data.clear_subscriptions();
stream_data.add_sub_for_tests(denmark_stream);
message_store.clear_for_testing();
diff --git a/web/tests/lib/compose_helpers.cjs b/web/tests/lib/compose_helpers.cjs
index 9cf58fe2188..6c8c7ca0758 100644
--- a/web/tests/lib/compose_helpers.cjs
+++ b/web/tests/lib/compose_helpers.cjs
@@ -2,7 +2,6 @@
const assert = require("node:assert/strict");
-const {noop} = require("./test.cjs");
const $ = require("./zjquery.cjs");
class FakeComposeBox {
@@ -36,7 +35,6 @@ class FakeComposeBox {
this.$content_textarea.val("default message");
this.$content_textarea.trigger("blur");
- this.$preview_message_area.css = noop;
$(".compose-submit-button .loader").show();
}
diff --git a/web/tests/lib/zjquery_element.cjs b/web/tests/lib/zjquery_element.cjs
index 43b0296a932..bed1b941ab7 100644
--- a/web/tests/lib/zjquery_element.cjs
+++ b/web/tests/lib/zjquery_element.cjs
@@ -59,7 +59,27 @@ class FakeClassList extends RejectMissing {
}
}
+class FakeStyle extends RejectMissing {
+ #style = new Map();
+
+ get length() {
+ return this.#style.size;
+ }
+ getPropertyValue(name) {
+ return this.#style.get(name) ?? "";
+ }
+ setProperty(name, value) {
+ this.#style.set(name, String(value));
+ }
+ removeProperty(name) {
+ const value = this.#style.get(name) ?? "";
+ this.#style.delete(name);
+ return value;
+ }
+}
+
class FakeElementState {
+ computed_style = new FakeStyle();
event_handlers = new Map();
delegated_event_handlers = new Map();
is_focused = false;
@@ -133,6 +153,10 @@ class FakeDataSet {
}
}
+// https://api.jquery.com/css/
+const auto_px =
+ /^(Border(Top|Right|Bottom|Left)?(Width)?|(Margin|Padding)?(Top|Right|Bottom|Left)?|(Min|Max)?(Width|Height))$/;
+
class FakeElement extends RejectMissing {
_tippy = undefined;
classList = new FakeClassList();
@@ -140,6 +164,7 @@ class FakeElement extends RejectMissing {
innerHTML = "never-been-set";
selectionEnd = undefined;
selectionStart = undefined;
+ style = new FakeStyle();
textContent = "never-been-set";
value = undefined;
@@ -224,6 +249,44 @@ exports.FakeJQuery = function (selector, opts) {
}
return state.jquery_closest_results.get(closest_selector);
},
+ css(property, ...args) {
+ if (args.length === 0 && typeof property === "string") {
+ if (!(0 in this)) {
+ return undefined;
+ }
+ return fake_element_state
+ .get(this[0])
+ .computed_style.getPropertyValue(decamel(property));
+ }
+
+ if (args.length === 0 && Array.isArray(property)) {
+ if (!(0 in this)) {
+ return undefined;
+ }
+ const state = fake_element_state.get(this[0]);
+ return Object.fromEntries(
+ property.map((key) => [
+ key,
+ state.computed_style.getPropertyValue(decamel(key)),
+ ]),
+ );
+ }
+
+ for (const element of this) {
+ for (const [key, value] of Object.entries(
+ typeof property === "string" ? {[property]: args[0]} : property,
+ )) {
+ element.style.setProperty(
+ decamel(key),
+ typeof value === "number" &&
+ auto_px.test(camel(key).replace(/^./, (c) => c.toUpperCase()))
+ ? `${value}px`
+ : value,
+ );
+ }
+ }
+ return this;
+ },
data(key, ...args) {
if (args.length === 0) {
if (!(0 in this)) {
diff --git a/web/tests/settings_org.test.cjs b/web/tests/settings_org.test.cjs
index 7c2f7e8162c..33c25536cb6 100644
--- a/web/tests/settings_org.test.cjs
+++ b/web/tests/settings_org.test.cjs
@@ -48,7 +48,6 @@ initialize_user_settings({user_settings: {}});
function test(label, f) {
run_test(label, (helpers) => {
- $("#realm-icon-upload-widget .upload-spinner-background").css = noop;
helpers.override(current_user, "is_admin", false);
helpers.override(realm, "realm_domains", [
{domain: "example.com", allow_subdomains: true},
diff --git a/web/tests/stream_data.test.cjs b/web/tests/stream_data.test.cjs
index 3aa02dac415..616e67cba97 100644
--- a/web/tests/stream_data.test.cjs
+++ b/web/tests/stream_data.test.cjs
@@ -2579,18 +2579,10 @@ test("set_max_channel_width_css_variable", async () => {
const $measure_div = $("<div>");
const $root = $(":root");
- $measure_div.css = () => $measure_div;
$measure_div[0].getBoundingClientRect = () => ({width: $measure_div.text().length});
$measure_div[0].remove = () => {};
- const set_property_calls = [];
- $root.css = (name, value) => {
- set_property_calls.push({name, value});
- };
-
await stream_data.set_max_channel_width_css_variable();
- assert.equal(set_property_calls.length, 1);
- assert.equal(set_property_calls[0].name, "--longest-subscribed-channel-name-width");
- assert.equal(set_property_calls[0].value, "3px");
+ assert.equal($root[0].style.getPropertyValue("--longest-subscribed-channel-name-width"), "3px");
});
diff --git a/web/tests/upload.test.cjs b/web/tests/upload.test.cjs
index 764c641fcf4..09b986dbf20 100644
--- a/web/tests/upload.test.cjs
+++ b/web/tests/upload.test.cjs
@@ -228,10 +228,6 @@ test("upload_files", async ({mock_template, override, override_rewire}) => {
markdown_preview_hide_button_clicked = true;
});
$("#compose-send-button").removeClass("disabled-message-send-controls");
- $("#compose .undo_markdown_preview").css = (property) => {
- assert.equal(property, "display");
- return "flex";
- };
banner_shown = false;
mock_template("compose_banner/upload_banner.hbs", false, () => {
diff --git a/web/tests/user_search.test.cjs b/web/tests/user_search.test.cjs
index 9957ff882c9..133c65cb069 100644
--- a/web/tests/user_search.test.cjs
+++ b/web/tests/user_search.test.cjs
@@ -99,7 +99,6 @@ test("clear_search with button", ({override}) => {
override(presence, "get_status", () => "active");
override(presence, "get_user_ids", () => all_user_ids);
override(popovers, "hide_all", noop);
- $("#buddy-list-loading-subscribers").css = noop;
stub_buddy_list_empty_list_message_lengths();
@@ -120,7 +119,6 @@ test("clear_search with button", ({override}) => {
test("clear_search", ({override}) => {
override(realm, "realm_presence_disabled", true);
- $("#buddy-list-loading-subscribers").css = noop;
override(popovers, "hide_all", noop);
stub_buddy_list_empty_list_message_lengths();
@@ -141,7 +139,6 @@ test("fetch on search", async ({override}) => {
override(fake_buddy_list, "populate", () => {
populate_call_count += 1;
});
- $("#buddy-list-loading-subscribers").css = noop;
override(popovers, "hide_all", noop);
stub_buddy_list_empty_list_message_lengths();