-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSplitFactoryProvider.test.tsx
More file actions
252 lines (218 loc) · 7.78 KB
/
SplitFactoryProvider.test.tsx
File metadata and controls
252 lines (218 loc) · 7.78 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import * as React from 'react';
import { render, act } from '@testing-library/react';
/** Mocks */
import { mockSdk, Event, getLastInstance } from './testUtils/mockSplitFactory';
jest.mock('@splitsoftware/splitio/client', () => {
return { SplitFactory: mockSdk() };
});
import { SplitFactory } from '@splitsoftware/splitio/client';
import { sdkBrowser } from './testUtils/sdkConfigs';
const logSpy = jest.spyOn(console, 'log');
/** Test target */
import { SplitFactoryProvider } from '../SplitFactoryProvider';
import { SplitContext, useSplitContext } from '../SplitContext';
import { getStatus } from '../utils';
import { WARN_SF_CONFIG_AND_FACTORY } from '../constants';
import { INITIAL_STATUS } from './testUtils/utils';
import { useSplitClient } from '../useSplitClient';
describe('SplitFactoryProvider', () => {
test('passes no-ready properties, no factory and no client to the context if initialized without a config and factory props.', () => {
render(
<SplitFactoryProvider >
{React.createElement(() => {
const context = useSplitContext();
expect(context).toEqual({
...INITIAL_STATUS,
factory: undefined,
client: undefined,
});
return null;
})}
</SplitFactoryProvider>
);
});
test('passes no-ready properties to the context if initialized with a config.', () => {
render(
<SplitFactoryProvider config={sdkBrowser} >
{React.createElement(() => {
const context = useSplitContext();
expect(context).toEqual({
...INITIAL_STATUS,
factory: getLastInstance(SplitFactory),
client: getLastInstance(SplitFactory).client(),
});
return null;
})}
</SplitFactoryProvider>
);
});
test('passes ready properties to the context if initialized with a ready factory.', async () => {
const outerFactory = SplitFactory(sdkBrowser);
(outerFactory as any).client().__emitter__.emit(Event.SDK_READY_FROM_CACHE);
(outerFactory as any).client().__emitter__.emit(Event.SDK_READY);
(outerFactory.manager().names as jest.Mock).mockReturnValue(['split1']);
await outerFactory.client().ready();
render(
<SplitFactoryProvider factory={outerFactory} >
{React.createElement(() => {
const context = useSplitClient();
expect(context).toEqual({
...INITIAL_STATUS,
factory: outerFactory,
client: outerFactory.client(),
isReady: true,
isReadyFromCache: true,
isOperational: true,
lastUpdate: getStatus(outerFactory.client()).lastUpdate
});
return null;
})}
</SplitFactoryProvider>
);
});
test('renders a passed JSX.Element with a new SplitContext value.', (done) => {
const Component = () => {
return (
<SplitContext.Consumer>
{(value) => {
expect(value).toEqual({
...INITIAL_STATUS,
factory: getLastInstance(SplitFactory),
client: getLastInstance(SplitFactory).client(),
});
done();
return null;
}}
</SplitContext.Consumer>
);
};
render(
<SplitFactoryProvider config={sdkBrowser} >
<Component />
</SplitFactoryProvider>
);
});
test('logs warning if both a config and factory are passed as props.', () => {
const outerFactory = SplitFactory(sdkBrowser);
render(
<SplitFactoryProvider config={sdkBrowser} factory={outerFactory} >
{React.createElement(() => {
return null;
})}
</SplitFactoryProvider>
);
expect(logSpy).toBeCalledWith('[WARN] splitio => ' + WARN_SF_CONFIG_AND_FACTORY);
logSpy.mockRestore();
});
test('cleans up on update and unmount if config prop is provided.', () => {
let renderTimes = 0;
const createdFactories = new Set<SplitIO.IBrowserSDK>();
const factoryDestroySpies: jest.SpyInstance[] = [];
const outerFactory = SplitFactory(sdkBrowser);
const Component = () => {
const { factory, isReady, hasTimedout } = useSplitClient();
renderTimes++;
switch (renderTimes) {
case 1:
expect(factory).toBe(outerFactory);
return null;
case 2:
case 5:
expect(isReady).toBe(false);
expect(hasTimedout).toBe(false);
expect(factory).toBe(getLastInstance(SplitFactory));
if (!createdFactories.has(factory!)) factoryDestroySpies.push(jest.spyOn(factory!, 'destroy'));
createdFactories.add(factory!);
return null;
case 3:
case 4:
case 6:
expect(isReady).toBe(true);
expect(hasTimedout).toBe(true);
expect(factory).toBe(getLastInstance(SplitFactory));
if (!createdFactories.has(factory!)) factoryDestroySpies.push(jest.spyOn(factory!, 'destroy'));
createdFactories.add(factory!);
return null;
case 7:
throw new Error('Must not rerender');
}
return null;
};
const emitSdkEvents = () => {
const factory = getLastInstance(SplitFactory);
factory.client().__emitter__.emit(Event.SDK_READY_TIMED_OUT)
factory.client().__emitter__.emit(Event.SDK_READY)
};
// 1st render: factory provided
const wrapper = render(
<SplitFactoryProvider factory={outerFactory} >
<Component />
</SplitFactoryProvider>
);
// 2nd render: factory created, not ready
wrapper.rerender(
<SplitFactoryProvider config={sdkBrowser} >
<Component />
</SplitFactoryProvider>
);
// 3rd render: SDK timeout and ready events emitted (only one re-render due to batched state updates in React)
act(emitSdkEvents);
// 4th render: same config prop -> factory is not recreated
wrapper.rerender(
<SplitFactoryProvider config={sdkBrowser} >
<Component />
</SplitFactoryProvider>
);
act(emitSdkEvents); // Emitting events again has no effect
expect(createdFactories.size).toBe(1);
// 5th render: Update config prop -> factory is recreated, not ready yet
wrapper.rerender(
<SplitFactoryProvider config={{ ...sdkBrowser }} >
<Component />
</SplitFactoryProvider>
);
// 6th render: SDK events emitted
act(emitSdkEvents);
wrapper.unmount();
// factory `destroy` methods are called
expect(createdFactories.size).toBe(2);
expect(factoryDestroySpies.length).toBe(2);
factoryDestroySpies.forEach(spy => expect(spy).toBeCalledTimes(1));
});
test('doesn\'t clean up on unmount if the factory is provided as a prop.', () => {
let destroySpy;
const outerFactory = SplitFactory(sdkBrowser);
const wrapper = render(
<SplitFactoryProvider factory={outerFactory}>
{React.createElement(() => {
const { factory } = useSplitClient();
destroySpy = jest.spyOn(factory!, 'destroy');
return null;
})}
</SplitFactoryProvider>
);
wrapper.unmount();
expect(destroySpy).not.toBeCalled();
});
test('passes attributes to the main client if provided.', () => {
(SplitFactory as jest.Mock).mockClear();
let client;
const Component = () => {
client = useSplitContext().client;
return null;
}
const wrapper = render(
<SplitFactoryProvider config={sdkBrowser} attributes={{ attr1: 'value1' }} >
<Component />
</SplitFactoryProvider>
);
expect(client.getAttributes()).toEqual({ attr1: 'value1' });
wrapper.rerender(
<SplitFactoryProvider config={sdkBrowser} attributes={{ attr1: 'value2' }} >
<Component />
</SplitFactoryProvider>
);
expect(client.getAttributes()).toEqual({ attr1: 'value2' });
expect(SplitFactory).toBeCalledTimes(1);
});
});