|
| 1 | +import type { LDEvaluationDetailTyped } from '@launchdarkly/js-client-sdk'; |
| 2 | + |
| 3 | +import type { LDVueClient } from '../../src/client/LDClient'; |
| 4 | + |
| 5 | +export interface MockControls { |
| 6 | + setBool: (v: boolean) => void; |
| 7 | + emitChange: (key: string) => void; |
| 8 | + handlerCount: (event: string) => number; |
| 9 | + emitInitStatus: (r: { status: string; error?: Error }) => void; |
| 10 | + emitContextChange: (c: unknown) => void; |
| 11 | + subscriberCount: () => number; |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * A controllable in-memory LDVueClient stand-in for component tests. Returns the client plus a |
| 16 | + * `controls` object used to drive events from tests. |
| 17 | + */ |
| 18 | +export function makeMockClient(initial?: { |
| 19 | + ready?: boolean; |
| 20 | + initializedState?: string; |
| 21 | + boolValue?: boolean; |
| 22 | +}): { client: LDVueClient; controls: MockControls } { |
| 23 | + let ready = initial?.ready ?? true; |
| 24 | + let initializedState = initial?.initializedState ?? 'complete'; |
| 25 | + let boolValue = initial?.boolValue ?? true; |
| 26 | + let initError: Error | undefined; |
| 27 | + |
| 28 | + const handlers = new Map<string, Set<(...args: unknown[]) => void>>(); |
| 29 | + const initStatusSubs = new Set<(r: { status: string; error?: Error }) => void>(); |
| 30 | + const contextSubs = new Set<(c: unknown) => void>(); |
| 31 | + |
| 32 | + const addHandler = (event: string, h: (...args: unknown[]) => void) => { |
| 33 | + if (!handlers.has(event)) { |
| 34 | + handlers.set(event, new Set()); |
| 35 | + } |
| 36 | + handlers.get(event)!.add(h); |
| 37 | + }; |
| 38 | + |
| 39 | + const notReadyDetail = <T>(def: T): LDEvaluationDetailTyped<T> => ({ |
| 40 | + value: def, |
| 41 | + variationIndex: null, |
| 42 | + reason: { kind: 'ERROR', errorKind: 'CLIENT_NOT_READY' }, |
| 43 | + }); |
| 44 | + |
| 45 | + const client = { |
| 46 | + getContext: () => ({ kind: 'user', key: 'context-key' }), |
| 47 | + getInitializationState: () => initializedState, |
| 48 | + getInitializationError: () => initError, |
| 49 | + onInitializationStatusChange: (cb: (r: { status: string; error?: Error }) => void) => { |
| 50 | + initStatusSubs.add(cb); |
| 51 | + return () => initStatusSubs.delete(cb); |
| 52 | + }, |
| 53 | + onContextChange: (cb: (c: unknown) => void) => { |
| 54 | + contextSubs.add(cb); |
| 55 | + return () => contextSubs.delete(cb); |
| 56 | + }, |
| 57 | + isReady: jest.fn(() => ready), |
| 58 | + boolVariation: jest.fn(() => boolValue), |
| 59 | + stringVariation: jest.fn((_key: string, def: string) => def), |
| 60 | + numberVariation: jest.fn((_key: string, def: number) => def), |
| 61 | + jsonVariation: jest.fn((_key: string, def: unknown) => def), |
| 62 | + boolVariationDetail: jest.fn((_key: string, def: boolean) => notReadyDetail(def)), |
| 63 | + stringVariationDetail: jest.fn((_key: string, def: string) => notReadyDetail(def)), |
| 64 | + numberVariationDetail: jest.fn((_key: string, def: number) => notReadyDetail(def)), |
| 65 | + jsonVariationDetail: jest.fn((_key: string, def: unknown) => notReadyDetail(def)), |
| 66 | + allFlags: jest.fn(() => ({})), |
| 67 | + variation: jest.fn(), |
| 68 | + identify: jest.fn(), |
| 69 | + track: jest.fn(), |
| 70 | + flush: jest.fn(), |
| 71 | + start: jest.fn(), |
| 72 | + close: jest.fn(), |
| 73 | + on: jest.fn((event: string, h: (...args: unknown[]) => void) => addHandler(event, h)), |
| 74 | + off: jest.fn((event: string, h: (...args: unknown[]) => void) => handlers.get(event)?.delete(h)), |
| 75 | + }; |
| 76 | + |
| 77 | + const controls: MockControls = { |
| 78 | + setBool: (v: boolean) => { |
| 79 | + boolValue = v; |
| 80 | + }, |
| 81 | + emitChange: (key: string) => |
| 82 | + handlers.get(`change:${key}`)?.forEach((h) => h({ kind: 'user', key: 'context-key' })), |
| 83 | + handlerCount: (event: string) => handlers.get(event)?.size ?? 0, |
| 84 | + emitInitStatus: (r: { status: string; error?: Error }) => { |
| 85 | + ready = true; |
| 86 | + initializedState = r.status; |
| 87 | + initError = r.error; |
| 88 | + initStatusSubs.forEach((cb) => cb(r)); |
| 89 | + }, |
| 90 | + emitContextChange: (c: unknown) => contextSubs.forEach((cb) => cb(c)), |
| 91 | + subscriberCount: () => initStatusSubs.size + contextSubs.size, |
| 92 | + }; |
| 93 | + |
| 94 | + return { client: client as unknown as LDVueClient, controls }; |
| 95 | +} |
0 commit comments