|
1 | 1 | import $ from "jquery";
|
| 2 | +import events from "../../core/events"; |
2 | 3 | import pattern from "./auto-suggest";
|
3 | 4 | import utils from "../../core/utils";
|
4 | 5 | import registry from "../../core/registry";
|
@@ -95,7 +96,7 @@ describe("pat-autosuggest", function () {
|
95 | 96 | });
|
96 | 97 |
|
97 | 98 | describe("2.1 - Selected items", function () {
|
98 |
| - it("4.1 - can be given custom CSS classes", async function () { |
| 99 | + it("2.1 - can be given custom CSS classes", async function () { |
99 | 100 | testutils.createInputElement({
|
100 | 101 | data: 'words: apple,orange,pear; pre-fill: orange; selection-classes: {"orange": ["fruit", "orange"]}',
|
101 | 102 | });
|
@@ -180,6 +181,62 @@ describe("pat-autosuggest", function () {
|
180 | 181 | expect(selected[1].textContent.trim()).toBe("orange");
|
181 | 182 | expect(input.value).toBe("apple,orange");
|
182 | 183 | });
|
| 184 | + |
| 185 | + it("2.5 - items can be pre-filled with json.", async function () { |
| 186 | + // Check if json pre-filled fields do work as expected. |
| 187 | + // Version 7.0 introduced a problem where at json pre-filled fields |
| 188 | + // no items could be deleted or added. |
| 189 | + // Version 7.11 fixes that problem. |
| 190 | + |
| 191 | + document.body.innerHTML = ` |
| 192 | + <input |
| 193 | + type="text" |
| 194 | + class="pat-autosuggest" |
| 195 | + data-pat-autosuggest=' |
| 196 | + words-json: [ |
| 197 | + {"id": "id-apple", "text":"Apple"}, |
| 198 | + {"id": "id-orange", "text": "Orange"}, |
| 199 | + {"id": "id-lemon", "text":"Lemon"} |
| 200 | + ]; |
| 201 | + prefill-json: { |
| 202 | + "id-orange": "Orange" |
| 203 | + }; |
| 204 | + ' /> |
| 205 | + `; |
| 206 | + |
| 207 | + const input = document.querySelector("input"); |
| 208 | + new pattern(input); |
| 209 | + await utils.timeout(1); // wait a tick for async to settle. |
| 210 | + |
| 211 | + let selected = document.querySelectorAll(".select2-search-choice"); |
| 212 | + expect(selected.length).toBe(1); |
| 213 | + expect(selected[0].textContent.trim()).toBe("Orange"); |
| 214 | + expect(input.value).toBe("id-orange"); |
| 215 | + |
| 216 | + // NOTE: the keyboard event init key ``which`` is deprecated, |
| 217 | + // but that's what select2 3.5.1 is expecting. |
| 218 | + document |
| 219 | + .querySelector(".select2-input") |
| 220 | + .dispatchEvent(new KeyboardEvent("keydown", { which: 8 })); |
| 221 | + // Need to send two times. |
| 222 | + document |
| 223 | + .querySelector(".select2-input") |
| 224 | + .dispatchEvent(new KeyboardEvent("keydown", { which: 8 })); |
| 225 | + |
| 226 | + selected = document.querySelectorAll(".select2-search-choice"); |
| 227 | + expect(selected.length).toBe(0); |
| 228 | + |
| 229 | + document.querySelector(".select2-input").click(); |
| 230 | + document.querySelector(".select2-result").dispatchEvent(events.mouseup_event()); // prettier-ignore |
| 231 | + document.querySelector(".select2-input").click(); |
| 232 | + document.querySelector(".select2-result").dispatchEvent(events.mouseup_event()); // prettier-ignore |
| 233 | + |
| 234 | + selected = document.querySelectorAll(".select2-search-choice"); |
| 235 | + expect(selected.length).toBe(2); |
| 236 | + expect(selected[0].textContent.trim()).toBe("Apple"); |
| 237 | + expect(selected[1].textContent.trim()).toBe("Orange"); |
| 238 | + expect(input.value).toBe("id-apple,id-orange"); |
| 239 | + }); |
183 | 240 | });
|
184 | 241 |
|
185 | 242 | describe("3 - Placeholder tests", function () {
|
|
0 commit comments