|
| 1 | +import { getClient as baseGetClient } from "../actions/get-client.js"; |
| 2 | +import { defineConfig } from "../config.js"; |
| 3 | +import { whenClientChanged } from "./when-client-changed.js"; |
| 4 | +import { firstValueFrom } from "rxjs"; |
| 5 | +import { afterEach, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +vi.mock("../actions/get-client.js"); |
| 8 | + |
| 9 | +const config = defineConfig({ |
| 10 | + chains: { test: { descriptor: {}, provider: {} } as never }, |
| 11 | +}); |
| 12 | + |
| 13 | +afterEach(() => { |
| 14 | + vi.clearAllMocks(); |
| 15 | +}); |
| 16 | + |
| 17 | +it("defers getClient until subscribing", async () => { |
| 18 | + const mockClient = { id: "test-client" }; |
| 19 | + vi.mocked(baseGetClient).mockResolvedValue(mockClient as never); |
| 20 | + |
| 21 | + const observable = whenClientChanged(config, { chainId: "test" }); |
| 22 | + expect(baseGetClient).not.toHaveBeenCalled(); |
| 23 | + |
| 24 | + const result = await firstValueFrom(observable); |
| 25 | + expect(baseGetClient).toHaveBeenCalledTimes(1); |
| 26 | + expect(result).toEqual(mockClient); |
| 27 | +}); |
| 28 | + |
| 29 | +it("re-invokes getClient for each subscription", async () => { |
| 30 | + const mockClient = { id: "test-client" }; |
| 31 | + vi.mocked(baseGetClient).mockResolvedValue(mockClient as never); |
| 32 | + |
| 33 | + const observable = whenClientChanged(config, { chainId: "test" }); |
| 34 | + |
| 35 | + const promise1 = firstValueFrom(observable); |
| 36 | + const promise2 = firstValueFrom(observable); |
| 37 | + |
| 38 | + await Promise.all([promise1, promise2]); |
| 39 | + |
| 40 | + expect(baseGetClient).toHaveBeenCalledTimes(2); |
| 41 | +}); |
0 commit comments