Skip to content

Commit 21d3dc6

Browse files
Add unit test for autocomplete component
1 parent a8ca2c9 commit 21d3dc6

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { Autocomplete } from "./autocomplete.js";
2+
3+
document.body.classList.add("nhsuk-frontend-supported");
4+
document.body.innerHTML = `
5+
<select id="fruit" name="fruit" data-module="app-autocomplete">
6+
<option value=""></option>
7+
<option data-hint="Red" data-append="Sliced">Apple</option>
8+
<option data-hint="Yellow" data-append="Peeled">Banana</option>
9+
<option data-hint="Orange" data-append="Squeezed">Orange</option>
10+
</select>
11+
`;
12+
13+
describe("Autocomplete", () => {
14+
beforeAll(() => {
15+
const $select = document.querySelector('[data-module="app-autocomplete"]');
16+
return new Autocomplete($select);
17+
});
18+
19+
test("should enhance select", () => {
20+
const select = document.querySelector("select");
21+
expect(window.getComputedStyle(select).display).toBe("none");
22+
23+
const autocomplete = document.querySelector(".app-autocomplete__wrapper");
24+
expect(autocomplete).toBeTruthy();
25+
26+
const input = document.querySelector(".app-autocomplete__wrapper input");
27+
expect(input.getAttribute("aria-controls")).toEqual("fruit__listbox");
28+
});
29+
30+
test("should show matching options when a value is entered", async () => {
31+
const input = document.querySelector('[aria-controls="fruit__listbox"]');
32+
const listbox = document.querySelector("#fruit__listbox");
33+
34+
// Initially, the listbox should be hidden
35+
expect(listbox.classList).toContain("app-autocomplete__menu--hidden");
36+
37+
// Simulate typing "a" in the input
38+
input.focus();
39+
input.value = "a";
40+
input.dispatchEvent(new Event("input", { bubbles: true }));
41+
await new Promise((resolve) => setTimeout(resolve, 100));
42+
43+
// The listbox should now be visible
44+
expect(listbox.classList).toContain("app-autocomplete__menu--visible");
45+
46+
// Check that matching options are shown
47+
const visibleOptions = listbox.querySelectorAll("li");
48+
expect(visibleOptions.length).toEqual(3);
49+
50+
// Check that options display hint and appended text
51+
expect(visibleOptions[0].innerHTML.trim()).toEqual(
52+
`Apple – Sliced<br><span class="app-autocomplete__option-hint">Red</span>`,
53+
);
54+
55+
// Simulate clicking first option
56+
visibleOptions[0].click();
57+
await new Promise((resolve) => setTimeout(resolve, 50));
58+
59+
// Check that selected options is saved
60+
expect(input.value).toBe("Apple");
61+
});
62+
63+
test("should shows a message if no values found", async () => {
64+
const input = document.querySelector('[aria-controls="fruit__listbox"]');
65+
const listbox = document.querySelector("#fruit__listbox");
66+
67+
// Simulate typing "z" in the input
68+
input.focus();
69+
input.value = "z";
70+
input.dispatchEvent(new Event("input", { bubbles: true }));
71+
await new Promise((resolve) => setTimeout(resolve, 100));
72+
73+
// Check that matching options are shown
74+
const visibleOptions = listbox.querySelectorAll("li");
75+
console.log(visibleOptions[0].innerHTML.trim());
76+
expect(visibleOptions.length).toEqual(1);
77+
78+
// Option display hint text
79+
expect(visibleOptions[0].innerHTML.trim()).toEqual("No results found");
80+
});
81+
});

0 commit comments

Comments
 (0)