2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2023-02-22 23:04:10 +01:00
|
|
|
const {zrequire} = require("./lib/namespace");
|
|
|
|
const {run_test} = require("./lib/test");
|
|
|
|
const blueslip = require("./lib/zblueslip");
|
|
|
|
const $ = require("./lib/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2021-02-24 05:00:20 +01:00
|
|
|
const {ListCursor} = zrequire("list_cursor");
|
2018-08-24 14:39:22 +02:00
|
|
|
|
2021-05-18 18:51:01 +02:00
|
|
|
function basic_conf({first_key, prev_key, next_key}) {
|
2018-08-24 14:39:22 +02:00
|
|
|
const list = {
|
2020-07-15 01:29:15 +02:00
|
|
|
scroll_container_sel: "whatever",
|
2022-11-17 23:33:43 +01:00
|
|
|
find_li() {},
|
2021-05-18 18:51:01 +02:00
|
|
|
first_key,
|
|
|
|
prev_key,
|
|
|
|
next_key,
|
2018-08-24 14:39:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const conf = {
|
2020-07-20 22:18:43 +02:00
|
|
|
list,
|
2020-07-15 01:29:15 +02:00
|
|
|
highlight_class: "highlight",
|
2018-08-24 14:39:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("misc errors", ({override}) => {
|
2021-05-18 18:51:01 +02:00
|
|
|
const conf = basic_conf({
|
|
|
|
first_key: () => undefined,
|
2022-04-09 23:44:38 +02:00
|
|
|
prev_key: /* istanbul ignore next */ () => undefined,
|
|
|
|
next_key: /* istanbul ignore next */ () => undefined,
|
2021-05-18 18:51:01 +02:00
|
|
|
});
|
2018-08-24 14:39:22 +02:00
|
|
|
|
2020-07-23 01:48:16 +02:00
|
|
|
const cursor = new ListCursor(conf);
|
2018-08-24 14:39:22 +02:00
|
|
|
|
|
|
|
// Test that we just ignore empty
|
|
|
|
// lists for unknown keys.
|
2021-05-18 18:51:01 +02:00
|
|
|
override(conf.list, "find_li", ({key, force_render}) => {
|
|
|
|
assert.equal(key, "nada");
|
|
|
|
assert.equal(force_render, true);
|
2018-08-24 14:39:22 +02:00
|
|
|
return [];
|
2021-05-18 18:51:01 +02:00
|
|
|
});
|
2018-08-24 14:39:22 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
cursor.get_row("nada");
|
2018-08-24 14:39:22 +02:00
|
|
|
|
2020-07-23 01:48:16 +02:00
|
|
|
blueslip.expect("error", "Caller is not checking keys for ListCursor.go_to");
|
2018-08-24 14:39:22 +02:00
|
|
|
cursor.go_to(undefined);
|
|
|
|
|
2023-04-24 15:57:45 +02:00
|
|
|
blueslip.expect("error", "Cannot highlight key for ListCursor");
|
2020-07-15 01:29:15 +02:00
|
|
|
cursor.go_to("nada");
|
2018-08-24 14:39:22 +02:00
|
|
|
|
|
|
|
cursor.prev();
|
|
|
|
cursor.next();
|
|
|
|
});
|
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("single item list", ({override}) => {
|
2021-05-18 18:51:01 +02:00
|
|
|
const valid_key = "42";
|
|
|
|
|
|
|
|
const conf = basic_conf({
|
2022-04-09 23:44:38 +02:00
|
|
|
first_key: /* istanbul ignore next */ () => valid_key,
|
2021-05-18 18:51:01 +02:00
|
|
|
next_key: () => undefined,
|
|
|
|
prev_key: () => undefined,
|
|
|
|
});
|
2020-07-23 01:48:16 +02:00
|
|
|
const cursor = new ListCursor(conf);
|
2018-08-24 14:39:22 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $li_stub = {
|
2018-08-24 14:39:22 +02:00
|
|
|
length: 1,
|
2022-11-17 23:33:43 +01:00
|
|
|
addClass() {},
|
2018-08-24 14:39:22 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
override(conf.list, "find_li", () => $li_stub);
|
2021-05-18 18:51:01 +02:00
|
|
|
override(cursor, "adjust_scroll", () => {});
|
2018-08-24 14:39:22 +02:00
|
|
|
|
|
|
|
cursor.go_to(valid_key);
|
|
|
|
|
|
|
|
// Test prev/next, which should just silently do nothing.
|
|
|
|
cursor.prev();
|
|
|
|
cursor.next();
|
|
|
|
|
|
|
|
// The next line is also a noop designed to just give us test
|
|
|
|
// coverage.
|
|
|
|
cursor.go_to(valid_key);
|
|
|
|
});
|
2021-05-18 18:55:02 +02:00
|
|
|
|
2021-06-16 14:38:37 +02:00
|
|
|
run_test("multiple item list", ({override}) => {
|
2021-05-18 18:55:02 +02:00
|
|
|
const conf = basic_conf({
|
2022-04-09 23:44:38 +02:00
|
|
|
first_key: /* istanbul ignore next */ () => 1,
|
2021-05-18 18:55:02 +02:00
|
|
|
next_key: (key) => (key < 3 ? key + 1 : undefined),
|
|
|
|
prev_key: (key) => (key > 1 ? key - 1 : undefined),
|
|
|
|
});
|
|
|
|
const cursor = new ListCursor(conf);
|
|
|
|
override(cursor, "adjust_scroll", () => {});
|
|
|
|
|
|
|
|
function li(key) {
|
|
|
|
return $.create(`item-${key}`, {children: ["stub"]});
|
|
|
|
}
|
|
|
|
|
|
|
|
const list_items = {
|
|
|
|
1: li(1),
|
|
|
|
2: li(2),
|
|
|
|
3: li(3),
|
|
|
|
};
|
|
|
|
|
|
|
|
override(conf.list, "find_li", ({key}) => list_items[key]);
|
|
|
|
|
|
|
|
cursor.go_to(2);
|
|
|
|
assert.equal(cursor.get_key(), 2);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!list_items[1].hasClass("highlight"));
|
|
|
|
assert.ok(list_items[2].hasClass("highlight"));
|
|
|
|
assert.ok(!list_items[3].hasClass("highlight"));
|
2021-05-18 18:55:02 +02:00
|
|
|
|
|
|
|
cursor.next();
|
|
|
|
cursor.next();
|
|
|
|
cursor.next();
|
|
|
|
|
|
|
|
assert.equal(cursor.get_key(), 3);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!list_items[1].hasClass("highlight"));
|
|
|
|
assert.ok(!list_items[2].hasClass("highlight"));
|
|
|
|
assert.ok(list_items[3].hasClass("highlight"));
|
2021-05-18 18:55:02 +02:00
|
|
|
|
|
|
|
cursor.prev();
|
|
|
|
cursor.prev();
|
|
|
|
cursor.prev();
|
|
|
|
|
|
|
|
assert.equal(cursor.get_key(), 1);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(list_items[1].hasClass("highlight"));
|
|
|
|
assert.ok(!list_items[2].hasClass("highlight"));
|
|
|
|
assert.ok(!list_items[3].hasClass("highlight"));
|
2021-05-18 18:55:02 +02:00
|
|
|
|
|
|
|
cursor.clear();
|
|
|
|
assert.equal(cursor.get_key(), undefined);
|
|
|
|
cursor.redraw();
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!list_items[1].hasClass("highlight"));
|
2021-05-18 18:55:02 +02:00
|
|
|
});
|