2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2020-11-30 23:46:45 +01:00
|
|
|
const {strict: assert} = require("assert");
|
|
|
|
|
2021-02-28 01:00:36 +01:00
|
|
|
const rewiremock = require("rewiremock/node");
|
|
|
|
|
2020-12-01 00:02:16 +01:00
|
|
|
const {set_global, zrequire} = require("../zjsunit/namespace");
|
2020-12-01 00:39:47 +01:00
|
|
|
const {run_test} = require("../zjsunit/test");
|
2021-02-21 15:38:51 +01:00
|
|
|
const $ = require("../zjsunit/zjquery");
|
2020-12-01 00:02:16 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("document", {});
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
const noop = () => {};
|
2020-07-15 01:29:15 +02:00
|
|
|
const example_img_link = "http://example.com/example.png";
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2021-02-28 01:00:36 +01:00
|
|
|
rewiremock("../../static/js/ui_util").with({
|
2018-03-07 20:46:13 +01:00
|
|
|
place_caret_at_end: noop,
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
set_global("getSelection", () => ({
|
2020-07-02 01:41:40 +02:00
|
|
|
anchorOffset: 0,
|
|
|
|
}));
|
2018-08-24 19:09:09 +02:00
|
|
|
|
2021-02-28 01:00:36 +01:00
|
|
|
rewiremock.enable();
|
|
|
|
|
2020-12-01 23:21:38 +01:00
|
|
|
zrequire("templates");
|
|
|
|
const input_pill = zrequire("input_pill");
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let id_seq = 0;
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("set_up_ids", () => {
|
2018-03-07 20:46:13 +01:00
|
|
|
// just get coverage on a simple one-liner:
|
|
|
|
input_pill.random_id();
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
input_pill.random_id = () => {
|
2018-03-07 20:46:13 +01:00
|
|
|
id_seq += 1;
|
2020-07-15 01:29:15 +02:00
|
|
|
return "some_id" + id_seq;
|
2018-03-07 20:46:13 +01:00
|
|
|
};
|
2018-06-02 09:17:16 +02:00
|
|
|
});
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2018-06-27 20:17:04 +02:00
|
|
|
function pill_html(value, data_id, img_src) {
|
2019-11-02 00:06:25 +01:00
|
|
|
const has_image = img_src !== undefined;
|
2018-06-27 20:17:04 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const opts = {
|
2018-03-31 13:22:29 +02:00
|
|
|
id: data_id,
|
|
|
|
display_value: value,
|
2020-07-20 22:18:43 +02:00
|
|
|
has_image,
|
2018-03-31 13:22:29 +02:00
|
|
|
};
|
|
|
|
|
2018-06-27 20:17:04 +02:00
|
|
|
if (has_image) {
|
|
|
|
opts.img_src = img_src;
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
return require("../../static/templates/input_pill.hbs")(opts);
|
2018-03-07 20:46:13 +01:00
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("basics", () => {
|
2019-11-02 00:06:25 +01:00
|
|
|
const config = {};
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Pill needs container.");
|
2018-03-07 20:46:13 +01:00
|
|
|
input_pill.create(config);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const pill_input = $.create("pill_input");
|
|
|
|
const container = $.create("container");
|
|
|
|
container.set_find_results(".input", pill_input);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Pill needs create_item_from_text");
|
2018-03-07 20:46:13 +01:00
|
|
|
config.container = container;
|
|
|
|
input_pill.create(config);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "Pill needs get_text_from_item");
|
2018-03-07 20:46:13 +01:00
|
|
|
config.create_item_from_text = noop;
|
|
|
|
input_pill.create(config);
|
|
|
|
|
|
|
|
config.get_text_from_item = noop;
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget = input_pill.create(config);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const item = {
|
2020-07-15 01:29:15 +02:00
|
|
|
display_value: "JavaScript",
|
|
|
|
language: "js",
|
2018-06-27 20:17:04 +02:00
|
|
|
img_src: example_img_link,
|
2018-03-07 20:46:13 +01:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let inserted_before;
|
2020-07-15 01:29:15 +02:00
|
|
|
const expected_html = pill_html("JavaScript", "some_id1", example_img_link);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
pill_input.before = (elem) => {
|
2018-03-07 20:46:13 +01:00
|
|
|
inserted_before = true;
|
|
|
|
assert.equal(elem.html(), expected_html);
|
|
|
|
};
|
|
|
|
|
|
|
|
widget.appendValidatedData(item);
|
|
|
|
assert(inserted_before);
|
|
|
|
|
|
|
|
assert.deepEqual(widget.items(), [item]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2018-08-24 17:47:26 +02:00
|
|
|
function set_up() {
|
2019-11-02 00:06:25 +01:00
|
|
|
const items = {
|
2018-03-07 20:46:13 +01:00
|
|
|
blue: {
|
2020-07-15 01:29:15 +02:00
|
|
|
display_value: "BLUE",
|
|
|
|
description: "color of the sky",
|
2018-06-27 20:17:04 +02:00
|
|
|
img_src: example_img_link,
|
2018-03-07 20:46:13 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
red: {
|
2020-07-15 01:29:15 +02:00
|
|
|
display_value: "RED",
|
|
|
|
description: "color of stop signs",
|
2018-03-07 20:46:13 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
yellow: {
|
2020-07-15 01:29:15 +02:00
|
|
|
display_value: "YELLOW",
|
|
|
|
description: "color of bananas",
|
2018-03-07 20:46:13 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const pill_input = $.create("pill_input");
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2020-08-01 03:56:10 +02:00
|
|
|
pill_input[0] = {};
|
2018-08-26 13:40:22 +02:00
|
|
|
pill_input.before = () => {};
|
|
|
|
|
2021-02-23 14:37:26 +01:00
|
|
|
const create_item_from_text = (text) => items[text];
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const container = $.create("container");
|
|
|
|
container.set_find_results(".input", pill_input);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const config = {
|
2020-07-20 22:18:43 +02:00
|
|
|
container,
|
|
|
|
create_item_from_text,
|
2018-08-26 17:01:06 +02:00
|
|
|
get_text_from_item: (item) => item.display_value,
|
2018-03-07 20:46:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
id_seq = 0;
|
|
|
|
|
2018-08-24 17:47:26 +02:00
|
|
|
return {
|
2020-07-20 22:18:43 +02:00
|
|
|
config,
|
|
|
|
pill_input,
|
|
|
|
items,
|
|
|
|
container,
|
2018-08-24 17:47:26 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("copy from pill", () => {
|
2018-08-26 17:01:06 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
|
|
|
const container = info.container;
|
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-26 17:01:06 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const copy_handler = container.get_on_handler("copy", ".pill");
|
2018-08-26 17:01:06 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let copied_text;
|
2018-08-26 17:01:06 +02:00
|
|
|
|
|
|
|
const pill_stub = {
|
|
|
|
data: (field) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(field, "id");
|
|
|
|
return "some_id2";
|
2018-08-26 17:01:06 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const e = {
|
|
|
|
originalEvent: {
|
|
|
|
clipboardData: {
|
|
|
|
setData: (format, text) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(format, "text/plain");
|
2018-08-26 17:01:06 +02:00
|
|
|
copied_text = text;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
preventDefault: noop,
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
container.set_find_results(":focus", pill_stub);
|
2018-08-26 17:01:06 +02:00
|
|
|
|
|
|
|
copy_handler(e);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(copied_text, "RED");
|
2018-08-26 17:01:06 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("paste to input", () => {
|
2018-08-26 17:15:12 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
|
|
|
const container = info.container;
|
|
|
|
const items = info.items;
|
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const paste_handler = container.get_on_handler("paste", ".input");
|
2018-08-26 17:15:12 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const paste_text = "blue,yellow";
|
2018-08-26 17:15:12 +02:00
|
|
|
|
|
|
|
const e = {
|
|
|
|
originalEvent: {
|
|
|
|
clipboardData: {
|
|
|
|
getData: (format) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(format, "text/plain");
|
2018-08-26 17:15:12 +02:00
|
|
|
return paste_text;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
preventDefault: noop,
|
|
|
|
};
|
|
|
|
|
|
|
|
document.execCommand = (cmd, _, text) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(cmd, "insertText");
|
|
|
|
container.find(".input").text(text);
|
2018-08-26 17:15:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
paste_handler(e);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.yellow]);
|
2020-05-15 01:27:15 +02:00
|
|
|
|
|
|
|
let entered = false;
|
2020-07-02 01:45:54 +02:00
|
|
|
widget.createPillonPaste(() => {
|
2020-05-15 01:27:15 +02:00
|
|
|
entered = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
paste_handler(e);
|
|
|
|
assert(entered);
|
2018-08-26 17:15:12 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("arrows on pills", () => {
|
2018-08-26 21:16:46 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
|
|
|
const container = info.container;
|
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-26 21:16:46 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const key_handler = container.get_on_handler("keydown", ".pill");
|
2018-08-26 21:16:46 +02:00
|
|
|
|
|
|
|
function test_key(c) {
|
|
|
|
key_handler({
|
|
|
|
charCode: c,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const LEFT_ARROW = 37;
|
|
|
|
const RIGHT_ARROW = 39;
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let prev_focused = false;
|
|
|
|
let next_focused = false;
|
2018-08-26 21:16:46 +02:00
|
|
|
|
|
|
|
const pill_stub = {
|
2020-07-02 01:41:40 +02:00
|
|
|
prev: () => ({
|
2020-07-20 21:24:26 +02:00
|
|
|
trigger: (type) => {
|
|
|
|
if (type === "focus") {
|
|
|
|
prev_focused = true;
|
|
|
|
}
|
2020-07-02 01:41:40 +02:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
next: () => ({
|
2020-07-20 21:24:26 +02:00
|
|
|
trigger: (type) => {
|
|
|
|
if (type === "focus") {
|
|
|
|
next_focused = true;
|
|
|
|
}
|
2020-07-02 01:41:40 +02:00
|
|
|
},
|
|
|
|
}),
|
2018-08-26 21:16:46 +02:00
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
container.set_find_results(".pill:focus", pill_stub);
|
2018-08-26 21:16:46 +02:00
|
|
|
|
|
|
|
// We use the same stub to test both arrows, since we don't
|
|
|
|
// actually cause any real state changes here. We stub out
|
|
|
|
// the only interaction, which is to move the focus.
|
|
|
|
test_key(LEFT_ARROW);
|
|
|
|
assert(prev_focused);
|
|
|
|
|
|
|
|
test_key(RIGHT_ARROW);
|
|
|
|
assert(next_focused);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("left arrow on input", () => {
|
2018-08-24 19:09:09 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
|
|
|
const container = info.container;
|
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-24 19:09:09 +02:00
|
|
|
|
|
|
|
const LEFT_ARROW = 37;
|
2020-07-15 01:29:15 +02:00
|
|
|
const key_handler = container.get_on_handler("keydown", ".input");
|
2018-08-24 19:09:09 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let last_pill_focused = false;
|
2018-08-24 19:09:09 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
container.set_find_results(".pill", {
|
2020-07-02 01:41:40 +02:00
|
|
|
last: () => ({
|
2020-07-20 21:24:26 +02:00
|
|
|
trigger: (type) => {
|
|
|
|
if (type === "focus") {
|
|
|
|
last_pill_focused = true;
|
|
|
|
}
|
2020-07-02 01:41:40 +02:00
|
|
|
},
|
|
|
|
}),
|
2018-08-24 19:09:09 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
key_handler({
|
|
|
|
keyCode: LEFT_ARROW,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(last_pill_focused);
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("comma", () => {
|
2018-08-24 18:58:02 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
|
|
|
const items = info.items;
|
|
|
|
const pill_input = info.pill_input;
|
|
|
|
const container = info.container;
|
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-24 18:58:02 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red]);
|
2018-08-24 18:58:02 +02:00
|
|
|
|
|
|
|
const COMMA = 188;
|
2020-07-15 01:29:15 +02:00
|
|
|
const key_handler = container.get_on_handler("keydown", ".input");
|
2018-08-24 18:58:02 +02:00
|
|
|
|
2021-02-24 15:04:00 +01:00
|
|
|
pill_input.text(" yel");
|
2018-08-24 18:58:02 +02:00
|
|
|
|
|
|
|
key_handler({
|
|
|
|
keyCode: COMMA,
|
|
|
|
preventDefault: noop,
|
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red]);
|
2018-08-24 18:58:02 +02:00
|
|
|
|
2021-02-24 15:04:00 +01:00
|
|
|
pill_input.text(" yellow");
|
2018-08-24 18:58:02 +02:00
|
|
|
|
|
|
|
key_handler({
|
|
|
|
keyCode: COMMA,
|
2020-06-01 02:42:46 +02:00
|
|
|
preventDefault: noop,
|
2018-08-24 18:58:02 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red, items.yellow]);
|
2018-08-24 18:58:02 +02:00
|
|
|
});
|
|
|
|
|
2020-08-11 02:09:14 +02:00
|
|
|
run_test("Enter key with text", () => {
|
2018-08-24 18:09:57 +02:00
|
|
|
const info = set_up();
|
|
|
|
const config = info.config;
|
|
|
|
const items = info.items;
|
|
|
|
const container = info.container;
|
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-24 18:09:57 +02:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red]);
|
2018-08-24 18:09:57 +02:00
|
|
|
|
|
|
|
const ENTER = 13;
|
2020-07-15 01:29:15 +02:00
|
|
|
const key_handler = container.get_on_handler("keydown", ".input");
|
2018-08-24 18:09:57 +02:00
|
|
|
|
|
|
|
key_handler({
|
|
|
|
keyCode: ENTER,
|
|
|
|
preventDefault: noop,
|
|
|
|
stopPropagation: noop,
|
|
|
|
target: {
|
2020-10-07 10:15:47 +02:00
|
|
|
textContent: " yellow ",
|
2018-08-24 18:09:57 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red, items.yellow]);
|
2018-08-24 18:09:57 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("insert_remove", () => {
|
2018-08-24 17:47:26 +02:00
|
|
|
const info = set_up();
|
|
|
|
|
|
|
|
const config = info.config;
|
|
|
|
const pill_input = info.pill_input;
|
|
|
|
const items = info.items;
|
|
|
|
const container = info.container;
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const inserted_html = [];
|
2021-02-23 14:37:26 +01:00
|
|
|
pill_input.before = (elem) => {
|
2018-08-24 17:47:26 +02:00
|
|
|
inserted_html.push(elem.html());
|
|
|
|
};
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const widget = input_pill.create(config);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let created;
|
|
|
|
let removed;
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
widget.onPillCreate(() => {
|
2018-03-07 20:46:13 +01:00
|
|
|
created = true;
|
|
|
|
});
|
|
|
|
|
2020-07-02 01:45:54 +02:00
|
|
|
widget.onPillRemove(() => {
|
2018-03-07 20:46:13 +01:00
|
|
|
removed = true;
|
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,chartreuse,red,yellow,mauve");
|
2018-03-07 20:46:13 +01:00
|
|
|
|
|
|
|
assert(created);
|
|
|
|
assert(!removed);
|
|
|
|
|
|
|
|
assert.deepEqual(inserted_html, [
|
2020-07-15 01:29:15 +02:00
|
|
|
pill_html("BLUE", "some_id1", example_img_link),
|
|
|
|
pill_html("RED", "some_id2"),
|
|
|
|
pill_html("YELLOW", "some_id3"),
|
2018-03-07 20:46:13 +01:00
|
|
|
]);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red, items.yellow]);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(pill_input.text(), "chartreuse, mauve");
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2018-09-18 01:25:38 +02:00
|
|
|
assert.equal(widget.is_pending(), true);
|
2018-03-07 20:46:13 +01:00
|
|
|
widget.clear_text();
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(pill_input.text(), "");
|
2018-09-18 01:25:38 +02:00
|
|
|
assert.equal(widget.is_pending(), false);
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
let color_removed;
|
|
|
|
function set_colored_removed_func(color) {
|
|
|
|
return () => {
|
|
|
|
color_removed = color;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const pills = widget._get_pills_for_testing();
|
|
|
|
for (const pill of pills) {
|
|
|
|
pill.$element.remove = set_colored_removed_func(pill.item.display_value);
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const BACKSPACE = 8;
|
2020-07-15 01:29:15 +02:00
|
|
|
let key_handler = container.get_on_handler("keydown", ".input");
|
2018-03-07 20:46:13 +01:00
|
|
|
|
|
|
|
key_handler({
|
|
|
|
keyCode: BACKSPACE,
|
|
|
|
target: {
|
2020-10-07 10:15:47 +02:00
|
|
|
textContent: "",
|
2018-03-07 20:46:13 +01:00
|
|
|
},
|
|
|
|
preventDefault: noop,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert(removed);
|
2021-02-25 16:07:04 +01:00
|
|
|
assert.equal(color_removed, "YELLOW");
|
2018-03-07 20:46:13 +01:00
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.blue, items.red]);
|
2018-08-24 15:42:48 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let next_pill_focused = false;
|
2018-08-24 15:42:48 +02:00
|
|
|
|
|
|
|
const next_pill_stub = {
|
2020-07-20 21:24:26 +02:00
|
|
|
trigger: (type) => {
|
|
|
|
if (type === "focus") {
|
|
|
|
next_pill_focused = true;
|
|
|
|
}
|
2018-08-24 15:42:48 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const focus_pill_stub = {
|
|
|
|
next: () => next_pill_stub,
|
|
|
|
data: (field) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(field, "id");
|
|
|
|
return "some_id1";
|
2018-08-24 15:42:48 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
container.set_find_results(".pill:focus", focus_pill_stub);
|
2018-08-24 15:42:48 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
key_handler = container.get_on_handler("keydown", ".pill");
|
2018-08-24 15:42:48 +02:00
|
|
|
key_handler({
|
|
|
|
keyCode: BACKSPACE,
|
|
|
|
preventDefault: noop,
|
|
|
|
});
|
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
assert.equal(color_removed, "BLUE");
|
2018-08-24 15:42:48 +02:00
|
|
|
assert(next_pill_focused);
|
2018-08-25 23:22:46 +02:00
|
|
|
});
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("exit button on pill", () => {
|
2018-08-25 23:22:46 +02:00
|
|
|
const info = set_up();
|
|
|
|
|
|
|
|
const config = info.config;
|
|
|
|
const items = info.items;
|
|
|
|
const container = info.container;
|
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
widget.appendValue("blue,red");
|
2018-08-25 23:22:46 +02:00
|
|
|
|
2021-02-25 16:07:04 +01:00
|
|
|
const pills = widget._get_pills_for_testing();
|
|
|
|
for (const pill of pills) {
|
|
|
|
pill.$element.remove = () => {};
|
|
|
|
}
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let next_pill_focused = false;
|
2018-08-25 23:22:46 +02:00
|
|
|
|
|
|
|
const next_pill_stub = {
|
2020-07-20 21:24:26 +02:00
|
|
|
trigger: (type) => {
|
|
|
|
if (type === "focus") {
|
|
|
|
next_pill_focused = true;
|
|
|
|
}
|
2018-08-25 23:22:46 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const curr_pill_stub = {
|
|
|
|
next: () => next_pill_stub,
|
|
|
|
data: (field) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(field, "id");
|
|
|
|
return "some_id1";
|
2018-08-25 23:22:46 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const exit_button_stub = {
|
2020-07-02 01:41:40 +02:00
|
|
|
to_$: () => ({
|
|
|
|
closest: (sel) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sel, ".pill");
|
2020-07-02 01:41:40 +02:00
|
|
|
return curr_pill_stub;
|
|
|
|
},
|
|
|
|
}),
|
2018-08-25 23:22:46 +02:00
|
|
|
};
|
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
const e = {
|
2018-11-02 16:00:43 +01:00
|
|
|
stopPropagation: noop,
|
|
|
|
};
|
2020-07-15 01:29:15 +02:00
|
|
|
const exit_click_handler = container.get_on_handler("click", ".exit");
|
2018-08-25 23:22:46 +02:00
|
|
|
|
2020-02-12 01:35:16 +01:00
|
|
|
exit_click_handler.call(exit_button_stub, e);
|
2018-08-25 23:22:46 +02:00
|
|
|
|
|
|
|
assert(next_pill_focused);
|
|
|
|
|
2020-07-15 00:34:28 +02:00
|
|
|
assert.deepEqual(widget.items(), [items.red]);
|
2018-05-15 12:40:07 +02:00
|
|
|
});
|
2018-08-26 21:29:25 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
run_test("misc things", () => {
|
2018-08-26 21:29:25 +02:00
|
|
|
const info = set_up();
|
|
|
|
|
|
|
|
const config = info.config;
|
|
|
|
const container = info.container;
|
|
|
|
const pill_input = info.pill_input;
|
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
|
|
|
|
|
|
|
// animation
|
2020-07-15 01:29:15 +02:00
|
|
|
const animation_end_handler = container.get_on_handler("animationend", ".input");
|
2018-08-26 21:29:25 +02:00
|
|
|
|
2019-11-02 00:06:25 +01:00
|
|
|
let shake_class_removed = false;
|
2018-08-26 21:29:25 +02:00
|
|
|
|
|
|
|
const input_stub = {
|
2020-07-02 01:41:40 +02:00
|
|
|
to_$: () => ({
|
|
|
|
removeClass: (cls) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(cls, "shake");
|
2020-07-02 01:41:40 +02:00
|
|
|
shake_class_removed = true;
|
|
|
|
},
|
|
|
|
}),
|
2018-08-26 21:29:25 +02:00
|
|
|
};
|
|
|
|
|
2020-02-12 01:35:16 +01:00
|
|
|
animation_end_handler.call(input_stub);
|
2018-08-26 21:29:25 +02:00
|
|
|
assert(shake_class_removed);
|
|
|
|
|
|
|
|
// bad data
|
2020-07-15 01:29:15 +02:00
|
|
|
blueslip.expect("error", "no display_value returned");
|
|
|
|
widget.appendValidatedData("this-has-no-item-attribute");
|
2018-08-26 21:29:25 +02:00
|
|
|
|
|
|
|
// click on container
|
2020-07-15 01:29:15 +02:00
|
|
|
const container_click_handler = container.get_on_handler("click");
|
2018-08-26 21:29:25 +02:00
|
|
|
|
2020-07-15 01:29:15 +02:00
|
|
|
const stub = $.create("the-pill-container");
|
|
|
|
stub.set_find_results(".input", pill_input);
|
2018-08-26 21:29:25 +02:00
|
|
|
stub.is = (sel) => {
|
2020-07-15 01:29:15 +02:00
|
|
|
assert.equal(sel, ".pill-container");
|
2018-08-26 21:29:25 +02:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
const this_ = {
|
|
|
|
to_$: () => stub,
|
|
|
|
};
|
|
|
|
|
|
|
|
container_click_handler.call(this_, {target: this_});
|
|
|
|
});
|
2021-02-25 17:31:33 +01:00
|
|
|
|
|
|
|
run_test("clear", () => {
|
|
|
|
const pill_input = $.create("pill_input");
|
|
|
|
const container = $.create("container");
|
|
|
|
container.set_find_results(".input", pill_input);
|
|
|
|
|
|
|
|
const config = {
|
|
|
|
container,
|
|
|
|
create_item_from_text: (s) => ({display_value: s}),
|
|
|
|
get_text_from_item: (s) => s.display_value,
|
|
|
|
};
|
|
|
|
|
|
|
|
pill_input.before = () => {};
|
|
|
|
pill_input[0] = {};
|
|
|
|
|
|
|
|
const widget = input_pill.create(config);
|
|
|
|
|
|
|
|
widget.appendValue("red,yellow,blue");
|
|
|
|
|
|
|
|
const pills = widget._get_pills_for_testing();
|
|
|
|
|
|
|
|
const removed_colors = [];
|
|
|
|
for (const pill of pills) {
|
|
|
|
pill.$element.remove = () => {
|
|
|
|
removed_colors.push(pill.item.display_value);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.clear();
|
|
|
|
|
|
|
|
// Note that we remove colors in the reverse order that we inserted.
|
|
|
|
assert.deepEqual(removed_colors, ["blue", "yellow", "red"]);
|
|
|
|
assert.equal(pill_input[0].textContent, "");
|
|
|
|
});
|
2021-02-28 01:00:36 +01:00
|
|
|
rewiremock.disable();
|