Skip to content

Commit d4d2e99

Browse files
committed
maint(pat auto suggest): Improve and extend tests.
1 parent 8d289eb commit d4d2e99

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

src/pat/auto-suggest/auto-suggest.test.js

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ describe("pat-autosuggest", function () {
4848
jest.restoreAllMocks();
4949
});
5050

51-
describe("An ordinary <input> element", function () {
52-
it("gets converted into a select2 widget", async function () {
51+
describe("1 - Basic tests", function () {
52+
it("1.1 - An ordinary <input> element gets converted into a select2 widget", async function () {
5353
testutils.createInputElement();
5454
var $el = $("input.pat-autosuggest");
5555

@@ -67,10 +67,8 @@ describe("pat-autosuggest", function () {
6767
expect($el[0].getAttribute("type")).toBe("text");
6868
expect($el[0].hasAttribute("hidden")).toBe(true);
6969
});
70-
});
7170

72-
describe("An <input> element with an ajax option", function () {
73-
it("keeps the ajax option when turning into a select2 widget", async function () {
71+
it("1.1 - An <input> element with an ajax option keeps the ajax option when turning into a select2 widget", async function () {
7472
testutils.createInputElement({
7573
data: "ajax-url: http://test.org/test",
7674
});
@@ -82,10 +80,8 @@ describe("pat-autosuggest", function () {
8280
expect($el.select2.mock.calls.pop()[0].ajax).toBeDefined();
8381
testutils.removeSelect2();
8482
});
85-
});
8683

87-
describe("A <select> element", function () {
88-
it("gets converted into a select2 widget", async function () {
84+
it("1.3 - A <select> element gets converted into a select2 widget", async function () {
8985
testutils.createSelectElement();
9086
var $el = $("select.pat-autosuggest");
9187
expect($(".select2-container").length).toBe(0);
@@ -98,11 +94,10 @@ describe("pat-autosuggest", function () {
9894
});
9995
});
10096

101-
describe("Selected items", function () {
102-
it("can be given custom CSS classes", async function () {
97+
describe("2.1 - Selected items", function () {
98+
it("4.1 - can be given custom CSS classes", async function () {
10399
testutils.createInputElement({
104-
data:
105-
'words: apple,orange,pear; pre-fill: orange; selection-classes: {"orange": ["fruit", "orange"]}',
100+
data: 'words: apple,orange,pear; pre-fill: orange; selection-classes: {"orange": ["fruit", "orange"]}',
106101
});
107102
var $el = $("input.pat-autosuggest");
108103
expect($(".select2-search-choice").length).toBe(0);
@@ -114,7 +109,7 @@ describe("pat-autosuggest", function () {
114109
testutils.removeSelect2();
115110
});
116111

117-
it("can be restricted to a certain amount", async function () {
112+
it("2.2 - can be restricted to a certain amount", async function () {
118113
// First check without limit
119114
testutils.createInputElement({
120115
data: "words: apple,orange,pear; pre-fill: orange",
@@ -130,8 +125,7 @@ describe("pat-autosuggest", function () {
130125

131126
// Then with limit
132127
testutils.createInputElement({
133-
data:
134-
"maximum-selection-size: 1; words: apple,orange,pear; pre-fill: orange",
128+
data: "maximum-selection-size: 1; words: apple,orange,pear; pre-fill: orange",
135129
});
136130
expect($(".select2-input").length).toBe(0);
137131
pattern.init($("input.pat-autosuggest"));
@@ -143,10 +137,53 @@ describe("pat-autosuggest", function () {
143137
expect($(".select2-search-choice-close").length).toBe(1);
144138
testutils.removeSelect2();
145139
});
140+
141+
it("2.3 - select an item from a word list.", async function () {
142+
document.body.innerHTML = `
143+
<input
144+
type="text"
145+
class="pat-autosuggest"
146+
data-pat-autosuggest="words: apple, orange, pear" />
147+
`;
148+
149+
const input = document.querySelector("input");
150+
new pattern(input);
151+
await utils.timeout(1); // wait a tick for async to settle.
152+
$(".select2-input").click();
153+
$(document.querySelector(".select2-result")).mouseup();
154+
155+
const selected = document.querySelectorAll(".select2-search-choice");
156+
expect(selected.length).toBe(1);
157+
expect(selected[0].textContent.trim()).toBe("apple");
158+
expect(input.value).toBe("apple");
159+
});
160+
161+
it("2.4 - select multiple items from a word list.", async function () {
162+
document.body.innerHTML = `
163+
<input
164+
type="text"
165+
class="pat-autosuggest"
166+
data-pat-autosuggest="words: apple, orange, pear" />
167+
`;
168+
169+
const input = document.querySelector("input");
170+
new pattern(input);
171+
await utils.timeout(1); // wait a tick for async to settle.
172+
$(".select2-input").click();
173+
$(document.querySelector(".select2-result")).mouseup();
174+
$(".select2-input").click();
175+
$(document.querySelector(".select2-result")).mouseup();
176+
177+
const selected = document.querySelectorAll(".select2-search-choice");
178+
expect(selected.length).toBe(2);
179+
expect(selected[0].textContent.trim()).toBe("apple");
180+
expect(selected[1].textContent.trim()).toBe("orange");
181+
expect(input.value).toBe("apple,orange");
182+
});
146183
});
147184

148-
describe("Placeholder tests", function () {
149-
it("A placeholder on the original element is reused on the auto-suggest element", async function () {
185+
describe("3 - Placeholder tests", function () {
186+
it("3.1 - A placeholder on the original element is reused on the auto-suggest element", async function () {
150187
var placeholder = "Test placeholder";
151188

152189
$("<input/>", {
@@ -166,7 +203,7 @@ describe("pat-autosuggest", function () {
166203
testutils.removeSelect2();
167204
});
168205

169-
it("No placeholder doesn't create an automatic one.", async function () {
206+
it("3.2 - No placeholder doesn't create an automatic one.", async function () {
170207
$("<input/>", {
171208
id: "select2",
172209
class: "pat-autosuggest",

0 commit comments

Comments
 (0)