zjquery: Avoid array-related hacks.

Callers can either explicitly pass in children,
stub out $(...)[0] as needed, or just
circumvent jQuery complications with override.

Note the reactions test was broken before,
since $(...)[0] was always returning the same
stub.
This commit is contained in:
Steve Howell 2021-02-22 16:01:29 +00:00 committed by Steve Howell
parent 2b8921bf67
commit 42c2c9fb2d
8 changed files with 30 additions and 12 deletions

View File

@ -203,6 +203,7 @@ run_test("hotspots", (override) => {
}); });
run_test("invites_changed", (override) => { run_test("invites_changed", (override) => {
$.create("#admin-invites-list", {children: ["stub"]});
const event = event_fixtures.invites_changed; const event = event_fixtures.invites_changed;
const stub = make_stub(); const stub = make_stub();
override(settings_invites, "set_up", stub.f); override(settings_invites, "set_up", stub.f);

View File

@ -301,6 +301,7 @@ run_test("format_drafts", (override) => {
override(drafts, "open_overlay", noop); override(drafts, "open_overlay", noop);
drafts.set_initial_element = noop; drafts.set_initial_element = noop;
$.create("#drafts_table .draft-row", {children: []});
drafts.launch(); drafts.launch();
timerender.render_now = stub_render_now; timerender.render_now = stub_render_now;
}); });

View File

@ -22,7 +22,7 @@ set_global("popovers", {
rows.is_draft_row = () => false; rows.is_draft_row = () => false;
run_test("pan_and_zoom", () => { run_test("pan_and_zoom", (override) => {
$.clear_all_elements(); $.clear_all_elements();
const img = $.create("img-stub"); const img = $.create("img-stub");
@ -33,7 +33,12 @@ run_test("pan_and_zoom", () => {
img.set_parent(link); img.set_parent(link);
link.closest = () => msg; link.closest = () => msg;
msg.attr("zid", "1234");
override(rows, "id", (row) => {
assert.equal(row, msg);
return 1234;
});
img.attr("src", "example"); img.attr("src", "example");
let fetched_zid; let fetched_zid;
@ -51,7 +56,7 @@ run_test("pan_and_zoom", () => {
assert.equal(fetched_zid, 1234); assert.equal(fetched_zid, 1234);
}); });
run_test("youtube", () => { run_test("youtube", (override) => {
$.clear_all_elements(); $.clear_all_elements();
const href = "https://youtube.com/some-random-clip"; const href = "https://youtube.com/some-random-clip";
@ -59,7 +64,10 @@ run_test("youtube", () => {
const link = $.create("link-stub"); const link = $.create("link-stub");
const msg = $.create("msg-stub"); const msg = $.create("msg-stub");
msg.attr("zid", "4321"); override(rows, "id", (row) => {
assert.equal(row, msg);
return 4321;
});
$(img).attr("src", href); $(img).attr("src", href);

View File

@ -127,14 +127,14 @@ set_global("current_msg_list", {
}); });
run_test("open_reactions_popover", () => { run_test("open_reactions_popover", () => {
$(".selected-row").set_find_results(".actions_hover", $(".target-action")); $(".selected-row").set_find_results(".actions_hover", ["action-stub"]);
$(".selected-row").set_find_results(".reaction_button", $(".target-reaction")); $(".selected-row").set_find_results(".reaction_button", ["reaction-stub"]);
let called = false; let called = false;
emoji_picker.toggle_emoji_popover = function (target, id) { emoji_picker.toggle_emoji_popover = function (target, id) {
called = true; called = true;
assert.equal(id, 42); assert.equal(id, 42);
assert.equal(target, $(".target-reaction")[0]); assert.equal(target, "action-stub");
}; };
assert(reactions.open_reactions_popover()); assert(reactions.open_reactions_popover());
@ -148,7 +148,7 @@ run_test("open_reactions_popover", () => {
emoji_picker.toggle_emoji_popover = function (target, id) { emoji_picker.toggle_emoji_popover = function (target, id) {
called = true; called = true;
assert.equal(id, 42); assert.equal(id, 42);
assert.equal(target, $(".target-action")[0]); assert.equal(target, "reaction-stub");
}; };
assert(reactions.open_reactions_popover()); assert(reactions.open_reactions_popover());

View File

@ -84,6 +84,8 @@ run_test("initialize", () => {
const search_button = $(".search_button"); const search_button = $(".search_button");
const searchbox = $("#searchbox"); const searchbox = $("#searchbox");
search_query_box[0] = "stub";
search_pill.get_search_string_for_current_filter = function () { search_pill.get_search_string_for_current_filter = function () {
return "is:starred"; return "is:starred";
}; };

View File

@ -52,6 +52,8 @@ function test_populate(opts) {
table.set_find_results("tr.profile-field-row", rows); table.set_find_results("tr.profile-field-row", rows);
table.set_find_results("tr.profile-field-form", form); table.set_find_results("tr.profile-field-form", form);
table[0] = "stub";
let num_appends = 0; let num_appends = 0;
table.append = () => { table.append = () => {
num_appends += 1; num_appends += 1;

View File

@ -187,5 +187,7 @@ run_test("initialize_everything", () => {
compose.compute_show_video_chat_button = () => {}; compose.compute_show_video_chat_button = () => {};
$("#below-compose-content .video_link").toggle = () => {}; $("#below-compose-content .video_link").toggle = () => {};
$("<audio>")[0] = "stub";
ui_init.initialize_everything(); ui_init.initialize_everything();
}); });

View File

@ -413,18 +413,20 @@ function make_new_elem(selector, opts) {
f.call(child); f.call(child);
} }
}; };
self[Symbol.iterator] = function* () {
for (const child of opts.children) {
yield child;
}
};
self.length = opts.children.length;
} }
if (selector[0] === "<") { if (selector[0] === "<") {
self.html(selector); self.html(selector);
} }
self[0] = "you-must-set-the-child-yourself";
self.selector = selector; self.selector = selector;
self.length = 1;
return self; return self;
} }