|
| 1 | +import { RegistryContext } from "@effect/atom-react"; |
| 2 | +import { |
| 3 | + EnvironmentId, |
| 4 | + type PullRequestListInput, |
| 5 | + type PullRequestListResult, |
| 6 | +} from "@t3tools/contracts"; |
| 7 | +import { AtomRegistry } from "effect/reactivity"; |
| 8 | +import { act, useLayoutEffect } from "react"; |
| 9 | +import { createRoot } from "react-dom/client"; |
| 10 | +import { expect, it, vi } from "vite-plus/test"; |
| 11 | + |
| 12 | +const mocks = vi.hoisted(() => ({ failPage: false })); |
| 13 | + |
| 14 | +vi.mock("../../connection/catalog", async () => { |
| 15 | + const { Atom } = await import("effect/reactivity"); |
| 16 | + return { |
| 17 | + environmentCatalog: { |
| 18 | + sessionGenerationAtom: Atom.family((_environmentId: string) => Atom.make(0)), |
| 19 | + }, |
| 20 | + }; |
| 21 | +}); |
| 22 | + |
| 23 | +vi.mock("../../state/pull-requests", async () => { |
| 24 | + const { AsyncResult, Atom } = await import("effect/reactivity"); |
| 25 | + const Effect = await import("effect/Effect"); |
| 26 | + const page = (nextCursors: PullRequestListResult["nextCursors"]): PullRequestListResult => ({ |
| 27 | + entries: [], |
| 28 | + nextCursors, |
| 29 | + truncated: false, |
| 30 | + viewers: {}, |
| 31 | + providers: [], |
| 32 | + errors: [], |
| 33 | + }); |
| 34 | + const list = Atom.family((key: string) => { |
| 35 | + const target: { environmentId: string; input: PullRequestListInput } = JSON.parse(key); |
| 36 | + const response = page(target.input.cursors ? {} : { "github.com org/repo": "next" }); |
| 37 | + return Atom.make( |
| 38 | + Effect.suspend(() => { |
| 39 | + if (target.environmentId === "offline") |
| 40 | + return Effect.fail(new Error("Environment is not connected.")); |
| 41 | + if (target.input.cursors && mocks.failPage) return Effect.fail(new Error("Page failed.")); |
| 42 | + return Effect.succeed(response); |
| 43 | + }), |
| 44 | + ); |
| 45 | + }); |
| 46 | + const { pullRequestListHead } = await import("./pullRequestList"); |
| 47 | + const pages = Atom.family((_key: string) => |
| 48 | + Atom.make<ReturnType<typeof pullRequestListHead> | null>(null), |
| 49 | + ); |
| 50 | + return { |
| 51 | + pullRequestListPagesKey: (target: unknown) => JSON.stringify(target), |
| 52 | + pullRequestListPagesFamily: pages, |
| 53 | + pullRequestListStatsFamily: Atom.family((_key: string) => Atom.make(new Map())), |
| 54 | + composerPullRequests: { |
| 55 | + list: (target: unknown) => list(JSON.stringify(target)), |
| 56 | + listStats: Atom.family(() => Atom.make(AsyncResult.success({ stats: [] }))), |
| 57 | + invalidate: { |
| 58 | + run: async () => AsyncResult.success({}), |
| 59 | + }, |
| 60 | + }, |
| 61 | + }; |
| 62 | +}); |
| 63 | + |
| 64 | +import { usePullRequestList } from "./usePullRequestList"; |
| 65 | + |
| 66 | +function setup(ids: readonly string[]) { |
| 67 | + mocks.failPage = false; |
| 68 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 69 | + const registry = AtomRegistry.make(); |
| 70 | + const targets = ids.map((id) => ({ |
| 71 | + environmentId: EnvironmentId.make(id), |
| 72 | + input: { state: "open" as const }, |
| 73 | + })); |
| 74 | + let latest: ReturnType<typeof usePullRequestList>; |
| 75 | + function Probe() { |
| 76 | + const result = usePullRequestList(targets); |
| 77 | + useLayoutEffect(() => { |
| 78 | + latest = result; |
| 79 | + }); |
| 80 | + return null; |
| 81 | + } |
| 82 | + const root = createRoot(document.createElement("div")); |
| 83 | + act(() => { |
| 84 | + root.render( |
| 85 | + <RegistryContext.Provider value={registry}> |
| 86 | + <Probe /> |
| 87 | + </RegistryContext.Provider>, |
| 88 | + ); |
| 89 | + }); |
| 90 | + return { |
| 91 | + get latest() { |
| 92 | + return latest; |
| 93 | + }, |
| 94 | + [Symbol.dispose]() { |
| 95 | + act(() => root.unmount()); |
| 96 | + registry.dispose(); |
| 97 | + }, |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +it("keeps a refresh failure in the source errors and allows healthy pagination", async () => { |
| 102 | + using probe = setup(["healthy", "offline"]); |
| 103 | + await act(async () => probe.latest.onRefresh()); |
| 104 | + expect(probe.latest.data.errors.map((error) => error.message)).toEqual([ |
| 105 | + "Environment is not connected.", |
| 106 | + ]); |
| 107 | + expect(probe.latest.refreshing).toBe(false); |
| 108 | + expect(probe.latest.loadMoreError).toBeNull(); |
| 109 | + |
| 110 | + await act(async () => probe.latest.loadMore()); |
| 111 | + expect(probe.latest.continued).toBe(true); |
| 112 | + expect(probe.latest.hasMore).toBe(false); |
| 113 | + expect(probe.latest.loadMoreError).toBeNull(); |
| 114 | +}); |
| 115 | + |
| 116 | +it("keeps load-more failures retryable", async () => { |
| 117 | + using probe = setup(["healthy"]); |
| 118 | + await act(async () => probe.latest.onRefresh()); |
| 119 | + mocks.failPage = true; |
| 120 | + await act(async () => probe.latest.loadMore()); |
| 121 | + expect(probe.latest.loadMoreError).toBe("Page failed."); |
| 122 | + expect(probe.latest.hasMore).toBe(true); |
| 123 | + expect(probe.latest.continued).toBe(false); |
| 124 | + |
| 125 | + mocks.failPage = false; |
| 126 | + await act(async () => probe.latest.loadMore(true)); |
| 127 | + expect(probe.latest.loadMoreError).toBeNull(); |
| 128 | + expect(probe.latest.hasMore).toBe(false); |
| 129 | + expect(probe.latest.continued).toBe(true); |
| 130 | +}); |
0 commit comments