Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useDirectoryEntries.browser.test.tsx

66 lines2.2 KB

1import { RegistryContext } from "@effect/atom-react";
2import { EnvironmentId } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import { AsyncResult, Atom } from "effect/reactivity";
5import { describe, expect, it, vi } from "vite-plus/test";
6​
7import { appAtomRegistry } from "~/rpc/atomRegistry";
8import { act, render } from "../../../test/render";
9import { useDirectoryEntries } from "./useDirectoryEntries";
10​
11const mocks = vi.hoisted(() => ({
12 reachable: null as unknown as Atom.Writable<boolean>,
13 connectedSession: null as unknown as Atom.Writable<number>,
14}));
15​
16vi.mock("~/connection/catalog", () => ({
17 // Derived like the real one, which only follows the connection once something computes it.
18 environmentCatalog: {
19 sessionGenerationAtom: () => Atom.make((get) => get(mocks.connectedSession)),
20 },
21}));
22vi.mock("~/state/projects", () => ({
23 projectEnvironment: {
24 listEntries: () =>
25 Atom.make((get) =>
26 get(mocks.reachable)
27 ? AsyncResult.success({
28 entries: [{ path: "README.md", kind: "file" }],
29 truncated: false,
30 })
31 : AsyncResult.failure(Cause.fail(new Error("Environment is reconnecting."))),
32 ),
33 },
34}));
35​
36function setup() {
37 mocks.reachable = Atom.make(false);
38 mocks.connectedSession = Atom.make(1);
40​
41function Probe() {
42 const directory = useDirectoryEntries(EnvironmentId.make("env-files"), "/repo");
43 return (
44 <output>{directory.error ?? directory.entries.map((entry) => entry.path).join(",")}</output>
45 );
47​
48describe("useDirectoryEntries", () => {
49 it("reads a folder that failed while unreachable again once a new session is up", async () => {
50 setup();
51 await using screen = await render(
52 <RegistryContext.Provider value={appAtomRegistry}>
53 <Probe />
54 </RegistryContext.Provider>,
55 // A bare hook probe; nothing here navigates.
56 { router: false },
57 );
58 await expect.element(screen.getByText("Environment is reconnecting.")).toBeInTheDocument();
59​
60 await act(() => {
61 appAtomRegistry.set(mocks.reachable, true);
62 appAtomRegistry.set(mocks.connectedSession, 2);
63 });
64 await expect.element(screen.getByText("README.md")).toBeInTheDocument();
65 });
66});