|
| 1 | +import { createClient as createBaseClient } from '@launchdarkly/js-client-sdk'; |
| 2 | + |
| 3 | +import { createClient } from '../../src/client/LDVueClient'; |
| 4 | + |
| 5 | +jest.mock('@launchdarkly/js-client-sdk', () => ({ |
| 6 | + createClient: jest.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +const createBaseClientMock = createBaseClient as jest.Mock; |
| 10 | + |
| 11 | +type Result = { status: string; error?: Error }; |
| 12 | + |
| 13 | +const makeBaseClient = (overrides: Record<string, unknown> = {}) => ({ |
| 14 | + getContext: jest.fn(() => ({ kind: 'user', key: 'context-key' })), |
| 15 | + start: jest.fn(() => Promise.resolve<Result>({ status: 'complete' })), |
| 16 | + identify: jest.fn(() => Promise.resolve<Result>({ status: 'completed' })), |
| 17 | + boolVariation: jest.fn(() => true), |
| 18 | + on: jest.fn(), |
| 19 | + off: jest.fn(), |
| 20 | + close: jest.fn(), |
| 21 | + ...overrides, |
| 22 | +}); |
| 23 | + |
| 24 | +beforeEach(() => { |
| 25 | + createBaseClientMock.mockReset(); |
| 26 | +}); |
| 27 | + |
| 28 | +it('passes wrapper metadata to the base client', () => { |
| 29 | + createBaseClientMock.mockReturnValue(makeBaseClient()); |
| 30 | + |
| 31 | + createClient('env-id', { kind: 'user', key: 'k' }); |
| 32 | + |
| 33 | + expect(createBaseClientMock).toHaveBeenCalledWith( |
| 34 | + 'env-id', |
| 35 | + { kind: 'user', key: 'k' }, |
| 36 | + expect.objectContaining({ wrapperName: 'vue-client-sdk', wrapperVersion: expect.any(String) }), |
| 37 | + ); |
| 38 | +}); |
| 39 | + |
| 40 | +it('tracks initialization state through start()', async () => { |
| 41 | + createBaseClientMock.mockReturnValue(makeBaseClient()); |
| 42 | + const client = createClient('env-id', { kind: 'user', key: 'k' }); |
| 43 | + |
| 44 | + expect(client.getInitializationState()).toBe('initializing'); |
| 45 | + expect(client.isReady()).toBe(false); |
| 46 | + |
| 47 | + await client.start(); |
| 48 | + |
| 49 | + expect(client.getInitializationState()).toBe('complete'); |
| 50 | + expect(client.isReady()).toBe(true); |
| 51 | + expect(client.getInitializationError()).toBeUndefined(); |
| 52 | +}); |
| 53 | + |
| 54 | +it('notifies init-status subscribers and replays the cached result to late subscribers', async () => { |
| 55 | + createBaseClientMock.mockReturnValue(makeBaseClient()); |
| 56 | + const client = createClient('env-id', { kind: 'user', key: 'k' }); |
| 57 | + |
| 58 | + const early = jest.fn(); |
| 59 | + client.onInitializationStatusChange(early); |
| 60 | + |
| 61 | + await client.start(); |
| 62 | + |
| 63 | + expect(early).toHaveBeenCalledWith({ status: 'complete' }); |
| 64 | + |
| 65 | + const late = jest.fn(); |
| 66 | + client.onInitializationStatusChange(late); |
| 67 | + expect(late).toHaveBeenCalledWith({ status: 'complete' }); |
| 68 | +}); |
| 69 | + |
| 70 | +it('exposes the initialization error when start fails', async () => { |
| 71 | + const error = new Error('boom'); |
| 72 | + createBaseClientMock.mockReturnValue( |
| 73 | + makeBaseClient({ start: jest.fn(() => Promise.resolve({ status: 'failed', error })) }), |
| 74 | + ); |
| 75 | + const client = createClient('env-id', { kind: 'user', key: 'k' }); |
| 76 | + |
| 77 | + await client.start(); |
| 78 | + |
| 79 | + expect(client.getInitializationState()).toBe('failed'); |
| 80 | + expect(client.getInitializationError()).toBe(error); |
| 81 | +}); |
| 82 | + |
| 83 | +it('notifies context subscribers after a successful identify', async () => { |
| 84 | + createBaseClientMock.mockReturnValue(makeBaseClient()); |
| 85 | + const client = createClient('env-id', { kind: 'user', key: 'k' }); |
| 86 | + |
| 87 | + const onContext = jest.fn(); |
| 88 | + client.onContextChange(onContext); |
| 89 | + |
| 90 | + await client.identify({ kind: 'user', key: 'new-key' }); |
| 91 | + |
| 92 | + expect(onContext).toHaveBeenCalledWith({ kind: 'user', key: 'context-key' }); |
| 93 | +}); |
| 94 | + |
| 95 | +it('does not notify context subscribers when identify does not complete', async () => { |
| 96 | + createBaseClientMock.mockReturnValue( |
| 97 | + makeBaseClient({ identify: jest.fn(() => Promise.resolve({ status: 'error', error: new Error('x') })) }), |
| 98 | + ); |
| 99 | + const client = createClient('env-id', { kind: 'user', key: 'k' }); |
| 100 | + |
| 101 | + const onContext = jest.fn(); |
| 102 | + client.onContextChange(onContext); |
| 103 | + |
| 104 | + await client.identify({ kind: 'user', key: 'new-key' }); |
| 105 | + |
| 106 | + expect(onContext).not.toHaveBeenCalled(); |
| 107 | +}); |
0 commit comments