|
| 1 | +import { EnvironmentId } from "@t3tools/contracts"; |
| 2 | +import { Atom, AtomRegistry } from "effect/reactivity"; |
| 3 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 4 | + |
| 5 | +const fixture = vi.hoisted(() => ({ |
| 6 | + appState: "active", |
| 7 | + appStateListeners: new Set<() => void>(), |
| 8 | + refreshed: [] as string[], |
| 9 | +})); |
| 10 | + |
| 11 | +vi.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 | + |
| 24 | +vi.mock("../state/atom-registry", async () => { |
| 25 | + const { AtomRegistry } = await import("effect/reactivity"); |
| 26 | + return { appAtomRegistry: AtomRegistry.make() }; |
| 27 | +}); |
| 28 | + |
| 29 | +vi.mock("../connection/catalog", async () => { |
| 30 | + const { Atom } = await import("effect/reactivity"); |
| 31 | + return { environmentCatalog: { catalogValueAtom: Atom.make({ isReady: false }) } }; |
| 32 | +}); |
| 33 | + |
| 34 | +vi.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 | + |
| 41 | +vi.mock("../state/server", () => ({ |
| 42 | + serverEnvironment: { refreshProviders: { label: "Refresh providers" } }, |
| 43 | +})); |
| 44 | + |
| 45 | +vi.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 | + |
| 56 | +import { environmentCatalog } from "../connection/catalog"; |
| 57 | +import { appAtomRegistry } from "../state/atom-registry"; |
| 58 | +import { environmentPresentations } from "../state/presentation"; |
| 59 | +import { installSubscriptionUsageRefresh } from "./useSubscriptionUsage"; |
| 60 | + |
| 61 | +const registry = appAtomRegistry as AtomRegistry.AtomRegistry; |
| 62 | +const catalogAtom = environmentCatalog.catalogValueAtom as unknown as Atom.Writable<{ |
| 63 | + isReady: boolean; |
| 64 | +}>; |
| 65 | +const presentationsAtom = environmentPresentations.presentationsAtom as unknown as Atom.Writable< |
| 66 | + Map<string, { connection: { phase: string } }> |
| 67 | +>; |
| 68 | + |
| 69 | +const connected = EnvironmentId.make("env-connected"); |
| 70 | +const connecting = EnvironmentId.make("env-connecting"); |
| 71 | + |
| 72 | +describe("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 | +}); |
0 commit comments