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

useSubscriptionUsage.test.ts

97 lines3.2 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { Atom, AtomRegistry } from "effect/reactivity";
3import { describe, expect, it, vi } from "vite-plus/test";
4​
5const fixture = vi.hoisted(() => ({
6 appState: "active",
7 appStateListeners: new Set<() => void>(),
8 refreshed: [] as string[],
9}));
10​
11vi.mock("react-native", () => ({
12 AppState: {
13 get currentState() {
14 return fixture.appState;
15 },
16 addEventListener: (_event: string, listener: () => void) => {
17 fixture.appStateListeners.add(listener);
18 return { remove: () => fixture.appStateListeners.delete(listener) };
19 },
20 },
21 AccessibilityInfo: { addEventListener: vi.fn(), isReduceMotionEnabled: vi.fn() },
22}));
23​
24vi.mock("../state/atom-registry", async () => {
25 const { AtomRegistry } = await import("effect/reactivity");
26 return { appAtomRegistry: AtomRegistry.make() };
27});
28​
29vi.mock("../connection/catalog", async () => {
30 const { Atom } = await import("effect/reactivity");
31 return { environmentCatalog: { catalogValueAtom: Atom.make({ isReady: false }) } };
32});
33​
34vi.mock("../state/presentation", async () => {
35 const { Atom } = await import("effect/reactivity");
36 return {
37 environmentPresentations: { presentationsAtom: Atom.make(new Map<string, unknown>()) },
38 };
39});
40​
41vi.mock("../state/server", () => ({
42 serverEnvironment: { refreshProviders: { label: "Refresh providers" } },
43}));
44​
45vi.mock("@t3tools/client-runtime/state/runtime", () => ({
46 runAtomCommand: (
47 _registry: unknown,
48 _command: unknown,
49 input: { readonly environmentId: string },
50 ) => {
51 fixture.refreshed.push(input.environmentId);
52 return Promise.resolve();
53 },
54}));
55​
56import { environmentCatalog } from "../connection/catalog";
57import { appAtomRegistry } from "../state/atom-registry";
58import { environmentPresentations } from "../state/presentation";
59import { installSubscriptionUsageRefresh } from "./useSubscriptionUsage";
60​
61const registry = appAtomRegistry as AtomRegistry.AtomRegistry;
62const catalogAtom = environmentCatalog.catalogValueAtom as unknown as Atom.Writable<{
63 isReady: boolean;
64}>;
65const presentationsAtom = environmentPresentations.presentationsAtom as unknown as Atom.Writable<
66 Map<string, { connection: { phase: string } }>
67>;
68​
69const connected = EnvironmentId.make("env-connected");
70const connecting = EnvironmentId.make("env-connecting");
71​
72describe("installSubscriptionUsageRefresh", () => {
73 it("refreshes connected environments once the catalog is ready and the app is active", () => {
74 fixture.appState = "background";
75 fixture.refreshed = [];
76 registry.set(catalogAtom, { isReady: false });
77 registry.set(
78 presentationsAtom,
79 new Map([
80 [connected, { connection: { phase: "connected" } }],
81 [connecting, { connection: { phase: "connecting" } }],
82 ]),
83 );
84​
85 const dispose = installSubscriptionUsageRefresh();
86 registry.set(catalogAtom, { isReady: true });
87 // Backgrounded: nothing refreshes yet.
88 expect(fixture.refreshed).toEqual([]);
89​
90 fixture.appState = "active";
91 for (const listener of fixture.appStateListeners) listener();
92 expect(fixture.refreshed).toEqual([connected]);
93​
94 dispose();
95 expect(fixture.appStateListeners.size).toBe(0);
96 });
97});