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: Support eq() and index().

Aman Agrawale7edda252c81
diff --git a/web/tests/lib/zjquery_element.cjs b/web/tests/lib/zjquery_element.cjs
index fedf2de7b07..9f3ba37e16c 100644
--- a/web/tests/lib/zjquery_element.cjs
+++ b/web/tests/lib/zjquery_element.cjs
@@ -410,6 +410,10 @@ exports.FakeJQuery = class extends RejectMissing {
}
return this;
}
+ eq(index) {
+ const element = [...this].at(index);
+ return new exports.FakeJQuery(element === undefined ? [] : [element]);
+ }
expectOne() {
// silently do nothing
return this;
@@ -508,6 +512,17 @@ exports.FakeJQuery = class extends RejectMissing {
}
return this;
}
+ index(...args) {
+ assert.equal(args.length, 0, "zjquery does not support this index() call");
+ assert.equal(this.length, 1);
+ if (!Object.hasOwn(this[0], "parentNode")) {
+ const state = fake_element_state.get(this[0]);
+ throw new Error(
+ `You need to call $(${JSON.stringify(state.selector)}).set_parent(...)`,
+ );
+ }
+ return [...this[0].parentNode.children].indexOf(this[0]);
+ }
insertAfter(...args) {
args = dom_args(args);
assert.equal(args.length, 1);
diff --git a/web/tests/zjquery.test.cjs b/web/tests/zjquery.test.cjs
index 0120703bbac..805ce31f6db 100644
--- a/web/tests/zjquery.test.cjs
+++ b/web/tests/zjquery.test.cjs
@@ -109,6 +109,21 @@ run_test("finding_related_objects", () => {
$elem.set_parents_result(".folder", $my_parents);
$elem.parents(".folder").addClass("active");
assert.ok($my_parents.hasClass("active"));
+
+ /*
+ Positions work the same way. eq() picks an element of a result
+ by its position there, and index() gives an element's position
+ among the children of its parent, both of which you set up.
+ */
+ const $list = $("#todo-list");
+ const $first_item = $("#todo-1");
+ const $second_item = $("#todo-2");
+ $list.set_children([...$first_item, ...$second_item]);
+ $second_item.set_parent($list);
+
+ assert.equal($list.children().eq(1)[0], $second_item[0]);
+ assert.equal($list.children().eq(2).length, 0);
+ assert.equal($second_item.index(), 1);
});
run_test("clicks", () => {