-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathinitLDClient.test.ts
92 lines (71 loc) · 3.68 KB
/
initLDClient.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
jest.mock('launchdarkly-js-client-sdk', () => {
const actual = jest.requireActual('launchdarkly-js-client-sdk');
return {
...actual,
initialize: jest.fn(),
};
});
jest.mock('../package.json', () => ({ version: 'mock.version' }));
import { initialize, LDClient, LDOptions, LDUser } from 'launchdarkly-js-client-sdk';
import { defaultReactOptions, LDReactOptions } from './types';
import initLDClient from './initLDClient';
const ldClientInitialize = initialize as jest.Mock;
const clientSideID = 'deadbeef';
const defaultUser: LDUser = { key: 'abcdef' };
const options: LDOptions = { bootstrap: 'localStorage' };
const extraOptionsAddedBySdk: LDOptions = { wrapperName: 'react-client-sdk', wrapperVersion: 'mock.version' };
const expectedOptions: LDOptions = { ...options, ...extraOptionsAddedBySdk };
const flags = { 'test-flag': false, 'another-test-flag': true };
describe('initLDClient', () => {
let mockLDClient: Partial<LDClient>;
beforeEach(() => {
mockLDClient = {
on: (e: string, cb: () => void) => {
cb();
},
allFlags: () => flags,
variation: jest.fn(() => true),
};
ldClientInitialize.mockImplementation(() => mockLDClient);
});
afterEach(() => {
jest.resetAllMocks();
});
test('initialise with clientSideID only', async () => {
const anonUser: LDUser = { anonymous: true };
await initLDClient(clientSideID);
expect(ldClientInitialize.mock.calls[0]).toEqual([clientSideID, anonUser, extraOptionsAddedBySdk]);
expect(mockLDClient.variation).toHaveBeenCalledTimes(0);
});
test('initialise with custom user and options', async () => {
const customUser = { key: '[email protected]' };
await initLDClient(clientSideID, customUser, defaultReactOptions, options);
expect(ldClientInitialize.mock.calls[0]).toEqual([clientSideID, customUser, expectedOptions]);
expect(mockLDClient.variation).toHaveBeenCalledTimes(0);
});
test('initialise should return camelCased flags by default', async () => {
const flagsClient = await initLDClient(clientSideID);
expect(mockLDClient.variation).toHaveBeenCalledTimes(0);
expect(flagsClient).toEqual({ flags: { anotherTestFlag: true, testFlag: false }, ldClient: mockLDClient });
});
test('initialise should not transform keys to camel case if option is disabled', async () => {
const reactOptions: LDReactOptions = { useCamelCaseFlagKeys: false };
const flagsClient = await initLDClient(clientSideID, defaultUser, reactOptions, options);
expect(mockLDClient.variation).toHaveBeenCalledTimes(0);
expect(flagsClient).toEqual({ flags: { 'another-test-flag': true, 'test-flag': false }, ldClient: mockLDClient });
});
test('initialise should transform keys to camel case if option is absent', async () => {
const flagsClient = await initLDClient(clientSideID, defaultUser, defaultReactOptions, options);
expect(mockLDClient.variation).toHaveBeenCalledTimes(0);
expect(flagsClient).toEqual({ flags: { anotherTestFlag: true, testFlag: false }, ldClient: mockLDClient });
});
test('initialise should call variation if flags are specified', async () => {
const customUser = { key: '[email protected]' };
const targetFlags = { 'lonely-flag': false, 'lonelier-flag': false };
const flagsClient = await initLDClient(clientSideID, customUser, defaultReactOptions, options, targetFlags);
expect(mockLDClient.variation).toHaveBeenCalledTimes(2);
expect(mockLDClient.variation).toHaveBeenNthCalledWith(1, 'lonely-flag', false);
expect(mockLDClient.variation).toHaveBeenNthCalledWith(2, 'lonelier-flag', false);
expect(flagsClient).toEqual({ flags: { lonelyFlag: true, lonelierFlag: true }, ldClient: mockLDClient });
});
});