discourse/test/javascripts/components/select-box-test.js.es6

329 lines
7.9 KiB
Plaintext
Raw Normal View History

import componentTest from 'helpers/component-test';
2017-10-19 15:51:08 -04:00
moduleForComponent('select-box-kit', { integration: true });
componentTest('updating the content refreshes the list', {
2017-10-19 15:51:08 -04:00
template: '{{select-box-kit value=1 content=content}}',
beforeEach() {
2017-10-19 15:51:08 -04:00
this.set("content", [{ id: 1, name: "robin" }]);
},
test(assert) {
expandSelectBox();
andThen(() => {
2017-10-19 15:51:08 -04:00
assert.equal(selectBox().rowByValue(1).name(), "robin");
this.set("content", [{ id: 1, name: "regis" }]);
assert.equal(selectBox().rowByValue(1).name(), "regis");
});
}
});
componentTest('accepts a value by reference', {
2017-10-19 15:51:08 -04:00
template: '{{select-box-kit value=value content=content}}',
beforeEach() {
this.set("value", 1);
2017-10-19 15:51:08 -04:00
this.set("content", [{ id: 1, name: "robin" }, { id: 2, name: "regis" }]);
},
test(assert) {
expandSelectBox();
andThen(() => {
assert.equal(
2017-10-19 15:51:08 -04:00
selectBox().selectedRow.name(), "robin",
"it highlights the row corresponding to the value"
);
});
selectBoxSelectRow(1);
andThen(() => {
assert.equal(this.get("value"), 1, "it mutates the value");
});
}
});
componentTest('select-box can be filtered', {
2017-10-19 15:51:08 -04:00
template: '{{select-box-kit filterable=true value=1 content=content}}',
beforeEach() {
2017-10-19 15:51:08 -04:00
this.set("content", [{ id: 1, name: "robin"}, { id: 2, name: "regis" }]);
},
test(assert) {
expandSelectBox();
2017-10-19 15:51:08 -04:00
andThen(() => assert.equal(find(".select-box-kit-filter-input").length, 1, "it has a search input"));
selectBoxFillInFilter("regis");
andThen(() => assert.equal(selectBox().rows.length, 1, "it filters results"));
selectBoxFillInFilter("");
andThen(() => {
assert.equal(
selectBox().rows.length, 2,
"it returns to original content when filter is empty"
);
});
}
});
componentTest('no default icon', {
2017-10-19 15:51:08 -04:00
template: '{{select-box-kit}}',
test(assert) {
assert.equal(selectBox().header.icon().length, 0, "it doesnt have an icon if not specified");
}
});
componentTest('default search icon', {
2017-10-19 15:51:08 -04:00
template: '{{select-box-kit filterable=true}}',
test(assert) {
expandSelectBox();
andThen(() => {
2017-10-19 15:51:08 -04:00
assert.ok(exists(selectBox().filter.icon), "it has a the correct icon");
});
}
});
componentTest('with no search icon', {
2017-10-19 15:51:08 -04:00
template: '{{select-box-kit filterable=true filterIcon=null}}',
test(assert) {
expandSelectBox();
andThen(() => {
assert.equal(selectBox().filter.icon().length, 0, "it has no icon");
});
}
});
componentTest('custom search icon', {
2017-10-19 15:51:08 -04:00
template: '{{select-box-kit filterable=true filterIcon="shower"}}',
test(assert) {
expandSelectBox();
andThen(() => {
assert.ok(selectBox().filter.icon().hasClass("d-icon-shower"), "it has a the correct icon");
});
}
});
componentTest('select-box is expandable', {
2017-10-19 15:51:08 -04:00
template: '{{select-box-kit}}',
test(assert) {
expandSelectBox();
andThen(() => assert.ok(selectBox().isExpanded) );
collapseSelectBox();
andThen(() => assert.notOk(selectBox().isExpanded) );
}
});
2017-10-19 15:51:08 -04:00
componentTest('accepts custom value/name keys', {
template: '{{select-box-kit value=value nameProperty="item" content=content valueAttribute="identifier"}}',
beforeEach() {
this.set("value", 1);
2017-10-19 15:51:08 -04:00
this.set("content", [{ identifier: 1, item: "robin" }]);
},
test(assert) {
expandSelectBox();
andThen(() => {
2017-10-19 15:51:08 -04:00
assert.equal(selectBox().selectedRow.name(), "robin");
});
}
});
componentTest('doesnt render collection content before first expand', {
2017-10-19 15:51:08 -04:00
template: '{{select-box-kit value=1 content=content}}',
beforeEach() {
2017-10-19 15:51:08 -04:00
this.set("content", [{ value: 1, name: "robin" }]);
},
test(assert) {
2017-10-19 15:51:08 -04:00
assert.notOk(exists(find(".select-box-kit-collection")));
expandSelectBox();
andThen(() => {
2017-10-19 15:51:08 -04:00
assert.ok(exists(find(".select-box-kit-collection")));
});
}
});
2017-10-19 15:51:08 -04:00
componentTest('persists filter state when expanding/collapsing', {
template: '{{select-box-kit value=1 content=content filterable=true}}',
beforeEach() {
2017-10-19 15:51:08 -04:00
this.set("content", [{ id: 1, name: "robin" }, { id: 2, name: "régis" }]);
},
test(assert) {
expandSelectBox();
selectBoxFillInFilter("rob");
andThen(() => assert.equal(selectBox().rows.length, 1) );
collapseSelectBox();
andThen(() => assert.notOk(selectBox().isExpanded) );
expandSelectBox();
andThen(() => assert.equal(selectBox().rows.length, 1) );
}
});
componentTest('supports options to limit size', {
2017-10-19 15:51:08 -04:00
template: '{{select-box-kit collectionHeight=20 content=content}}',
beforeEach() {
2017-10-19 15:51:08 -04:00
this.set("content", [{ id: 1, name: "robin" }]);
},
test(assert) {
expandSelectBox();
andThen(() => {
2017-10-19 15:51:08 -04:00
const height = find(".select-box-kit-collection").height();
assert.equal(parseInt(height, 10), 20, "it limits the height");
});
}
});
2017-10-19 15:51:08 -04:00
componentTest('dynamic headerText', {
template: '{{select-box-kit value=1 content=content}}',
beforeEach() {
2017-10-19 15:51:08 -04:00
this.set("content", [{ id: 1, name: "robin" }, { id: 2, name: "regis" }]);
},
test(assert) {
expandSelectBox();
2017-10-19 15:51:08 -04:00
andThen(() => assert.equal(selectBox().header.name(), "robin") );
2017-10-19 15:51:08 -04:00
selectBoxSelectRow(2);
andThen(() => {
2017-10-19 15:51:08 -04:00
assert.equal(selectBox().header.name(), "regis", "it changes header text");
});
}
});
2017-10-19 15:51:08 -04:00
componentTest('supports custom row template', {
template: '{{select-box-kit content=content templateForRow=templateForRow}}',
beforeEach() {
2017-10-19 15:51:08 -04:00
this.set("content", [{ id: 1, name: "robin" }]);
this.set("templateForRow", rowComponent => {
return `<b>${rowComponent.get("content.name")}</b>`;
});
},
test(assert) {
expandSelectBox();
2017-10-19 15:51:08 -04:00
andThen(() => assert.equal(selectBox().rowByValue(1).el.html().trim(), "<b>robin</b>") );
}
});
2017-10-19 15:51:08 -04:00
componentTest('supports converting select value to integer', {
template: '{{select-box-kit value=value content=content castInteger=true}}',
beforeEach() {
2017-10-19 15:51:08 -04:00
this.set("value", 2);
this.set("content", [{ id: "1", name: "robin"}, {id: "2", name: "régis" }]);
},
test(assert) {
expandSelectBox();
2017-10-19 15:51:08 -04:00
andThen(() => assert.equal(selectBox().selectedRow.name(), "régis") );
andThen(() => {
2017-10-19 15:51:08 -04:00
this.set("value", 3);
this.set("content", [{ id: "3", name: "jeff" }]);
});
andThen(() => {
2017-10-19 15:51:08 -04:00
assert.equal(selectBox().selectedRow.name(), "jeff", "it works with dynamic content");
});
}
});
componentTest('supports keyboard events', {
2017-10-19 15:51:08 -04:00
template: '{{select-box-kit content=content filterable=true}}',
beforeEach() {
2017-10-19 15:51:08 -04:00
this.set("content", [{ id: 1, name: "robin" }, { id: 2, name: "regis" }]);
},
test(assert) {
expandSelectBox();
selectBox().keyboard.down();
andThen(() => {
2017-10-19 15:51:08 -04:00
assert.equal(selectBox().highlightedRow.title(), "regis", "the next row is highlighted");
});
selectBox().keyboard.down();
andThen(() => {
2017-10-19 15:51:08 -04:00
assert.equal(selectBox().highlightedRow.title(), "robin", "it returns to the first row");
});
selectBox().keyboard.up();
andThen(() => {
2017-10-19 15:51:08 -04:00
assert.equal(selectBox().highlightedRow.title(), "regis", "it highlights the last row");
});
selectBox().keyboard.enter();
andThen(() => {
2017-10-19 15:51:08 -04:00
assert.equal(selectBox().selectedRow.title(), "regis", "it selects the row when pressing enter");
assert.notOk(selectBox().isExpanded, "it collapses the select box when selecting a row");
});
expandSelectBox();
selectBox().keyboard.escape();
andThen(() => {
assert.notOk(selectBox().isExpanded, "it collapses the select box");
});
expandSelectBox();
selectBoxFillInFilter("regis");
andThen(() => {
assert.equal(selectBox().highlightedRow.title(), "regis", "it highlights the first result");
});
selectBox().keyboard.tab();
andThen(() => {
assert.equal(selectBox().selectedRow.title(), "regis", "it selects the row when pressing tab");
assert.notOk(selectBox().isExpanded, "it collapses the select box when selecting a row");
});
}
});