forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

searchable-select.test.tsx128 lines, 4.4 KiBHistoryRawDownload
1import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library";
2import { afterEach, expect, it, vi } from "vitest";
3import { SearchableSelect } from "@/components/ui/searchable-select";
4import { PreferencesProvider } from "@/stores/preferences-context";
5
6const OPTIONS = [
7 { value: "m", label: "9-A · Matematik" },
8 { value: "t", label: "10-B · Türkçe" },
9];
10
11afterEach(cleanup);
12
13function renderSelect(onChange = vi.fn()) {
14 render(() => (
15 <PreferencesProvider>
16 <SearchableSelect value="" onChange={onChange} options={OPTIONS} placeholder="Seç" />
17 </PreferencesProvider>
18 ));
19 return { input: screen.getByRole("combobox"), onChange };
20}
21
22it("1. opens the option list on a plain click on the field", async () => {
23 const { input } = renderSelect();
24
25 // No typing, no chevron: the field itself is the affordance.
26 fireEvent.click(input);
27
28 await waitFor(() => expect(screen.getByRole("option", { name: "9-A · Matematik" })).toBeTruthy());
29 expect(screen.getByRole("option", { name: "10-B · Türkçe" })).toBeTruthy();
30});
31
32it("2. closes again when the open field is clicked", async () => {
33 const { input } = renderSelect();
34
35 fireEvent.click(input);
36 await screen.findByRole("option", { name: "9-A · Matematik" });
37
38 fireEvent.click(input);
39
40 await waitFor(() => expect(screen.queryByRole("option")).toBeNull());
41});
42
43it("3. reports the picked value and stays closed after the pick", async () => {
44 const { input, onChange } = renderSelect();
45
46 fireEvent.click(input);
47 fireEvent.click(await screen.findByRole("option", { name: "10-B · Türkçe" }));
48
49 await waitFor(() => expect(onChange).toHaveBeenCalledWith("t"));
50 // The close → refocus dance must not re-open the list by itself.
51 await waitFor(() => expect(screen.queryByRole("option")).toBeNull());
52});
53
54it("4. closes when clicking outside the field", async () => {
55 const { input } = renderSelect();
56
57 fireEvent.click(input);
58 await screen.findByRole("option", { name: "9-A · Matematik" });
59
60 // Kobalte attaches its document-level outside listener one macrotask after
61 // it opens; a real click always lands long after that.
62 const tick = Promise.withResolvers<void>();
63 setTimeout(tick.resolve, 0);
64 await tick.promise;
65 fireEvent.pointerDown(document.body);
66
67 await waitFor(() => expect(screen.queryByRole("option")).toBeNull());
68});
69
70it("5. closes on Escape without changing the value", async () => {
71 const { input, onChange } = renderSelect();
72
73 fireEvent.click(input);
74 await screen.findByRole("option", { name: "9-A · Matematik" });
75
76 fireEvent.keyDown(input, { key: "Escape" });
77
78 await waitFor(() => expect(screen.queryByRole("option")).toBeNull());
79 expect(onChange).not.toHaveBeenCalled();
80});
81
82it("6. opens on ArrowDown from the closed field", async () => {
83 const { input } = renderSelect();
84
85 fireEvent.keyDown(input, { key: "ArrowDown" });
86
87 await waitFor(() => expect(screen.getByRole("option", { name: "9-A · Matematik" })).toBeTruthy());
88});
89
90it("7. chevron toggles the list open and closed", async () => {
91 const { onChange } = renderSelect();
92
93 // Opened from the chevron, not the field: the trigger must still open.
94 // Kobalte toggles on pointerdown (a bare click event never reaches it).
95 const trigger = screen.getByRole("button");
96 fireEvent.pointerDown(trigger, { pointerType: "mouse", button: 0 });
97 await screen.findByRole("option", { name: "9-A · Matematik" });
98
99 fireEvent.pointerDown(trigger, { pointerType: "mouse", button: 0 });
100
101 await waitFor(() => expect(screen.queryByRole("option")).toBeNull());
102 expect(onChange).not.toHaveBeenCalled();
103});
104
105it("finds an option across a dash and word order", async () => {
106 const { input } = renderSelect();
107
108 fireEvent.input(input, { target: { value: "9a matematik" } });
109
110 await waitFor(() => expect(screen.getByRole("option", { name: "9-A · Matematik" })).toBeTruthy());
111 expect(screen.queryByRole("option", { name: "10-B · Türkçe" })).toBeNull();
112});
113
114it("finds an option typed without its Turkish diacritics", async () => {
115 const { input } = renderSelect();
116
117 fireEvent.input(input, { target: { value: "turkce" } });
118
119 await waitFor(() => expect(screen.getByRole("option", { name: "10-B · Türkçe" })).toBeTruthy());
120});
121
122it("says so when the search matches nothing", async () => {
123 const { input } = renderSelect();
124
125 fireEvent.input(input, { target: { value: "fizik" } });
126
127 await waitFor(() => expect(screen.getByText("No results.")).toBeTruthy());
128});