2019-06-06 16:47:10 +08:00
|
|
|
|
import selectKit from "helpers/select-kit-helper";
|
2018-06-15 23:03:24 +08:00
|
|
|
|
import componentTest from "helpers/component-test";
|
2018-11-29 22:56:19 +08:00
|
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
|
|
|
|
import { clearCallbacks } from "select-kit/mixins/plugin-api";
|
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
|
moduleForComponent("multi-select", {
|
2017-12-22 20:08:12 +08:00
|
|
|
|
integration: true,
|
|
|
|
|
beforeEach: function() {
|
2018-06-15 23:03:24 +08:00
|
|
|
|
this.set("subject", selectKit());
|
2017-12-22 20:08:12 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
|
componentTest("with objects and values", {
|
|
|
|
|
template: "{{multi-select content=items values=values}}",
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
|
|
beforeEach() {
|
2018-06-15 23:03:24 +08:00
|
|
|
|
this.set("items", [{ id: 1, name: "hello" }, { id: 2, name: "world" }]);
|
|
|
|
|
this.set("values", [1, 2]);
|
2017-11-21 18:53:09 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
test(assert) {
|
2019-05-27 16:42:53 +08:00
|
|
|
|
assert.equal(this.subject.header().value(), "1,2");
|
2017-11-21 18:53:09 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
|
componentTest("with title", {
|
2017-12-22 20:08:12 +08:00
|
|
|
|
template: '{{multi-select title=(i18n "test.title")}}',
|
|
|
|
|
|
|
|
|
|
beforeEach() {
|
2018-06-15 23:03:24 +08:00
|
|
|
|
I18n.translations[I18n.locale].js.test = { title: "My title" };
|
2017-12-22 20:08:12 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
test(assert) {
|
2018-07-30 04:51:32 +08:00
|
|
|
|
assert.equal(
|
|
|
|
|
selectKit()
|
|
|
|
|
.header()
|
|
|
|
|
.title(),
|
|
|
|
|
"My title"
|
2018-06-15 23:03:24 +08:00
|
|
|
|
);
|
2017-12-22 20:08:12 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
|
componentTest("interactions", {
|
|
|
|
|
template: "{{multi-select none=none content=items values=values}}",
|
2017-11-21 18:53:09 +08:00
|
|
|
|
|
|
|
|
|
beforeEach() {
|
2018-06-15 23:03:24 +08:00
|
|
|
|
I18n.translations[I18n.locale].js.test = { none: "none" };
|
|
|
|
|
this.set("items", [
|
|
|
|
|
{ id: 1, name: "regis" },
|
|
|
|
|
{ id: 2, name: "sam" },
|
|
|
|
|
{ id: 3, name: "robin" }
|
|
|
|
|
]);
|
|
|
|
|
this.set("values", [1, 2]);
|
2017-11-21 18:53:09 +08:00
|
|
|
|
},
|
|
|
|
|
|
2018-07-30 04:51:32 +08:00
|
|
|
|
async test(assert) {
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.expand();
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
|
|
|
|
assert.equal(
|
2019-05-27 16:42:53 +08:00
|
|
|
|
this.subject.highlightedRow().name(),
|
2018-07-30 04:51:32 +08:00
|
|
|
|
"robin",
|
|
|
|
|
"it highlights the first content row"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await this.set("none", "test.none");
|
|
|
|
|
|
2019-05-27 16:42:53 +08:00
|
|
|
|
assert.ok(this.subject.noneRow().exists());
|
2018-07-30 04:51:32 +08:00
|
|
|
|
assert.equal(
|
2019-05-27 16:42:53 +08:00
|
|
|
|
this.subject.highlightedRow().name(),
|
2018-07-30 04:51:32 +08:00
|
|
|
|
"robin",
|
|
|
|
|
"it highlights the first content row"
|
|
|
|
|
);
|
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.selectRowByValue(3);
|
|
|
|
|
await this.subject.expand();
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
|
|
|
|
assert.equal(
|
2019-05-27 16:42:53 +08:00
|
|
|
|
this.subject.highlightedRow().name(),
|
2018-07-30 04:51:32 +08:00
|
|
|
|
"none",
|
|
|
|
|
"it highlights none row if no content"
|
|
|
|
|
);
|
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.fillInFilter("joffrey");
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
|
|
|
|
assert.equal(
|
2019-05-27 16:42:53 +08:00
|
|
|
|
this.subject.highlightedRow().name(),
|
2018-07-30 04:51:32 +08:00
|
|
|
|
"joffrey",
|
|
|
|
|
"it highlights create row when filling filter"
|
|
|
|
|
);
|
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.keyboard("enter");
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
|
|
|
|
assert.equal(
|
2019-05-27 16:42:53 +08:00
|
|
|
|
this.subject.highlightedRow().name(),
|
2018-07-30 04:51:32 +08:00
|
|
|
|
"none",
|
|
|
|
|
"it highlights none row after creating content and no content left"
|
|
|
|
|
);
|
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.keyboard("backspace");
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
const $lastSelectedName = this.subject
|
2018-07-30 04:51:32 +08:00
|
|
|
|
.header()
|
|
|
|
|
.el()
|
|
|
|
|
.find(".selected-name")
|
|
|
|
|
.last();
|
|
|
|
|
assert.equal($lastSelectedName.attr("data-name"), "joffrey");
|
|
|
|
|
assert.ok(
|
|
|
|
|
$lastSelectedName.hasClass("is-highlighted"),
|
|
|
|
|
"it highlights the last selected name when using backspace"
|
|
|
|
|
);
|
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.keyboard("backspace");
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
const $lastSelectedName1 = this.subject
|
2018-07-30 04:51:32 +08:00
|
|
|
|
.header()
|
|
|
|
|
.el()
|
|
|
|
|
.find(".selected-name")
|
|
|
|
|
.last();
|
|
|
|
|
assert.equal(
|
|
|
|
|
$lastSelectedName1.attr("data-name"),
|
|
|
|
|
"robin",
|
|
|
|
|
"it removes the previous highlighted selected content"
|
|
|
|
|
);
|
|
|
|
|
assert.notOk(
|
2019-05-27 16:42:53 +08:00
|
|
|
|
this.subject.rowByValue("joffrey").exists(),
|
2018-07-30 04:51:32 +08:00
|
|
|
|
"generated content shouldn’t appear in content when removed"
|
|
|
|
|
);
|
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.keyboard("selectAll");
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
const $highlightedSelectedNames2 = this.subject
|
2018-07-30 04:51:32 +08:00
|
|
|
|
.header()
|
|
|
|
|
.el()
|
|
|
|
|
.find(".selected-name.is-highlighted");
|
|
|
|
|
assert.equal(
|
|
|
|
|
$highlightedSelectedNames2.length,
|
|
|
|
|
3,
|
|
|
|
|
"it highlights each selected name"
|
|
|
|
|
);
|
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.keyboard("backspace");
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
const $selectedNames = this.subject
|
2018-07-30 04:51:32 +08:00
|
|
|
|
.header()
|
|
|
|
|
.el()
|
|
|
|
|
.find(".selected-name");
|
|
|
|
|
assert.equal($selectedNames.length, 0, "it removed all selected content");
|
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
assert.ok(this.subject.isFocused());
|
|
|
|
|
assert.ok(this.subject.isExpanded());
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.keyboard("escape");
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
assert.ok(this.subject.isFocused());
|
|
|
|
|
assert.notOk(this.subject.isExpanded());
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.keyboard("escape");
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
assert.notOk(this.subject.isFocused());
|
|
|
|
|
assert.notOk(this.subject.isExpanded());
|
2017-11-21 18:53:09 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2018-01-11 16:54:39 +08:00
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
|
componentTest("with limitMatches", {
|
|
|
|
|
template: "{{multi-select content=content limitMatches=2}}",
|
2018-01-11 16:54:39 +08:00
|
|
|
|
|
|
|
|
|
beforeEach() {
|
2018-06-15 23:03:24 +08:00
|
|
|
|
this.set("content", ["sam", "jeff", "neil"]);
|
2018-01-11 16:54:39 +08:00
|
|
|
|
},
|
|
|
|
|
|
2018-07-30 04:51:32 +08:00
|
|
|
|
async test(assert) {
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.expand();
|
2018-07-30 04:51:32 +08:00
|
|
|
|
|
2019-05-27 16:42:53 +08:00
|
|
|
|
assert.equal(this.subject.el().find(".select-kit-row").length, 2);
|
2018-01-11 16:54:39 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2018-04-05 22:45:19 +08:00
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
|
componentTest("with minimum", {
|
|
|
|
|
template: "{{multi-select content=content minimum=1}}",
|
2018-04-05 22:45:19 +08:00
|
|
|
|
|
|
|
|
|
beforeEach() {
|
2018-06-15 23:03:24 +08:00
|
|
|
|
this.set("content", ["sam", "jeff", "neil"]);
|
2018-04-05 22:45:19 +08:00
|
|
|
|
},
|
|
|
|
|
|
2018-07-30 04:51:32 +08:00
|
|
|
|
async test(assert) {
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.expand();
|
2018-04-05 22:45:19 +08:00
|
|
|
|
|
2019-05-27 16:42:53 +08:00
|
|
|
|
assert.equal(this.subject.validationMessage(), "Select at least 1 item.");
|
2018-04-05 22:45:19 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.selectRowByValue("sam");
|
2018-04-05 22:45:19 +08:00
|
|
|
|
|
2019-05-27 16:42:53 +08:00
|
|
|
|
assert.equal(this.subject.header().label(), "sam");
|
2018-04-05 22:45:19 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
|
componentTest("with minimumLabel", {
|
|
|
|
|
template:
|
|
|
|
|
'{{multi-select content=content minimum=1 minimumLabel="test.minimum"}}',
|
2018-04-05 22:45:19 +08:00
|
|
|
|
|
|
|
|
|
beforeEach() {
|
2018-06-15 23:03:24 +08:00
|
|
|
|
I18n.translations[I18n.locale].js.test = { minimum: "min %{count}" };
|
|
|
|
|
this.set("content", ["sam", "jeff", "neil"]);
|
2018-04-05 22:45:19 +08:00
|
|
|
|
},
|
|
|
|
|
|
2018-07-30 04:51:32 +08:00
|
|
|
|
async test(assert) {
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.expand();
|
2018-04-05 22:45:19 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
assert.equal(this.subject.validationMessage(), "min 1");
|
2018-04-05 22:45:19 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.selectRowByValue("jeff");
|
2018-04-05 22:45:19 +08:00
|
|
|
|
|
2019-05-27 16:42:53 +08:00
|
|
|
|
assert.equal(this.subject.header().label(), "jeff");
|
2018-04-05 22:45:19 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2018-09-12 18:19:04 +08:00
|
|
|
|
|
|
|
|
|
componentTest("with forceEscape", {
|
|
|
|
|
template: "{{multi-select content=content forceEscape=true}}",
|
|
|
|
|
|
|
|
|
|
beforeEach() {
|
|
|
|
|
this.set("content", ["<div>sam</div>"]);
|
|
|
|
|
},
|
2019-05-01 02:01:21 +08:00
|
|
|
|
skip: true,
|
2018-09-12 18:19:04 +08:00
|
|
|
|
async test(assert) {
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.expand();
|
2018-09-12 18:19:04 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
const row = this.subject.rowByIndex(0);
|
2018-09-12 18:19:04 +08:00
|
|
|
|
assert.equal(
|
|
|
|
|
row
|
|
|
|
|
.el()
|
|
|
|
|
.find(".name")
|
|
|
|
|
.html()
|
|
|
|
|
.trim(),
|
|
|
|
|
"<div>sam</div>"
|
|
|
|
|
);
|
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.fillInFilter("<div>jeff</div>");
|
|
|
|
|
await this.subject.keyboard("enter");
|
2018-09-12 18:19:04 +08:00
|
|
|
|
|
|
|
|
|
assert.equal(
|
2019-05-27 16:15:39 +08:00
|
|
|
|
this.subject
|
2018-09-12 18:19:04 +08:00
|
|
|
|
.header()
|
|
|
|
|
.el()
|
|
|
|
|
.find(".name")
|
|
|
|
|
.html()
|
|
|
|
|
.trim(),
|
|
|
|
|
"<div>jeff</div>"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
componentTest("with forceEscape", {
|
|
|
|
|
template: "{{multi-select content=content forceEscape=false}}",
|
|
|
|
|
|
|
|
|
|
beforeEach() {
|
|
|
|
|
this.set("content", ["<div>sam</div>"]);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async test(assert) {
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.expand();
|
2018-09-12 18:19:04 +08:00
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
const row = this.subject.rowByIndex(0);
|
2018-09-12 18:19:04 +08:00
|
|
|
|
assert.equal(
|
|
|
|
|
row
|
|
|
|
|
.el()
|
|
|
|
|
.find(".name")
|
|
|
|
|
.html()
|
|
|
|
|
.trim(),
|
|
|
|
|
"<div>sam</div>"
|
|
|
|
|
);
|
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.fillInFilter("<div>jeff</div>");
|
|
|
|
|
await this.subject.keyboard("enter");
|
2018-09-12 18:19:04 +08:00
|
|
|
|
|
|
|
|
|
assert.equal(
|
2019-05-27 16:15:39 +08:00
|
|
|
|
this.subject
|
2018-09-12 18:19:04 +08:00
|
|
|
|
.header()
|
|
|
|
|
.el()
|
|
|
|
|
.find(".name")
|
|
|
|
|
.html()
|
|
|
|
|
.trim(),
|
|
|
|
|
"<div>jeff</div>"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-11-29 22:56:19 +08:00
|
|
|
|
|
|
|
|
|
componentTest("support modifying on select behavior through plugin api", {
|
|
|
|
|
template:
|
|
|
|
|
'<span class="on-select-test"></span>{{multi-select content=content}}',
|
|
|
|
|
|
|
|
|
|
beforeEach() {
|
|
|
|
|
withPluginApi("0.8.13", api => {
|
|
|
|
|
api.modifySelectKit("select-kit").onSelect((context, value) => {
|
|
|
|
|
find(".on-select-test").html(value);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
this.set("content", [
|
|
|
|
|
{ id: "1", name: "robin" },
|
|
|
|
|
{ id: "2", name: "arpit", __sk_row_type: "noopRow" }
|
|
|
|
|
]);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
async test(assert) {
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.expand();
|
|
|
|
|
await this.subject.selectRowByValue(1);
|
2018-11-29 22:56:19 +08:00
|
|
|
|
|
|
|
|
|
assert.equal(find(".on-select-test").html(), "1");
|
|
|
|
|
|
2019-05-27 16:15:39 +08:00
|
|
|
|
await this.subject.expand();
|
|
|
|
|
await this.subject.selectRowByValue(2);
|
2018-11-29 22:56:19 +08:00
|
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
|
find(".on-select-test").html(),
|
|
|
|
|
"2",
|
|
|
|
|
"it calls onSelect for noopRows"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
clearCallbacks();
|
|
|
|
|
}
|
|
|
|
|
});
|