diff --git a/packages/sdk/react-native/example/app.json b/packages/sdk/react-native/example/app.json index b797a3a7e..681b269d3 100644 --- a/packages/sdk/react-native/example/app.json +++ b/packages/sdk/react-native/example/app.json @@ -11,9 +11,7 @@ "resizeMode": "contain", "backgroundColor": "#ffffff" }, - "assetBundlePatterns": [ - "**/*" - ], + "assetBundlePatterns": ["**/*"], "ios": { "supportsTablet": true, "bundleIdentifier": "com.anonymous.reactnativeexample" diff --git a/packages/sdk/react-native/src/provider/LDProvider.test.tsx b/packages/sdk/react-native/src/provider/LDProvider.test.tsx index bae8ba56c..2b5c90498 100644 --- a/packages/sdk/react-native/src/provider/LDProvider.test.tsx +++ b/packages/sdk/react-native/src/provider/LDProvider.test.tsx @@ -1,26 +1,14 @@ -import { act, render } from '@testing-library/react'; +import { render } from '@testing-library/react'; import type { LDContext, LDOptions } from '@launchdarkly/js-client-sdk-common'; import { useLDClient } from '../hooks'; import ReactNativeLDClient from '../ReactNativeLDClient'; import LDProvider from './LDProvider'; +import setupListeners from './setupListeners'; -jest.mock('../ReactNativeLDClient', () => - jest.fn((mobileKey: string, _options: LDOptions) => { - let context: LDContext; - - return { - sdkKey: mobileKey, - identify: jest.fn((c: LDContext) => { - context = c; - return Promise.resolve(); - }), - getContext: jest.fn(() => context), - on: jest.fn(), - }; - }), -); +jest.mock('./setupListeners'); +jest.mock('../ReactNativeLDClient', () => jest.fn()); const TestApp = () => { const ldClient = useLDClient(); @@ -33,14 +21,41 @@ const TestApp = () => { ); }; describe('LDProvider', () => { - let ldc: ReactNativeLDClient; + let ldc: any; let context: LDContext; + let mockSetupListeners = setupListeners as jest.Mock; beforeEach(() => { - ldc = new ReactNativeLDClient('mobile-key', { sendEvents: false }); + jest.useFakeTimers(); + (ReactNativeLDClient as jest.Mock).mockImplementation( + (mobileKey: string, _options?: LDOptions) => { + let context: LDContext; + + return { + sdkKey: mobileKey, + identify: jest.fn((c: LDContext) => { + context = c; + return Promise.resolve(); + }), + getContext: jest.fn(() => context), + on: jest.fn(), + logger: { + debug: jest.fn(), + }, + }; + }, + ); + mockSetupListeners.mockImplementation((client: ReactNativeLDClient, setState: any) => { + setState({ client }); + }); + ldc = new ReactNativeLDClient('mobile-key'); context = { kind: 'user', key: 'test-user-key-1' }; }); + afterEach(() => { + jest.resetAllMocks(); + }); + test('client is correctly set', () => { const { getByText } = render( @@ -53,18 +68,30 @@ describe('LDProvider', () => { expect(getByText(/context undefined/i)).toBeTruthy(); }); - test.only('specified context is identified', async () => { - let output; - await act(async () => { - output = render( - - - , - ); - }); + test('specified context is identified', async () => { + const { getByText } = render( + + + , + ); - expect(output!.getByText(/context defined/i)).toBeTruthy(); + expect(mockSetupListeners).toHaveBeenCalledWith(ldc, expect.any(Function)); + expect(ldc.identify).toHaveBeenCalledWith(context); + expect(ldc.getContext()).toEqual(context); + expect(getByText(/context defined/i)).toBeTruthy(); }); - test.todo('listeners are setup correctly'); + test('identify errors are caught', async () => { + (ldc.identify as jest.Mock).mockImplementation(() => { + return Promise.reject('faking error when identifying'); + }); + const { getByText } = render( + + + , + ); + await jest.runAllTimersAsync(); + + expect(ldc.logger.debug).toHaveBeenCalledWith(expect.stringMatching(/identify error/)); + }); });