2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2022-04-19 02:45:44 +02:00
|
|
|
const {mock_esm, mock_jquery, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-03-16 23:38:59 +01:00
|
|
|
const blueslip = require("../zjsunit/zblueslip");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2018-04-17 21:29:21 +02:00
|
|
|
// We need these stubs to get by instanceof checks.
|
2021-01-29 10:27:56 +01:00
|
|
|
// The ListWidget library allows you to insert objects
|
2018-04-17 21:29:21 +02:00
|
|
|
// that are either jQuery, Element, or just raw HTML
|
|
|
|
// strings. We initially test with raw strings.
|
2021-03-10 06:10:32 +01:00
|
|
|
const ui = mock_esm("../../static/js/ui");
|
2021-02-28 21:33:10 +01:00
|
|
|
|
2018-04-17 21:29:21 +02:00
|
|
|
// We only need very simple jQuery wrappers for when the
|
2021-03-06 17:37:51 +01:00
|
|
|
|
2018-04-17 21:29:21 +02:00
|
|
|
// "real" code wraps html or sets up click handlers.
|
|
|
|
// We'll simulate most other objects ourselves.
|
2021-06-16 15:58:34 +02:00
|
|
|
mock_jquery((arg) => {
|
2018-04-17 21:29:21 +02:00
|
|
|
if (arg.to_jquery) {
|
|
|
|
return arg.to_jquery();
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-08-16 06:50:20 +02:00
|
|
|
addClass() {
|
|
|
|
return this;
|
|
|
|
},
|
2020-05-28 19:58:51 +02:00
|
|
|
replace: (regex, string) => {
|
|
|
|
arg = arg.replace(regex, string);
|
|
|
|
},
|
2018-04-17 21:29:21 +02:00
|
|
|
html: () => arg,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
const ListWidget = zrequire("list_widget");
|
|
|
|
|
2020-04-24 12:20:45 +02:00
|
|
|
// We build objects here that simulate jQuery containers.
|
|
|
|
// The main thing to do at first is simulate that our
|
|
|
|
// scroll container is the nearest ancestor to our main
|
|
|
|
// container that has a max-height attribute, and then
|
|
|
|
// the scroll container will have a scroll event attached to
|
|
|
|
// it. This is a good time to read set_up_event_handlers
|
|
|
|
// in the real code.
|
|
|
|
|
|
|
|
function make_container() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = {};
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2020-04-24 12:20:45 +02:00
|
|
|
// Make our append function just set a field we can
|
|
|
|
// check in our tests.
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.append = ($data) => {
|
|
|
|
$container.$appended_data = $data;
|
2020-04-24 12:20:45 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
return $container;
|
2020-04-24 12:20:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-04 10:07:43 +02:00
|
|
|
function make_scroll_container() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $scroll_container = {};
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$scroll_container.cleared = false;
|
2020-04-24 13:22:32 +02:00
|
|
|
|
2018-04-17 21:29:21 +02:00
|
|
|
// Capture the scroll callback so we can call it in
|
|
|
|
// our tests.
|
2022-01-25 11:36:19 +01:00
|
|
|
$scroll_container.on = (ev, f) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(ev, "scroll.list_widget_container");
|
2022-01-25 11:36:19 +01:00
|
|
|
$scroll_container.call_scroll = () => {
|
|
|
|
f.call($scroll_container);
|
2018-04-17 21:29:21 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$scroll_container.off = (ev) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(ev, "scroll.list_widget_container");
|
2022-01-25 11:36:19 +01:00
|
|
|
$scroll_container.cleared = true;
|
2020-04-24 13:22:32 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
return $scroll_container;
|
2018-04-17 21:29:21 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 12:30:56 +02:00
|
|
|
function make_sort_container() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $sort_container = {};
|
2020-04-24 12:30:56 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$sort_container.cleared = false;
|
2020-04-24 13:22:32 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$sort_container.on = (ev, sel, f) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(ev, "click.list_widget_sort");
|
|
|
|
assert.equal(sel, "[data-sort]");
|
2022-01-25 11:36:19 +01:00
|
|
|
$sort_container.f = f;
|
2020-04-24 12:30:56 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$sort_container.off = (ev) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(ev, "click.list_widget_sort");
|
2022-01-25 11:36:19 +01:00
|
|
|
$sort_container.cleared = true;
|
2020-04-24 13:22:32 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
return $sort_container;
|
2020-04-24 12:30:56 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 13:59:33 +02:00
|
|
|
function make_filter_element() {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $element = {};
|
2020-04-24 13:59:33 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$element.cleared = false;
|
2020-04-24 13:22:32 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$element.on = (ev, f) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(ev, "input.list_widget_filter");
|
2022-01-25 11:36:19 +01:00
|
|
|
$element.f = f;
|
2020-04-24 13:59:33 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$element.off = (ev) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(ev, "input.list_widget_filter");
|
2022-01-25 11:36:19 +01:00
|
|
|
$element.cleared = true;
|
2020-04-24 13:22:32 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
return $element;
|
2020-04-24 13:59:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-17 21:29:21 +02:00
|
|
|
function make_search_input() {
|
|
|
|
const $element = {};
|
|
|
|
|
|
|
|
// Allow ourselves to be wrapped by $(...) and
|
|
|
|
// return ourselves.
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2018-04-17 21:29:21 +02:00
|
|
|
$element.to_jquery = () => $element;
|
|
|
|
|
|
|
|
$element.on = (event_name, f) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(event_name, "input.list_widget_filter");
|
2018-04-17 21:29:21 +02:00
|
|
|
$element.simulate_input_event = () => {
|
|
|
|
const elem = {
|
|
|
|
value: $element.val(),
|
|
|
|
};
|
|
|
|
f.call(elem);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
return $element;
|
|
|
|
}
|
|
|
|
|
|
|
|
function div(item) {
|
2020-07-15 01:29:15 +02:00
|
|
|
return "<div>" + item + "</div>";
|
2018-04-17 21:29:21 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("scrolling", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2020-04-12 19:45:33 +02:00
|
|
|
const items = [];
|
|
|
|
|
2020-07-04 10:07:43 +02:00
|
|
|
let get_scroll_element_called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
ui.get_scroll_element = ($element) => {
|
2020-07-04 10:07:43 +02:00
|
|
|
get_scroll_element_called = true;
|
2022-01-25 11:36:19 +01:00
|
|
|
return $element;
|
2020-07-04 10:07:43 +02:00
|
|
|
};
|
|
|
|
|
2020-04-12 19:45:33 +02:00
|
|
|
for (let i = 0; i < 200; i += 1) {
|
2020-07-15 01:29:15 +02:00
|
|
|
items.push("item " + i);
|
2020-04-12 19:45:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
modifier: (item) => item,
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-04-12 19:45:33 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html = (html) => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(html, "");
|
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, items, opts);
|
2020-04-12 19:45:33 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), items.slice(0, 80).join(""));
|
2020-07-04 10:07:43 +02:00
|
|
|
assert.equal(get_scroll_element_called, true);
|
2020-04-12 19:45:33 +02:00
|
|
|
|
|
|
|
// Set up our fake geometry so it forces a scroll action.
|
2022-01-25 11:36:19 +01:00
|
|
|
$scroll_container.scrollTop = 180;
|
|
|
|
$scroll_container.clientHeight = 100;
|
|
|
|
$scroll_container.scrollHeight = 260;
|
2020-04-12 19:45:33 +02:00
|
|
|
|
|
|
|
// Scrolling gets the next two elements from the list into
|
|
|
|
// our widget.
|
2022-01-25 11:36:19 +01:00
|
|
|
$scroll_container.call_scroll();
|
|
|
|
assert.deepEqual($container.$appended_data.html(), items.slice(80, 100).join(""));
|
2020-04-12 19:45:33 +02:00
|
|
|
});
|
|
|
|
|
2021-05-06 19:06:42 +02:00
|
|
|
run_test("not_scrolling", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
2021-05-06 19:06:42 +02:00
|
|
|
|
|
|
|
const items = [];
|
|
|
|
|
|
|
|
let get_scroll_element_called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
ui.get_scroll_element = ($element) => {
|
2021-05-06 19:06:42 +02:00
|
|
|
get_scroll_element_called = true;
|
2022-01-25 11:36:19 +01:00
|
|
|
return $element;
|
2021-05-06 19:06:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let post_scroll__pre_render_callback_called = false;
|
|
|
|
const post_scroll__pre_render_callback = () => {
|
|
|
|
post_scroll__pre_render_callback_called = true;
|
|
|
|
};
|
|
|
|
|
2021-05-06 19:07:03 +02:00
|
|
|
let get_min_load_count_called = false;
|
|
|
|
const get_min_load_count = (offset, load_count) => {
|
|
|
|
get_min_load_count_called = true;
|
|
|
|
return load_count;
|
|
|
|
};
|
|
|
|
|
2021-05-06 19:06:42 +02:00
|
|
|
for (let i = 0; i < 200; i += 1) {
|
|
|
|
items.push("item " + i);
|
|
|
|
}
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
modifier: (item) => item,
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2021-05-06 19:06:42 +02:00
|
|
|
is_scroll_position_for_render: () => false,
|
|
|
|
post_scroll__pre_render_callback,
|
2021-05-06 19:07:03 +02:00
|
|
|
get_min_load_count,
|
2021-05-06 19:06:42 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html = (html) => {
|
2021-05-06 19:06:42 +02:00
|
|
|
assert.equal(html, "");
|
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, items, opts);
|
2021-05-06 19:06:42 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), items.slice(0, 80).join(""));
|
2021-05-06 19:06:42 +02:00
|
|
|
assert.equal(get_scroll_element_called, true);
|
|
|
|
|
|
|
|
// Set up our fake geometry.
|
2022-01-25 11:36:19 +01:00
|
|
|
$scroll_container.scrollTop = 180;
|
|
|
|
$scroll_container.clientHeight = 100;
|
|
|
|
$scroll_container.scrollHeight = 260;
|
2021-05-06 19:06:42 +02:00
|
|
|
|
|
|
|
// Since `should_render` is always false, no elements will be
|
|
|
|
// added regardless of scrolling.
|
2022-01-25 11:36:19 +01:00
|
|
|
$scroll_container.call_scroll();
|
|
|
|
// $appended_data remains the same.
|
|
|
|
assert.deepEqual($container.$appended_data.html(), items.slice(0, 80).join(""));
|
2021-05-06 19:06:42 +02:00
|
|
|
assert.equal(post_scroll__pre_render_callback_called, true);
|
2021-05-06 19:07:03 +02:00
|
|
|
assert.equal(get_min_load_count_called, true);
|
2021-05-06 19:06:42 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("filtering", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
2020-04-12 19:45:33 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $search_input = make_search_input();
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
const list = ["apple", "banana", "carrot", "dog", "egg", "fence", "grape"];
|
2020-04-24 22:16:39 +02:00
|
|
|
const opts = {
|
2018-04-17 21:29:21 +02:00
|
|
|
filter: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $search_input,
|
2020-07-02 01:41:40 +02:00
|
|
|
predicate: (item, value) => item.includes(value),
|
2018-04-17 21:29:21 +02:00
|
|
|
},
|
|
|
|
modifier: (item) => div(item),
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2018-04-17 21:29:21 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html = (html) => {
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.equal(html, "");
|
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
const widget = ListWidget.create($container, list, opts);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2020-04-12 19:45:33 +02:00
|
|
|
let expected_html =
|
2020-07-15 01:29:15 +02:00
|
|
|
"<div>apple</div>" +
|
|
|
|
"<div>banana</div>" +
|
|
|
|
"<div>carrot</div>" +
|
|
|
|
"<div>dog</div>" +
|
|
|
|
"<div>egg</div>" +
|
|
|
|
"<div>fence</div>" +
|
|
|
|
"<div>grape</div>";
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), expected_html);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
// Filtering will pick out dog/egg/grape when we put "g"
|
|
|
|
// into our search input. (This uses the default filter, which
|
|
|
|
// is a glorified indexOf call.)
|
2022-01-25 11:36:19 +01:00
|
|
|
$search_input.val = () => "g";
|
|
|
|
$search_input.simulate_input_event();
|
2021-04-22 23:40:09 +02:00
|
|
|
assert.deepEqual(widget.get_current_list(), ["dog", "egg", "grape"]);
|
2020-07-15 01:29:15 +02:00
|
|
|
expected_html = "<div>dog</div><div>egg</div><div>grape</div>";
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), expected_html);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
// We can insert new data into the widget.
|
2020-07-15 00:34:28 +02:00
|
|
|
const new_data = ["greta", "faye", "gary", "frank", "giraffe", "fox"];
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2020-04-15 01:29:34 +02:00
|
|
|
widget.replace_list_data(new_data);
|
2020-10-07 13:17:55 +02:00
|
|
|
expected_html = "<div>greta</div><div>gary</div><div>giraffe</div>";
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), expected_html);
|
2020-04-24 22:16:39 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("no filtering", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
|
|
|
$container.html = () => {};
|
2020-03-10 19:48:03 +01:00
|
|
|
|
2020-09-23 09:37:29 +02:00
|
|
|
let callback_called = false;
|
2020-03-10 19:48:03 +01:00
|
|
|
// Opts does not require a filter key.
|
2020-04-24 22:16:39 +02:00
|
|
|
const opts = {
|
2020-03-10 19:48:03 +01:00
|
|
|
modifier: (item) => div(item),
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-09-23 09:37:29 +02:00
|
|
|
callback_after_render: () => {
|
|
|
|
callback_called = true;
|
|
|
|
},
|
2020-03-10 19:48:03 +01:00
|
|
|
};
|
2022-01-25 11:36:19 +01:00
|
|
|
const widget = ListWidget.create($container, ["apple", "banana"], opts);
|
2020-03-10 19:48:03 +01:00
|
|
|
widget.render();
|
2020-09-23 09:37:29 +02:00
|
|
|
assert.deepEqual(callback_called, true);
|
2020-03-10 19:48:03 +01:00
|
|
|
|
2020-10-07 13:17:55 +02:00
|
|
|
const expected_html = "<div>apple</div><div>banana</div>";
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), expected_html);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
function sort_button(opts) {
|
|
|
|
// The complications here are due to needing to find
|
|
|
|
// the list via complicated HTML assumptions. Also, we
|
|
|
|
// don't have any abstraction for the button and its
|
|
|
|
// siblings other than direct jQuery actions.
|
|
|
|
|
|
|
|
function data(sel) {
|
|
|
|
switch (sel) {
|
2020-07-15 00:34:28 +02:00
|
|
|
case "sort":
|
|
|
|
return opts.sort_type;
|
|
|
|
case "sort-prop":
|
|
|
|
return opts.prop_name;
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2020-07-15 00:34:28 +02:00
|
|
|
default:
|
2020-10-07 09:58:04 +02:00
|
|
|
throw new Error("unknown selector: " + sel);
|
2018-04-17 21:29:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function lookup(sel, value) {
|
|
|
|
return (selector) => {
|
|
|
|
assert.equal(sel, selector);
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-04-24 16:13:42 +02:00
|
|
|
const classList = new Set();
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $button = {
|
2020-07-20 22:18:43 +02:00
|
|
|
data,
|
2020-07-15 01:29:15 +02:00
|
|
|
closest: lookup(".progressive-table-wrapper", {
|
2021-01-29 10:27:56 +01:00
|
|
|
data: lookup("list-widget", opts.list_name),
|
2018-04-17 21:29:21 +02:00
|
|
|
}),
|
2020-04-24 16:13:42 +02:00
|
|
|
addClass: (cls) => {
|
|
|
|
classList.add(cls);
|
|
|
|
},
|
2020-07-02 01:41:40 +02:00
|
|
|
hasClass: (cls) => classList.has(cls),
|
2020-04-24 16:13:42 +02:00
|
|
|
removeClass: (cls) => {
|
|
|
|
classList.delete(cls);
|
2020-04-13 16:13:06 +02:00
|
|
|
},
|
2020-07-15 01:29:15 +02:00
|
|
|
siblings: lookup(".active", {
|
2020-04-24 16:13:42 +02:00
|
|
|
removeClass: (cls) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(cls, "active");
|
2022-01-25 11:36:19 +01:00
|
|
|
$button.siblings_deactivated = true;
|
2018-04-17 21:29:21 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
siblings_deactivated: false,
|
2022-01-25 11:36:19 +01:00
|
|
|
to_jquery: () => $button,
|
2018-04-17 21:29:21 +02:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
return $button;
|
2018-04-17 21:29:21 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("wire up filter element", () => {
|
2020-07-15 00:34:28 +02:00
|
|
|
const lst = ["alice", "JESSE", "moses", "scott", "Sean", "Xavier"];
|
2019-12-30 16:13:42 +01:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
|
|
|
const $filter_element = make_filter_element();
|
2020-04-24 13:59:33 +02:00
|
|
|
|
|
|
|
// We don't care about what gets drawn initially.
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html = () => {};
|
2020-04-24 13:59:33 +02:00
|
|
|
|
2019-12-30 16:13:42 +01:00
|
|
|
const opts = {
|
|
|
|
filter: {
|
2020-07-02 01:41:40 +02:00
|
|
|
filterer: (list, value) => list.filter((item) => item.toLowerCase().includes(value)),
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $filter_element,
|
2019-12-30 16:13:42 +01:00
|
|
|
},
|
2020-07-15 01:29:15 +02:00
|
|
|
modifier: (s) => "(" + s + ")",
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2019-12-30 16:13:42 +01:00
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, lst, opts);
|
|
|
|
$filter_element.f.apply({value: "se"});
|
|
|
|
assert.equal($container.$appended_data.html(), "(JESSE)(moses)(Sean)");
|
2019-12-30 16:13:42 +01:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("sorting", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
|
|
|
const $sort_container = make_sort_container();
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let cleared;
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html = (html) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(html, "");
|
2018-04-17 21:29:21 +02:00
|
|
|
cleared = true;
|
|
|
|
};
|
|
|
|
|
2020-07-16 22:40:18 +02:00
|
|
|
const alice = {name: "alice", salary: 50};
|
|
|
|
const bob = {name: "Bob", salary: 40};
|
|
|
|
const cal = {name: "cal", salary: 30};
|
|
|
|
const dave = {name: "dave", salary: 25};
|
|
|
|
const ellen = {name: "ellen", salary: 95};
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2020-04-24 16:13:42 +02:00
|
|
|
const list = [bob, ellen, dave, alice, cal];
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
const opts = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "sorting-list",
|
2022-01-25 11:36:19 +01:00
|
|
|
$parent_container: $sort_container,
|
2020-07-02 01:41:40 +02:00
|
|
|
modifier: (item) => div(item.name) + div(item.salary),
|
2019-12-30 16:13:42 +01:00
|
|
|
filter: {
|
2019-12-30 17:44:24 +01:00
|
|
|
predicate: () => true,
|
2019-12-30 16:13:42 +01:00
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2018-04-17 21:29:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function html_for(people) {
|
2021-01-23 02:36:54 +01:00
|
|
|
return people.map((item) => opts.modifier(item)).join("");
|
2018-04-17 21:29:21 +02:00
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list, opts);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let button_opts;
|
2022-01-25 11:36:19 +01:00
|
|
|
let $button;
|
2019-11-02 00:06:25 +01:00
|
|
|
let expected_html;
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
button_opts = {
|
2020-07-15 01:29:15 +02:00
|
|
|
sort_type: "alphabetic",
|
|
|
|
prop_name: "name",
|
|
|
|
list_name: "my-list",
|
2018-04-17 21:29:21 +02:00
|
|
|
active: false,
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$button = sort_button(button_opts);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$sort_container.f.apply($button);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(cleared);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($button.siblings_deactivated);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
expected_html = html_for([alice, bob, cal, dave, ellen]);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), expected_html);
|
2020-04-24 16:13:42 +02:00
|
|
|
|
|
|
|
// Hit same button again to reverse the data.
|
|
|
|
cleared = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
$sort_container.f.apply($button);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(cleared);
|
2020-07-15 00:34:28 +02:00
|
|
|
expected_html = html_for([ellen, dave, cal, bob, alice]);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), expected_html);
|
|
|
|
assert.ok($button.hasClass("descend"));
|
2020-04-24 16:13:42 +02:00
|
|
|
|
|
|
|
// And then hit a third time to go back to the forward sort.
|
|
|
|
cleared = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
$sort_container.f.apply($button);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(cleared);
|
2020-07-15 00:34:28 +02:00
|
|
|
expected_html = html_for([alice, bob, cal, dave, ellen]);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), expected_html);
|
|
|
|
assert.ok(!$button.hasClass("descend"));
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
// Now try a numeric sort.
|
|
|
|
button_opts = {
|
2020-07-15 01:29:15 +02:00
|
|
|
sort_type: "numeric",
|
|
|
|
prop_name: "salary",
|
|
|
|
list_name: "my-list",
|
2018-04-17 21:29:21 +02:00
|
|
|
active: false,
|
|
|
|
};
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$button = sort_button(button_opts);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
|
|
|
cleared = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
$button.siblings_deactivated = false;
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$sort_container.f.apply($button);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(cleared);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($button.siblings_deactivated);
|
2018-04-17 21:29:21 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
expected_html = html_for([dave, cal, bob, alice, ellen]);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), expected_html);
|
2020-04-24 16:13:42 +02:00
|
|
|
|
|
|
|
// Hit same button again to reverse the numeric sort.
|
|
|
|
cleared = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
$sort_container.f.apply($button);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(cleared);
|
2020-07-15 00:34:28 +02:00
|
|
|
expected_html = html_for([ellen, alice, bob, cal, dave]);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), expected_html);
|
|
|
|
assert.ok($button.hasClass("descend"));
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2020-04-24 13:22:32 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("custom sort", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
|
|
|
$container.html = () => {};
|
2020-04-24 22:23:35 +02:00
|
|
|
|
|
|
|
const n42 = {x: 6, y: 7};
|
|
|
|
const n43 = {x: 1, y: 43};
|
|
|
|
const n44 = {x: 4, y: 11};
|
|
|
|
|
|
|
|
const list = [n42, n43, n44];
|
|
|
|
|
|
|
|
function sort_by_x(a, b) {
|
|
|
|
return a.x - b.x;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sort_by_product(a, b) {
|
|
|
|
return a.x * a.y - b.x * b.y;
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list, {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "custom-sort-list",
|
2020-07-16 23:29:01 +02:00
|
|
|
modifier: (n) => "(" + n.x + ", " + n.y + ")",
|
2020-04-24 22:23:35 +02:00
|
|
|
sort_fields: {
|
|
|
|
product: sort_by_product,
|
|
|
|
x_value: sort_by_x,
|
|
|
|
},
|
2020-08-29 01:04:42 +02:00
|
|
|
init_sort: [sort_by_product],
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-04-24 22:23:35 +02:00
|
|
|
});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), "(6, 7)(1, 43)(4, 11)");
|
2020-04-24 22:23:35 +02:00
|
|
|
|
2021-01-29 10:27:56 +01:00
|
|
|
const widget = ListWidget.get("custom-sort-list");
|
2020-04-24 22:23:35 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.sort("x_value");
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), "(1, 43)(4, 11)(6, 7)");
|
2020-04-24 22:23:35 +02:00
|
|
|
|
|
|
|
// We can sort without registering the function, too.
|
|
|
|
function sort_by_y(a, b) {
|
|
|
|
return a.y - b.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.sort(sort_by_y);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), "(6, 7)(4, 11)(1, 43)");
|
2020-04-24 22:23:35 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("clear_event_handlers", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
|
|
|
const $sort_container = make_sort_container();
|
|
|
|
const $filter_element = make_filter_element();
|
2020-04-24 13:22:32 +02:00
|
|
|
|
|
|
|
// We don't care about actual data for this test.
|
|
|
|
const list = [];
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html = () => {};
|
2020-04-24 13:22:32 +02:00
|
|
|
|
|
|
|
const opts = {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "list-we-create-twice",
|
2022-01-25 11:36:19 +01:00
|
|
|
$parent_container: $sort_container,
|
2020-04-24 13:22:32 +02:00
|
|
|
modifier: () => {},
|
|
|
|
filter: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $filter_element,
|
2022-04-09 23:44:38 +02:00
|
|
|
predicate: /* istanbul ignore next */ () => true,
|
2020-04-24 13:22:32 +02:00
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-04-24 13:22:32 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Create it the first time.
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list, opts);
|
|
|
|
assert.equal($sort_container.cleared, false);
|
|
|
|
assert.equal($scroll_container.cleared, false);
|
|
|
|
assert.equal($filter_element.cleared, false);
|
2020-04-24 13:22:32 +02:00
|
|
|
|
|
|
|
// The second time we'll clear the old events.
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list, opts);
|
|
|
|
assert.equal($sort_container.cleared, true);
|
|
|
|
assert.equal($scroll_container.cleared, true);
|
|
|
|
assert.equal($filter_element.cleared, true);
|
2020-04-24 13:22:32 +02:00
|
|
|
});
|
2020-04-24 15:38:16 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("errors", () => {
|
2020-04-24 15:38:16 +02:00
|
|
|
// We don't care about actual data for this test.
|
2020-08-29 01:04:42 +02:00
|
|
|
const list = ["stub"];
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
2020-04-24 15:38:16 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Need opts to create widget.");
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list);
|
2020-04-24 15:38:16 +02:00
|
|
|
blueslip.reset();
|
2020-04-24 15:57:41 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
blueslip.expect("error", "$simplebar_container is missing.");
|
|
|
|
ListWidget.create($container, list, {
|
2020-07-15 01:29:15 +02:00
|
|
|
modifier: "hello world",
|
2020-07-04 10:07:43 +02:00
|
|
|
});
|
|
|
|
blueslip.reset();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "get_item should be a function");
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list, {
|
2020-07-15 01:29:15 +02:00
|
|
|
get_item: "not a function",
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-05-10 12:45:16 +02:00
|
|
|
});
|
|
|
|
blueslip.reset();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Filter predicate is not a function.");
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list, {
|
2020-04-24 15:57:41 +02:00
|
|
|
filter: {
|
2020-07-15 01:29:15 +02:00
|
|
|
predicate: "wrong type",
|
2020-04-24 15:57:41 +02:00
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-04-24 15:57:41 +02:00
|
|
|
});
|
|
|
|
blueslip.reset();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Filterer and predicate are mutually exclusive.");
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list, {
|
2020-04-24 15:57:41 +02:00
|
|
|
filter: {
|
2022-04-09 23:44:38 +02:00
|
|
|
filterer: /* istanbul ignore next */ () => true,
|
|
|
|
predicate: /* istanbul ignore next */ () => true,
|
2020-04-24 15:57:41 +02:00
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-04-24 15:57:41 +02:00
|
|
|
});
|
|
|
|
blueslip.reset();
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Filter filterer is not a function (or missing).");
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list, {
|
2020-07-15 00:34:28 +02:00
|
|
|
filter: {},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-04-24 15:57:41 +02:00
|
|
|
});
|
|
|
|
blueslip.reset();
|
2020-04-24 22:09:17 +02:00
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html = () => {};
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "List item is not a string: 999");
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list, {
|
2020-04-24 22:09:17 +02:00
|
|
|
modifier: () => 999,
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-04-24 22:09:17 +02:00
|
|
|
});
|
|
|
|
blueslip.reset();
|
2020-04-24 15:38:16 +02:00
|
|
|
});
|
2020-04-24 19:49:18 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("sort helpers", () => {
|
2020-04-24 19:49:18 +02:00
|
|
|
/*
|
|
|
|
We mostly test our sorting helpers using the
|
|
|
|
actual widget, but this test gets us a bit
|
|
|
|
more line coverage.
|
|
|
|
*/
|
2020-07-15 01:29:15 +02:00
|
|
|
const alice2 = {name: "alice", id: 2};
|
|
|
|
const alice10 = {name: "alice", id: 10};
|
|
|
|
const bob2 = {name: "bob", id: 2};
|
|
|
|
const bob10 = {name: "bob", id: 10};
|
2020-04-24 19:49:18 +02:00
|
|
|
|
2021-01-29 10:27:56 +01:00
|
|
|
const alpha_cmp = ListWidget.alphabetic_sort("name");
|
|
|
|
const num_cmp = ListWidget.numeric_sort("id");
|
2020-04-24 19:49:18 +02:00
|
|
|
|
|
|
|
assert.equal(alpha_cmp(alice2, alice10), 0);
|
|
|
|
assert.equal(alpha_cmp(alice2, bob2), -1);
|
|
|
|
assert.equal(alpha_cmp(bob2, alice10), 1);
|
|
|
|
assert.equal(num_cmp(alice2, bob2), 0);
|
|
|
|
assert.equal(num_cmp(alice2, bob10), -1);
|
|
|
|
assert.equal(num_cmp(alice10, bob2), 1);
|
|
|
|
});
|
2020-04-24 23:23:51 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("replace_list_data w/filter update", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
|
|
|
$container.html = () => {};
|
2020-04-24 23:23:51 +02:00
|
|
|
|
|
|
|
const list = [1, 2, 3, 4];
|
|
|
|
let num_updates = 0;
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list, {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "replace-list",
|
|
|
|
modifier: (n) => "(" + n.toString() + ")",
|
2020-04-24 23:23:51 +02:00
|
|
|
filter: {
|
|
|
|
predicate: (n) => n % 2 === 0,
|
|
|
|
onupdate: () => {
|
|
|
|
num_updates += 1;
|
|
|
|
},
|
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-04-24 23:23:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(num_updates, 0);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), "(2)(4)");
|
2020-04-24 23:23:51 +02:00
|
|
|
|
2021-01-29 10:27:56 +01:00
|
|
|
const widget = ListWidget.get("replace-list");
|
2020-04-24 23:23:51 +02:00
|
|
|
widget.replace_list_data([5, 6, 7, 8]);
|
|
|
|
|
|
|
|
assert.equal(num_updates, 1);
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.deepEqual($container.$appended_data.html(), "(6)(8)");
|
2020-04-24 23:23:51 +02:00
|
|
|
});
|
2020-05-10 12:45:16 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("opts.get_item", () => {
|
2020-05-10 12:45:16 +02:00
|
|
|
const items = {};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
items[1] = "one";
|
|
|
|
items[2] = "two";
|
|
|
|
items[3] = "three";
|
|
|
|
items[4] = "four";
|
2020-05-10 12:45:16 +02:00
|
|
|
|
|
|
|
const list = [1, 2, 3, 4];
|
|
|
|
|
|
|
|
const boring_opts = {
|
|
|
|
get_item: (n) => items[n],
|
|
|
|
};
|
|
|
|
|
2021-01-29 10:27:56 +01:00
|
|
|
assert.deepEqual(ListWidget.get_filtered_items("whatever", list, boring_opts), [
|
2020-07-15 00:34:28 +02:00
|
|
|
"one",
|
|
|
|
"two",
|
|
|
|
"three",
|
|
|
|
"four",
|
|
|
|
]);
|
2020-05-10 12:45:16 +02:00
|
|
|
|
2020-07-02 01:41:40 +02:00
|
|
|
const predicate = (item, value) => item.startsWith(value);
|
2020-05-10 12:45:16 +02:00
|
|
|
|
|
|
|
const predicate_opts = {
|
|
|
|
get_item: (n) => items[n],
|
|
|
|
filter: {
|
2020-07-20 22:18:43 +02:00
|
|
|
predicate,
|
2020-05-10 12:45:16 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-01-29 10:27:56 +01:00
|
|
|
assert.deepEqual(ListWidget.get_filtered_items("t", list, predicate_opts), ["two", "three"]);
|
2020-05-10 12:45:16 +02:00
|
|
|
|
|
|
|
const filterer_opts = {
|
|
|
|
get_item: (n) => items[n],
|
|
|
|
filter: {
|
2020-07-02 01:41:40 +02:00
|
|
|
filterer: (items, value) => items.filter((item) => predicate(item, value)),
|
2020-05-10 12:45:16 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-01-29 10:27:56 +01:00
|
|
|
assert.deepEqual(ListWidget.get_filtered_items("t", list, filterer_opts), ["two", "three"]);
|
2020-05-10 12:45:16 +02:00
|
|
|
});
|
2020-05-28 19:58:51 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("render item", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
2020-05-28 19:58:51 +02:00
|
|
|
const INITIAL_RENDER_COUNT = 80; // Keep this in sync with the actual code.
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html = () => {};
|
2020-05-28 19:58:51 +02:00
|
|
|
let called = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
$scroll_container.find = (query) => {
|
2020-07-15 00:34:28 +02:00
|
|
|
const expected_queries = [
|
|
|
|
`tr[data-item='${INITIAL_RENDER_COUNT}']`,
|
|
|
|
`tr[data-item='${INITIAL_RENDER_COUNT - 1}']`,
|
|
|
|
];
|
2020-05-28 19:58:51 +02:00
|
|
|
const item = INITIAL_RENDER_COUNT - 1;
|
|
|
|
const new_html = `<tr data-item=${item}>updated: ${item}</tr>\n`;
|
2020-07-16 23:08:05 +02:00
|
|
|
const regex = new RegExp(`\\<tr data-item=${item}\\>.*?<\\/tr\\>`);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(expected_queries.includes(query));
|
2020-05-28 19:58:51 +02:00
|
|
|
if (query.includes(`data-item='${INITIAL_RENDER_COUNT}'`)) {
|
2020-09-24 07:50:36 +02:00
|
|
|
return undefined; // This item is not rendered, so we find nothing
|
2020-05-28 19:58:51 +02:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
// Return a JQuery stub for the original HTML.
|
|
|
|
// We want this to be called when we replace
|
|
|
|
// the existing HTML with newly rendered HTML.
|
|
|
|
replaceWith: (html) => {
|
|
|
|
assert.equal(new_html, html);
|
|
|
|
called = true;
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.$appended_data.replace(regex, new_html);
|
2020-05-28 19:58:51 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-01-23 02:55:23 +01:00
|
|
|
const list = [...Array.from({length: 100}).keys()];
|
2020-05-28 19:58:51 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
let text = "initial";
|
2020-05-28 19:58:51 +02:00
|
|
|
const get_item = (item) => ({text: `${text}: ${item}`, value: item});
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const widget = ListWidget.create($container, list, {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "replace-list",
|
2020-05-28 19:58:51 +02:00
|
|
|
modifier: (item) => `<tr data-item=${item.value}>${item.text}</tr>\n`,
|
2020-07-20 22:18:43 +02:00
|
|
|
get_item,
|
2020-05-28 19:58:51 +02:00
|
|
|
html_selector: (item) => `tr[data-item='${item}']`,
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-05-28 19:58:51 +02:00
|
|
|
});
|
|
|
|
const item = INITIAL_RENDER_COUNT - 1;
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($container.$appended_data.html().includes("<tr data-item=2>initial: 2</tr>"));
|
|
|
|
assert.ok($container.$appended_data.html().includes("<tr data-item=3>initial: 3</tr>"));
|
2020-07-15 01:29:15 +02:00
|
|
|
text = "updated";
|
2020-05-28 19:58:51 +02:00
|
|
|
called = false;
|
|
|
|
widget.render_item(INITIAL_RENDER_COUNT - 1);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(called);
|
2022-01-25 11:36:19 +01:00
|
|
|
assert.ok($container.$appended_data.html().includes("<tr data-item=2>initial: 2</tr>"));
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.$appended_data.html().includes(`<tr data-item=${item}>updated: ${item}</tr>`),
|
2021-06-10 08:32:54 +02:00
|
|
|
);
|
2020-05-28 19:58:51 +02:00
|
|
|
|
|
|
|
// Item 80 should not be in the rendered list. (0 indexed)
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(
|
2022-01-25 11:36:19 +01:00
|
|
|
!$container.$appended_data
|
2020-07-15 00:34:28 +02:00
|
|
|
.html()
|
|
|
|
.includes(
|
|
|
|
`<tr data-item=${INITIAL_RENDER_COUNT}>initial: ${INITIAL_RENDER_COUNT}</tr>`,
|
|
|
|
),
|
|
|
|
);
|
2020-05-28 19:58:51 +02:00
|
|
|
called = false;
|
|
|
|
widget.render_item(INITIAL_RENDER_COUNT);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!called);
|
2020-05-28 19:58:51 +02:00
|
|
|
widget.render_item(INITIAL_RENDER_COUNT - 1);
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(called);
|
2020-05-28 19:58:51 +02:00
|
|
|
|
|
|
|
// Tests below this are for the corner cases, where we abort the rerender.
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "html_selector should be a function.");
|
2022-01-25 11:36:19 +01:00
|
|
|
ListWidget.create($container, list, {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "replace-list",
|
2022-04-09 23:44:38 +02:00
|
|
|
modifier: /* istanbul ignore next */ (item) =>
|
|
|
|
`<tr data-item=${item.value}>${item.text}</tr>\n`,
|
2020-07-20 22:18:43 +02:00
|
|
|
get_item,
|
2020-07-15 01:29:15 +02:00
|
|
|
html_selector: "hello world",
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-05-28 19:58:51 +02:00
|
|
|
});
|
|
|
|
blueslip.reset();
|
|
|
|
|
|
|
|
let get_item_called;
|
2022-01-25 11:36:19 +01:00
|
|
|
const widget_2 = ListWidget.create($container, list, {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "replace-list",
|
2020-05-28 19:58:51 +02:00
|
|
|
modifier: (item) => `<tr data-item=${item.value}>${item.text}</tr>\n`,
|
|
|
|
get_item: (item) => {
|
|
|
|
get_item_called = true;
|
|
|
|
return item;
|
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-05-28 19:58:51 +02:00
|
|
|
});
|
|
|
|
get_item_called = false;
|
|
|
|
widget_2.render_item(item);
|
|
|
|
// Test that we didn't try to render the item.
|
2021-06-10 08:32:54 +02:00
|
|
|
assert.ok(!get_item_called);
|
2020-05-28 19:58:51 +02:00
|
|
|
|
|
|
|
let rendering_item = false;
|
2022-01-25 11:36:19 +01:00
|
|
|
const widget_3 = ListWidget.create($container, list, {
|
2020-07-15 01:29:15 +02:00
|
|
|
name: "replace-list",
|
2020-07-15 00:34:28 +02:00
|
|
|
modifier: (item) => (rendering_item ? undefined : `${item}\n`),
|
2020-07-20 22:18:43 +02:00
|
|
|
get_item,
|
2020-05-28 19:58:51 +02:00
|
|
|
html_selector: (item) => `tr[data-item='${item}']`,
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2020-05-28 19:58:51 +02:00
|
|
|
});
|
|
|
|
// Once we have initially rendered the widget, change the
|
|
|
|
// behavior of the modifier function.
|
|
|
|
rendering_item = true;
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "List item is not a string: undefined");
|
2020-05-28 19:58:51 +02:00
|
|
|
widget_3.render_item(item);
|
|
|
|
blueslip.reset();
|
|
|
|
});
|
2021-07-27 14:35:58 +02:00
|
|
|
|
|
|
|
run_test("Multiselect dropdown retain_selected_items", () => {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $container = make_container();
|
|
|
|
const $scroll_container = make_scroll_container();
|
|
|
|
const $filter_element = make_filter_element();
|
2021-07-27 14:35:58 +02:00
|
|
|
let data_rendered = [];
|
|
|
|
|
|
|
|
const list = ["one", "two", "three", "four"].map((x) => ({name: x, value: x}));
|
|
|
|
const data = ["one"]; // Data initially selected.
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
$container.html = () => {};
|
|
|
|
$container.find = (elem) => DropdownItem(elem);
|
2021-07-27 14:35:58 +02:00
|
|
|
|
2022-02-08 00:13:33 +01:00
|
|
|
// We essentially create fake jQuery functions
|
2021-07-27 14:35:58 +02:00
|
|
|
// whose return value are stored in objects so that
|
|
|
|
// they can be later asserted with expected values.
|
|
|
|
function DropdownItem(element) {
|
|
|
|
const temp = {};
|
|
|
|
|
|
|
|
function length() {
|
|
|
|
if (element) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-09 23:44:38 +02:00
|
|
|
/* istanbul ignore next */
|
2021-07-27 14:35:58 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function find(tag) {
|
|
|
|
return ListItem(tag, temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
function addClass(cls) {
|
|
|
|
temp.appended_class = cls;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp.element = element;
|
|
|
|
return {
|
|
|
|
length: length(),
|
|
|
|
find,
|
|
|
|
addClass,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function ListItem(element, temp) {
|
|
|
|
function expectOne() {
|
|
|
|
data_rendered.push(temp);
|
|
|
|
return ListItem(element, temp);
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
function prepend($data) {
|
|
|
|
temp.prepended_data = $data.html();
|
2021-07-27 14:35:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
expectOne,
|
|
|
|
prepend,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-01-25 11:36:19 +01:00
|
|
|
const widget = ListWidget.create($container, list, {
|
2021-07-27 14:35:58 +02:00
|
|
|
name: "replace-list",
|
|
|
|
modifier: (item) => `<li data-value="${item.value}">${item.name}</li>\n`,
|
|
|
|
multiselect: {
|
|
|
|
selected_items: data,
|
|
|
|
},
|
|
|
|
filter: {
|
2022-01-25 11:36:19 +01:00
|
|
|
$element: $filter_element,
|
2021-07-27 14:35:58 +02:00
|
|
|
predicate: () => true,
|
|
|
|
},
|
2022-01-25 11:36:19 +01:00
|
|
|
$simplebar_container: $scroll_container,
|
2021-07-27 14:35:58 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const expected_value = [
|
|
|
|
{
|
|
|
|
element: 'li[data-value = "one"]',
|
|
|
|
appended_class: "checked",
|
|
|
|
prepended_data: "<i>",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
assert.deepEqual(expected_value, data_rendered);
|
|
|
|
|
|
|
|
// Reset the variable and re execute the `widget.render` method.
|
|
|
|
data_rendered = [];
|
|
|
|
|
|
|
|
// Making sure!
|
|
|
|
assert.deepEqual(data_rendered, []);
|
|
|
|
|
|
|
|
widget.hard_redraw();
|
|
|
|
|
|
|
|
// Expect the `data_rendered` array to be same again.
|
|
|
|
assert.deepEqual(expected_value, data_rendered);
|
|
|
|
});
|