| 1 | import { Atom, AtomRegistry } from "effect/reactivity"; |
| 2 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 3 | |
| 4 | import { waitForAtomValue } from "./waitForAtomValue"; |
| 5 | |
| 6 | describe("waitForAtomValue", () => { |
| 7 | it("returns immediately when the current value is ready", async () => { |
| 8 | const registry = AtomRegistry.make(); |
| 9 | const atom = Atom.make("ready"); |
| 10 | |
| 11 | await expect( |
| 12 | waitForAtomValue({ |
| 13 | registry, |
| 14 | atom, |
| 15 | predicate: (value) => value === "ready", |
| 16 | timeoutMs: 1_000, |
| 17 | }), |
| 18 | ).resolves.toBe(true); |
| 19 | |
| 20 | registry.dispose(); |
| 21 | }); |
| 22 | |
| 23 | it("waits until a subscribed atom becomes ready", async () => { |
| 24 | const registry = AtomRegistry.make(); |
| 25 | const atom = Atom.make("pending"); |
| 26 | const result = waitForAtomValue({ |
| 27 | registry, |
| 28 | atom, |
| 29 | predicate: (value) => value === "ready", |
| 30 | timeoutMs: 1_000, |
| 31 | }); |
| 32 | |
| 33 | registry.set(atom, "ready"); |
| 34 | |
| 35 | await expect(result).resolves.toBe(true); |
| 36 | registry.dispose(); |
| 37 | }); |
| 38 | |
| 39 | it("returns false and unsubscribes when readiness times out", async () => { |
| 40 | vi.useFakeTimers(); |
| 41 | const registry = AtomRegistry.make(); |
| 42 | const atom = Atom.make("pending"); |
| 43 | const result = waitForAtomValue({ |
| 44 | registry, |
| 45 | atom, |
| 46 | predicate: (value) => value === "ready", |
| 47 | timeoutMs: 1_000, |
| 48 | }); |
| 49 | |
| 50 | await vi.advanceTimersByTimeAsync(1_000); |
| 51 | |
| 52 | await expect(result).resolves.toBe(false); |
| 53 | registry.set(atom, "ready"); |
| 54 | registry.dispose(); |
| 55 | vi.useRealTimers(); |
| 56 | }); |
| 57 | }); |