|
| 1 | +import { AuthFilesystemReadScope, EnvironmentId } from "@t3tools/contracts"; |
| 2 | +import { act, useLayoutEffect } from "react"; |
| 3 | +import { createRoot } from "react-dom/client"; |
| 4 | +import { expect, it, vi } from "vite-plus/test"; |
| 5 | + |
| 6 | +const mocks = vi.hoisted(() => ({ |
| 7 | + sessionAtom: {}, |
| 8 | + searchEntries: vi.fn(() => null), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("./session", () => ({ |
| 12 | + environmentSession: { sessionStateAtom: () => mocks.sessionAtom }, |
| 13 | +})); |
| 14 | +vi.mock("./presentation", () => ({ |
| 15 | + useEnvironmentPresentation: () => ({ |
| 16 | + isReady: true, |
| 17 | + presentation: { connection: { phase: "connected", error: null } }, |
| 18 | + }), |
| 19 | +})); |
| 20 | +vi.mock("./pull-requests", () => ({ composerPullRequests: {} })); |
| 21 | +vi.mock("./projects", () => ({ |
| 22 | + projectEnvironment: { searchEntries: mocks.searchEntries }, |
| 23 | +})); |
| 24 | +vi.mock("./atom-registry", () => ({ appAtomRegistry: {} })); |
| 25 | +vi.mock("./orchestration", () => ({ orchestrationEnvironment: {} })); |
| 26 | +vi.mock("./vcs", () => ({ vcsEnvironment: {} })); |
| 27 | +vi.mock("./query", () => ({ |
| 28 | + useEnvironmentQuery: (atom: unknown) => ({ |
| 29 | + data: |
| 30 | + atom === mocks.sessionAtom |
| 31 | + ? { authenticated: true, scopes: [AuthFilesystemReadScope] } |
| 32 | + : null, |
| 33 | + error: null, |
| 34 | + isPending: false, |
| 35 | + refresh: () => {}, |
| 36 | + }), |
| 37 | +})); |
| 38 | + |
| 39 | +import { useComposerPathSearch, type ComposerPathSearchTarget } from "./queries"; |
| 40 | + |
| 41 | +function setup() { |
| 42 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 43 | + vi.useFakeTimers(); |
| 44 | + mocks.searchEntries.mockClear(); |
| 45 | + const root = createRoot(document.createElement("div")); |
| 46 | + let commits = 0; |
| 47 | + let isPending = false; |
| 48 | + |
| 49 | + function Probe({ target }: { readonly target: ComposerPathSearchTarget }) { |
| 50 | + const result = useComposerPathSearch(target); |
| 51 | + useLayoutEffect(() => { |
| 52 | + commits += 1; |
| 53 | + isPending = result.isPending; |
| 54 | + }); |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + return { |
| 59 | + render(query: string | null) { |
| 60 | + act(() => |
| 61 | + root.render( |
| 62 | + <Probe |
| 63 | + target={{ environmentId: EnvironmentId.make("test-environment"), cwd: "/repo", query }} |
| 64 | + />, |
| 65 | + ), |
| 66 | + ); |
| 67 | + }, |
| 68 | + get commits() { |
| 69 | + return commits; |
| 70 | + }, |
| 71 | + get isPending() { |
| 72 | + return isPending; |
| 73 | + }, |
| 74 | + [Symbol.dispose]() { |
| 75 | + act(() => root.unmount()); |
| 76 | + vi.useRealTimers(); |
| 77 | + }, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +it.each([null, "src"])("stops rendering after the path target settles (%s)", (query) => { |
| 82 | + using harness = setup(); |
| 83 | + harness.render(query); |
| 84 | + // A parent render before the debounce fires used to start a self-sustaining loop. |
| 85 | + harness.render(query); |
| 86 | + act(() => vi.advanceTimersByTime(200)); |
| 87 | + const settledCommits = harness.commits; |
| 88 | + |
| 89 | + act(() => vi.advanceTimersByTime(200)); |
| 90 | + expect(harness.commits).toBe(settledCommits); |
| 91 | + act(() => vi.advanceTimersByTime(2_000)); |
| 92 | + expect(harness.commits).toBe(settledCommits); |
| 93 | + expect(harness.isPending).toBe(false); |
| 94 | + if (query === null) expect(mocks.searchEntries).not.toHaveBeenCalled(); |
| 95 | +}); |
| 96 | + |
| 97 | +it("still debounces a changed path query before searching", () => { |
| 98 | + using harness = setup(); |
| 99 | + harness.render(null); |
| 100 | + harness.render(" src "); |
| 101 | + expect(harness.isPending).toBe(true); |
| 102 | + act(() => vi.advanceTimersByTime(199)); |
| 103 | + expect(mocks.searchEntries).not.toHaveBeenCalled(); |
| 104 | + |
| 105 | + act(() => vi.advanceTimersByTime(1)); |
| 106 | + expect(mocks.searchEntries).toHaveBeenLastCalledWith({ |
| 107 | + environmentId: EnvironmentId.make("test-environment"), |
| 108 | + input: { cwd: "/repo", query: "src", limit: 20 }, |
| 109 | + }); |
| 110 | + expect(harness.isPending).toBe(false); |
| 111 | +}); |
0 commit comments