Skip to content

Commit

Permalink
chore: added more provider tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
yusinto committed Dec 29, 2023
1 parent 7f6f514 commit 56f1847
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 32 deletions.
4 changes: 1 addition & 3 deletions packages/sdk/react-native/example/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"assetBundlePatterns": [
"**/*"
],
"assetBundlePatterns": ["**/*"],
"ios": {
"supportsTablet": true,
"bundleIdentifier": "com.anonymous.reactnativeexample"
Expand Down
85 changes: 56 additions & 29 deletions packages/sdk/react-native/src/provider/LDProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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(
<LDProvider client={ldc}>
Expand All @@ -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(
<LDProvider client={ldc} context={context}>
<TestApp />
</LDProvider>,
);
});
test('specified context is identified', async () => {
const { getByText } = render(
<LDProvider client={ldc} context={context}>
<TestApp />
</LDProvider>,
);

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(
<LDProvider client={ldc} context={context}>
<TestApp />
</LDProvider>,
);
await jest.runAllTimersAsync();

expect(ldc.logger.debug).toHaveBeenCalledWith(expect.stringMatching(/identify error/));
});
});

0 comments on commit 56f1847

Please sign in to comment.