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

query.test.ts

38 lines1.4 KB

1import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
2import { describe, expect, it, vi } from "vite-plus/test";
3​
4import { environmentQueryDataAtom } from "./query";
5​
6interface Metadata {
7 readonly kind: "file" | "directory";
8 readonly byteLength?: number;
9}
10​
11describe("environmentQueryDataAtom", () => {
12 it("holds through a refetch that brings the same data back, and passes a change on", () => {
13 const loaded = AsyncResult.success<Metadata>(
14 { kind: "file", byteLength: 12 },
15 { timestamp: 1 },
16 );
17 const query = Atom.make<AsyncResult.AsyncResult<Metadata, Error>>(loaded);
18 const data = environmentQueryDataAtom(query);
19 expect(environmentQueryDataAtom(query)).toBe(data);
20​
21 const registry = AtomRegistry.make();
22 const heard = vi.fn<(value: Metadata | null) => void>();
23 const unsubscribe = registry.subscribe(data, heard);
24 const first = registry.get(data);
25 expect(first).toEqual({ kind: "file", byteLength: 12 });
26​
27 registry.set(query, AsyncResult.waiting(loaded));
28 registry.set(query, AsyncResult.success({ kind: "file", byteLength: 12 }, { timestamp: 2 }));
29 expect(registry.get(data)).toBe(first);
30 expect(heard).not.toHaveBeenCalled();
31​
32 registry.set(query, AsyncResult.success({ kind: "file", byteLength: 13 }, { timestamp: 3 }));
33 expect(heard).toHaveBeenCalledExactlyOnceWith({ kind: "file", byteLength: 13 });
34​
35 unsubscribe();
36 registry.dispose();
37 });
38});