|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | + |
| 3 | +import type { LDContext, LDOptions } from '@launchdarkly/js-client-sdk-common'; |
| 4 | + |
| 5 | +import { useLDClient } from '../hooks'; |
| 6 | +import ReactNativeLDClient from '../ReactNativeLDClient'; |
| 7 | +import LDProvider from './LDProvider'; |
| 8 | +import setupListeners from './setupListeners'; |
| 9 | + |
| 10 | +jest.mock('./setupListeners'); |
| 11 | +jest.mock('../ReactNativeLDClient'); |
| 12 | + |
| 13 | +const TestApp = () => { |
| 14 | + const ldClient = useLDClient(); |
| 15 | + return ( |
| 16 | + <> |
| 17 | + <p>ldClient {ldClient ? 'defined' : 'undefined'}</p> |
| 18 | + <p>mobileKey {ldClient.sdkKey ? ldClient.sdkKey : 'undefined'}</p> |
| 19 | + <p>context {ldClient.getContext() ? 'defined' : 'undefined'}</p> |
| 20 | + </> |
| 21 | + ); |
| 22 | +}; |
| 23 | +describe('LDProvider', () => { |
| 24 | + let ldc: ReactNativeLDClient; |
| 25 | + let context: LDContext; |
| 26 | + let mockSetupListeners = setupListeners as jest.Mock; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + jest.useFakeTimers(); |
| 30 | + (ReactNativeLDClient as jest.Mock).mockImplementation( |
| 31 | + (mobileKey: string, _options?: LDOptions) => { |
| 32 | + let context: LDContext; |
| 33 | + |
| 34 | + return { |
| 35 | + sdkKey: mobileKey, |
| 36 | + identify: jest.fn((c: LDContext) => { |
| 37 | + context = c; |
| 38 | + return Promise.resolve(); |
| 39 | + }), |
| 40 | + getContext: jest.fn(() => context), |
| 41 | + on: jest.fn(), |
| 42 | + logger: { |
| 43 | + debug: jest.fn(), |
| 44 | + }, |
| 45 | + }; |
| 46 | + }, |
| 47 | + ); |
| 48 | + mockSetupListeners.mockImplementation((client: ReactNativeLDClient, setState: any) => { |
| 49 | + setState({ client }); |
| 50 | + }); |
| 51 | + ldc = new ReactNativeLDClient('mobile-key'); |
| 52 | + context = { kind: 'user', key: 'test-user-key-1' }; |
| 53 | + }); |
| 54 | + |
| 55 | + afterEach(() => { |
| 56 | + jest.resetAllMocks(); |
| 57 | + }); |
| 58 | + |
| 59 | + test('client is correctly set', () => { |
| 60 | + const { getByText } = render( |
| 61 | + <LDProvider client={ldc}> |
| 62 | + <TestApp /> |
| 63 | + </LDProvider>, |
| 64 | + ); |
| 65 | + |
| 66 | + expect(getByText(/ldclient defined/i)).toBeTruthy(); |
| 67 | + expect(getByText(/mobilekey mobile-key/i)).toBeTruthy(); |
| 68 | + expect(getByText(/context undefined/i)).toBeTruthy(); |
| 69 | + }); |
| 70 | + |
| 71 | + test('specified context is identified', async () => { |
| 72 | + const { getByText } = render( |
| 73 | + <LDProvider client={ldc} context={context}> |
| 74 | + <TestApp /> |
| 75 | + </LDProvider>, |
| 76 | + ); |
| 77 | + |
| 78 | + expect(mockSetupListeners).toHaveBeenCalledWith(ldc, expect.any(Function)); |
| 79 | + expect(ldc.identify).toHaveBeenCalledWith(context); |
| 80 | + expect(ldc.getContext()).toEqual(context); |
| 81 | + expect(getByText(/context defined/i)).toBeTruthy(); |
| 82 | + }); |
| 83 | + |
| 84 | + test('identify errors are caught', async () => { |
| 85 | + (ldc.identify as jest.Mock).mockImplementation(() => { |
| 86 | + return Promise.reject('faking error when identifying'); |
| 87 | + }); |
| 88 | + const { getByText } = render( |
| 89 | + <LDProvider client={ldc} context={context}> |
| 90 | + <TestApp /> |
| 91 | + </LDProvider>, |
| 92 | + ); |
| 93 | + await jest.runAllTimersAsync(); |
| 94 | + |
| 95 | + expect(ldc.logger.debug).toHaveBeenCalledWith(expect.stringMatching(/identify error/)); |
| 96 | + }); |
| 97 | +}); |
0 commit comments