forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

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