|
| 1 | +import { describe, expect, test, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { render, screen, act, fireEvent } from "@testing-library/react"; |
| 3 | + |
| 4 | +// vi.hoisted ensures these are initialized before vi.mock hoists its factory. |
| 5 | +const { mockReplace, navState } = vi.hoisted(() => ({ |
| 6 | + mockReplace: vi.fn(), |
| 7 | + navState: { q: "" as string }, |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock("next/navigation", () => ({ |
| 11 | + useRouter: () => ({ replace: mockReplace }), |
| 12 | + usePathname: () => "/", |
| 13 | + useSearchParams: () => new URLSearchParams(navState.q ? `q=${navState.q}` : ""), |
| 14 | +})); |
| 15 | + |
| 16 | +const { SearchBox } = await import("../search-box"); |
| 17 | + |
| 18 | +describe("SearchBox — typing-ahead race condition", () => { |
| 19 | + beforeEach(() => { |
| 20 | + vi.useFakeTimers(); |
| 21 | + mockReplace.mockClear(); |
| 22 | + navState.q = ""; |
| 23 | + }); |
| 24 | + afterEach(() => { |
| 25 | + vi.useRealTimers(); |
| 26 | + }); |
| 27 | + |
| 28 | + test("preserves in-progress input when a navigation resolves mid-typing", () => { |
| 29 | + // Reproduces the race: user types "foo" → debounce fires → user types |
| 30 | + // more → navigation for "foo" resolves → input must NOT reset to "foo". |
| 31 | + const { rerender } = render(<SearchBox />); |
| 32 | + const input = screen.getByRole("textbox"); |
| 33 | + |
| 34 | + fireEvent.change(input, { target: { value: "foo" } }); |
| 35 | + |
| 36 | + // Debounce fires; typingAhead becomes false. |
| 37 | + act(() => { vi.advanceTimersByTime(300); }); |
| 38 | + expect(mockReplace).toHaveBeenCalledOnce(); |
| 39 | + |
| 40 | + // User types more before the navigation resolves. |
| 41 | + fireEvent.change(input, { target: { value: "foobar" } }); |
| 42 | + |
| 43 | + // Navigation for "foo" resolves — URL now reports "foo". |
| 44 | + navState.q = "foo"; |
| 45 | + rerender(<SearchBox />); |
| 46 | + |
| 47 | + // typingAhead is true, so the sync effect must NOT overwrite the input. |
| 48 | + expect(input).toHaveValue("foobar"); |
| 49 | + }); |
| 50 | + |
| 51 | + test("syncs from URL when the user is not typing (external navigation)", () => { |
| 52 | + // Back/forward nav or a tag click should still update the input when the |
| 53 | + // user hasn't typed anything since the last URL write. |
| 54 | + const { rerender } = render(<SearchBox />); |
| 55 | + const input = screen.getByRole("textbox"); |
| 56 | + |
| 57 | + navState.q = "tag:alpha"; |
| 58 | + rerender(<SearchBox />); |
| 59 | + |
| 60 | + expect(input).toHaveValue("tag:alpha"); |
| 61 | + }); |
| 62 | + |
| 63 | + test("clears the input when the URL is cleared externally", () => { |
| 64 | + navState.q = "foo"; |
| 65 | + const { rerender } = render(<SearchBox />); |
| 66 | + const input = screen.getByRole("textbox"); |
| 67 | + |
| 68 | + expect(input).toHaveValue("foo"); |
| 69 | + |
| 70 | + navState.q = ""; |
| 71 | + rerender(<SearchBox />); |
| 72 | + |
| 73 | + expect(input).toHaveValue(""); |
| 74 | + }); |
| 75 | +}); |
0 commit comments