forked from launchdarkly/react-client-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncWithLDProvider.test.tsx
197 lines (168 loc) · 7.14 KB
/
asyncWithLDProvider.test.tsx
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
jest.mock('./initLDClient', () => jest.fn());
import React from 'react';
import { render } from '@testing-library/react';
import { LDFlagChangeset, LDOptions, LDUser } from 'launchdarkly-js-client-sdk';
import initLDClient from './initLDClient';
import { defaultReactOptions, LDReactOptions, ProviderConfig } from './types';
import { Consumer } from './context';
import asyncWithLDProvider from './asyncWithLDProvider';
const clientSideID = 'deadbeef';
const user: LDUser = { key: 'yus', name: 'yus ng' };
const App = () => <>My App</>;
const mockInitLDClient = initLDClient as jest.Mock;
const mockFlags = { testFlag: true, anotherTestFlag: true };
let mockLDClient: { on: jest.Mock };
const renderWithConfig = async (config: ProviderConfig) => {
const LDProvider = await asyncWithLDProvider(config);
const { getByText } = render(
<LDProvider>
<Consumer>{value => <span>Received: {JSON.stringify(value.flags)}</span>}</Consumer>
</LDProvider>,
);
return getByText(/^Received:/);
};
describe('asyncWithLDProvider', () => {
beforeEach(() => {
mockLDClient = {
on: jest.fn((e: string, cb: () => void) => {
cb();
}),
};
mockInitLDClient.mockImplementation(() => ({
flags: mockFlags,
ldClient: mockLDClient,
}));
});
afterEach(() => {
jest.resetAllMocks();
});
test('provider renders app correctly', async () => {
const LDProvider = await asyncWithLDProvider({ clientSideID });
const { container } = render(
<LDProvider>
<App />
</LDProvider>,
);
expect(container).toMatchSnapshot();
});
test('ldClient is initialised correctly', async () => {
const options: LDOptions = { bootstrap: {} };
const reactOptions: LDReactOptions = { useCamelCaseFlagKeys: false };
await asyncWithLDProvider({ clientSideID, user, options, reactOptions });
expect(mockInitLDClient).toHaveBeenCalledWith(clientSideID, user, reactOptions, options, undefined);
});
test('subscribe to changes on mount', async () => {
const LDProvider = await asyncWithLDProvider({ clientSideID });
render(
<LDProvider>
<App />
</LDProvider>,
);
expect(mockLDClient.on).toHaveBeenNthCalledWith(1, 'change', expect.any(Function));
});
test('subscribe to changes with camelCase', async () => {
mockLDClient.on.mockImplementation((e: string, cb: (c: LDFlagChangeset) => void) => {
cb({ 'test-flag': { current: false, previous: true } });
});
const receivedNode = await renderWithConfig({ clientSideID });
expect(mockLDClient.on).toHaveBeenNthCalledWith(1, 'change', expect.any(Function));
expect(receivedNode).toHaveTextContent('{"testFlag":false,"anotherTestFlag":true}');
});
test('subscribe to changes with kebab-case', async () => {
mockInitLDClient.mockImplementation(() => ({
flags: { 'another-test-flag': true, 'test-flag': true },
ldClient: mockLDClient,
}));
mockLDClient.on.mockImplementation((e: string, cb: (c: LDFlagChangeset) => void) => {
cb({ 'another-test-flag': { current: false, previous: true }, 'test-flag': { current: false, previous: true } });
});
const receivedNode = await renderWithConfig({ clientSideID, reactOptions: { useCamelCaseFlagKeys: false } });
expect(mockLDClient.on).toHaveBeenNthCalledWith(1, 'change', expect.any(Function));
expect(receivedNode).toHaveTextContent('{"another-test-flag":false,"test-flag":false}');
});
test('consecutive flag changes gets stored in context correctly', async () => {
mockLDClient.on.mockImplementationOnce((e: string, cb: (c: LDFlagChangeset) => void) => {
cb({ 'another-test-flag': { current: false, previous: true } });
// simulate second update
cb({ 'test-flag': { current: false, previous: true } });
});
const receivedNode = await renderWithConfig({ clientSideID });
expect(mockLDClient.on).toHaveBeenNthCalledWith(1, 'change', expect.any(Function));
expect(receivedNode).toHaveTextContent('{"testFlag":false,"anotherTestFlag":false}');
});
test('ldClient bootstraps correctly', async () => {
// don't subscribe to changes to test bootstrap
mockLDClient.on.mockImplementation((e: string, cb: (c: LDFlagChangeset) => void) => {
return;
});
const options: LDOptions = {
bootstrap: {
'another-test-flag': false,
'test-flag': true,
},
};
const receivedNode = await renderWithConfig({ clientSideID, user, options });
expect(receivedNode).toHaveTextContent('{"anotherTestFlag":false,"testFlag":true}');
});
test('ldClient bootstraps with empty flags', async () => {
// don't subscribe to changes to test bootstrap
mockLDClient.on.mockImplementation((e: string, cb: (c: LDFlagChangeset) => void) => {
return;
});
const options: LDOptions = {
bootstrap: {},
};
const receivedNode = await renderWithConfig({ clientSideID, user, options });
expect(receivedNode).toHaveTextContent('{}');
});
test('ldClient bootstraps correctly with kebab-case', async () => {
// don't subscribe to changes to test bootstrap
mockLDClient.on.mockImplementation((e: string, cb: (c: LDFlagChangeset) => void) => {
return;
});
const options: LDOptions = {
bootstrap: {
'another-test-flag': false,
'test-flag': true,
},
};
const receivedNode = await renderWithConfig({
clientSideID,
user,
options,
reactOptions: { useCamelCaseFlagKeys: false },
});
expect(receivedNode).toHaveTextContent('{"another-test-flag":false,"test-flag":true}');
});
test('internal flags state should be initialised to all flags', async () => {
const options: LDOptions = {
bootstrap: 'localStorage',
};
const receivedNode = await renderWithConfig({ clientSideID, user, options });
expect(receivedNode).toHaveTextContent('{"testFlag":true,"anotherTestFlag":true}');
});
test('ldClient is initialised correctly with target flags', async () => {
mockInitLDClient.mockImplementation(() => ({
flags: { devTestFlag: true, launchDoggly: true },
ldClient: mockLDClient,
}));
const options: LDOptions = {};
const flags = { 'dev-test-flag': false, 'launch-doggly': false };
const receivedNode = await renderWithConfig({ clientSideID, user, options, flags });
expect(mockInitLDClient).toHaveBeenCalledWith(clientSideID, user, defaultReactOptions, options, flags);
expect(receivedNode).toHaveTextContent('{"devTestFlag":true,"launchDoggly":true}');
});
test('only updates to subscribed flags are pushed to the Provider', async () => {
mockInitLDClient.mockImplementation(() => ({
flags: { testFlag: true },
ldClient: mockLDClient,
}));
mockLDClient.on.mockImplementation((e: string, cb: (c: LDFlagChangeset) => void) => {
cb({ 'test-flag': { current: false, previous: true }, 'another-test-flag': { current: false, previous: true } });
});
const options: LDOptions = {};
const subscribedFlags = { 'test-flag': false };
const receivedNode = await renderWithConfig({ clientSideID, user, options, flags: subscribedFlags });
expect(receivedNode).toHaveTextContent('{"testFlag":false}');
});
});