Skip to content

Commit 6e3caf7

Browse files
committed
fix: MockFlags correctly mocks all types of flags
1 parent 1f2163e commit 6e3caf7

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

packages/tooling/jest/src/react-native/index.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import {
88
} from '.';
99

1010
describe('react-native', () => {
11+
afterEach(() => {
12+
resetLDMocks();
13+
});
14+
1115
test('reset LD Mocks', () => {
1216
const current = mockUseLDClient();
1317

@@ -47,7 +51,6 @@ describe('react-native', () => {
4751
});
4852

4953
test('mock ldClient correctly', () => {
50-
resetLDMocks();
5154
const current = mockUseLDClient();
5255

5356
current?.track('event');

packages/tooling/jest/src/react-native/index.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import {
33
LDProvider,
44
ReactNativeLDClient,
55
useLDClient,
6+
useBoolVariation,
7+
useJsonVariation,
8+
LDClient,
9+
useNumberVariation,
10+
useStringVariation,
611
} from '@launchdarkly/react-native-client-sdk';
712

813
jest.mock('@launchdarkly/react-native-client-sdk', () => ({
@@ -22,18 +27,23 @@ jest.mock('@launchdarkly/react-native-client-sdk', () => ({
2227
useTypedVariationDetail: jest.fn(),
2328
}));
2429

25-
export const ldClientMock: any = {
30+
export const ldClientMock: jest.Mocked<LDClient> = {
2631
allFlags: jest.fn(),
2732
boolVariation: jest.fn(),
2833
boolVariationDetail: jest.fn(),
2934
close: jest.fn(),
30-
flush: jest.fn(() => Promise.resolve()),
35+
flush: jest.fn(() => Promise.resolve({ result: true })),
3136
getConnectionMode: jest.fn(),
3237
getContext: jest.fn(),
33-
identify: jest.fn(() => Promise.resolve()),
38+
identify: jest.fn().mockResolvedValue(undefined),
3439
jsonVariation: jest.fn(),
3540
jsonVariationDetail: jest.fn(),
36-
logger: jest.fn(),
41+
logger: {
42+
error: jest.fn(),
43+
warn: jest.fn(),
44+
info: jest.fn(),
45+
debug: jest.fn(),
46+
},
3747
numberVariation: jest.fn(),
3848
numberVariationDetail: jest.fn(),
3949
off: jest.fn(),
@@ -50,6 +60,11 @@ export const mockLDProvider = LDProvider as jest.Mock;
5060
export const mockReactNativeLDClient = ReactNativeLDClient as jest.Mock;
5161
export const mockUseLDClient = useLDClient as jest.Mock;
5262

63+
const mockUseBoolVariation = useBoolVariation as jest.Mock;
64+
const mockUseNumberVariation = useNumberVariation as jest.Mock;
65+
const mockUseStringVariation = useStringVariation as jest.Mock;
66+
const mockUseJsonVariation = useJsonVariation as jest.Mock;
67+
5368
mockLDProvider.mockImplementation(({ children }) => children);
5469
mockReactNativeLDClient.mockImplementation(() => ldClientMock);
5570
mockUseLDClient.mockImplementation(() => ldClientMock);
@@ -59,21 +74,25 @@ export const mockFlags = (flags: LDFlagSet): any => {
5974
const defaultValue = flags[key];
6075
switch (typeof defaultValue) {
6176
case 'boolean':
77+
mockUseBoolVariation.mockImplementation((flagKey: string) => flags[flagKey] as boolean);
6278
ldClientMock.boolVariation.mockImplementation(
6379
(flagKey: string) => flags[flagKey] as boolean,
6480
);
6581
break;
6682
case 'number':
83+
mockUseNumberVariation.mockImplementation((flagKey: string) => flags[flagKey] as number);
6784
ldClientMock.numberVariation.mockImplementation(
6885
(flagKey: string) => flags[flagKey] as number,
6986
);
7087
break;
7188
case 'string':
89+
mockUseStringVariation.mockImplementation((flagKey: string) => flags[flagKey] as string);
7290
ldClientMock.stringVariation.mockImplementation(
7391
(flagKey: string) => flags[flagKey] as string,
7492
);
7593
break;
7694
case 'object':
95+
mockUseJsonVariation.mockImplementation((flagKey: string) => flags[flagKey] as object);
7796
ldClientMock.jsonVariation.mockImplementation(
7897
(flagKey: string) => flags[flagKey] as object,
7998
);

0 commit comments

Comments
 (0)