hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { cleanup, fireEvent, render, screen, waitFor } from "@solidjs/testing-library"; |
| 2 | import { createSignal } from "solid-js"; |
| 3 | import { afterEach, expect, it, vi } from "vitest"; |
| 4 | import type { PersonRef } from "@/api/client"; |
| 5 | import { UserSearchSelect } from "@/components/users/user-search-select"; |
| 6 | import { PreferencesProvider } from "@/stores/preferences-context"; |
| 7 | |
| 8 | const getUserSearch = vi.fn(); |
| 9 | const getUserById = vi.fn(); |
| 10 | |
| 11 | vi.mock("@/stores/auth-context", () => ({ |
| 12 | useAuth: () => ({ user: () => ({ role: "manager" }) }), |
| 13 | })); |
| 14 | vi.mock("@/api/users", () => ({ |
| 15 | getUserById: (...args: unknown[]) => getUserById(...args), |
| 16 | getUserSearch: (...args: unknown[]) => getUserSearch(...args), |
| 17 | })); |
| 18 | |
| 19 | const ada: PersonRef = { id: "ada", username: "ada", display_name: "Ada Yılmaz" }; |
| 20 | const mehmet: PersonRef = { id: "mehmet", username: "mehmet", display_name: "Mehmet Kaya" }; |
| 21 | |
| 22 | afterEach(() => { |
| 23 | cleanup(); |
| 24 | getUserSearch.mockReset(); |
| 25 | getUserById.mockReset(); |
| 26 | }); |
| 27 | |
| 28 | it("clears a clicked user's search without clearing the selected user", async () => { |
| 29 | getUserSearch.mockImplementation(async (query: string) => ({ |
| 30 | items: query ? [mehmet] : [ada, mehmet], |
| 31 | })); |
| 32 | getUserById.mockResolvedValue(ada); |
| 33 | const [value, setValue] = createSignal(ada.id); |
| 34 | const onChange = vi.fn((next: string) => setValue(next)); |
| 35 | render(() => ( |
| 36 | <PreferencesProvider> |
| 37 | <UserSearchSelect id="student" value={value()} onChange={onChange} initialUser={ada} /> |
| 38 | </PreferencesProvider> |
| 39 | )); |
| 40 | const input = screen.getByRole("combobox") as HTMLInputElement; |
| 41 | |
| 42 | await waitFor(() => expect(input.value).toBe("Ada Yılmaz (ada)")); |
| 43 | fireEvent.click(input); |
| 44 | |
| 45 | expect(input.value).toBe(""); |
| 46 | expect(value()).toBe(ada.id); |
| 47 | expect(onChange).not.toHaveBeenCalled(); |
| 48 | await waitFor(() => expect(getUserSearch).toHaveBeenCalledWith("", expect.any(AbortSignal), undefined)); |
| 49 | await screen.findByRole("option", { name: /Mehmet Kaya/ }); |
| 50 | |
| 51 | fireEvent.input(input, { target: { value: "meh" } }); |
| 52 | await waitFor(() => expect(getUserSearch).toHaveBeenCalledWith("meh", expect.any(AbortSignal), undefined)); |
| 53 | fireEvent.click(input); |
| 54 | |
| 55 | expect(input.value).toBe(""); |
| 56 | expect(value()).toBe(ada.id); |
| 57 | expect(onChange).not.toHaveBeenCalled(); |
| 58 | await waitFor(() => expect(getUserSearch).toHaveBeenLastCalledWith("", expect.any(AbortSignal), undefined)); |
| 59 | |
| 60 | fireEvent.click(await screen.findByRole("option", { name: /Mehmet Kaya/ })); |
| 61 | await waitFor(() => expect(value()).toBe(mehmet.id)); |
| 62 | expect(onChange).toHaveBeenCalledExactlyOnceWith(mehmet.id); |
| 63 | }); |
| 64 | |
| 65 | it("clears the selected label when the chevron opens suggestions", async () => { |
| 66 | getUserSearch.mockResolvedValue({ items: [ada, mehmet] }); |
| 67 | getUserById.mockResolvedValue(ada); |
| 68 | const onChange = vi.fn(); |
| 69 | render(() => ( |
| 70 | <PreferencesProvider> |
| 71 | <UserSearchSelect id="student" value={ada.id} onChange={onChange} initialUser={ada} /> |
| 72 | </PreferencesProvider> |
| 73 | )); |
| 74 | const input = screen.getByRole("combobox") as HTMLInputElement; |
| 75 | await waitFor(() => expect(input.value).toBe("Ada Yılmaz (ada)")); |
| 76 | |
| 77 | fireEvent.pointerDown(screen.getByRole("button"), { pointerType: "mouse", button: 0 }); |
| 78 | |
| 79 | expect(input.value).toBe(""); |
| 80 | await screen.findByRole("option", { name: /Mehmet Kaya/ }); |
| 81 | expect(onChange).not.toHaveBeenCalled(); |
| 82 | }); |