-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsdkClientMethodCS.spec.ts
More file actions
319 lines (243 loc) · 12.9 KB
/
sdkClientMethodCS.spec.ts
File metadata and controls
319 lines (243 loc) · 12.9 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import { sdkClientMethodCSFactory as sdkClientMethodCSWithTTFactory } from '../sdkClientMethodCSWithTT';
import { sdkClientMethodCSFactory } from '../sdkClientMethodCS';
import { assertClientApi } from './testUtils';
import { telemetryTrackerFactory } from '../../trackers/telemetryTracker';
import { settingsWithKey, settingsWithKeyAndTT, settingsWithKeyObject } from '../../utils/settingsValidation/__tests__/settings.mocks';
const partialStorages: { destroy: jest.Mock }[] = [];
const storageMock = {
destroy: jest.fn(),
shared: jest.fn(() => {
partialStorages.push({ destroy: jest.fn() });
return partialStorages[partialStorages.length - 1];
})
};
function readinessManagerMock() {
let isDestroyed = false;
return {
sdkStatus: { __getStatus: () => ({ isDestroyed }), },
readinessManager: { destroy: jest.fn(() => { isDestroyed = true; }) },
_undestroy: () => { isDestroyed = false; }
};
}
const partialSdkReadinessManagers: { sdkStatus: any, readinessManager: { destroy: jest.Mock } }[] = [];
const sdkReadinessManagerMock = {
...readinessManagerMock(),
shared: jest.fn(() => {
partialSdkReadinessManagers.push(readinessManagerMock());
return partialSdkReadinessManagers[partialSdkReadinessManagers.length - 1];
})
};
const mySegmentsSyncManagers: { start: jest.Mock, stop: jest.Mock }[] = [];
const syncManagerMock = {
stop: jest.fn(),
flush: jest.fn(() => Promise.resolve()),
shared: jest.fn(() => {
mySegmentsSyncManagers.push({ start: jest.fn(), stop: jest.fn() });
return mySegmentsSyncManagers[mySegmentsSyncManagers.length - 1];
})
};
const params = {
storage: storageMock,
sdkReadinessManager: sdkReadinessManagerMock,
syncManager: syncManagerMock,
signalListener: { stop: jest.fn() },
settings: settingsWithKey,
telemetryTracker: telemetryTrackerFactory(),
clients: {}
};
const invalidAttributes = [
new Date(),
{ some: 'object' },
Infinity
];
const validAttributes = [
25, // number
Date.now(), // number
'string', // string
['string', 'list'], // list
false // boolean
];
/** End mocks */
describe('sdkClientMethodCSFactory', () => {
afterEach(() => {
jest.clearAllMocks();
partialStorages.length = 0;
sdkReadinessManagerMock._undestroy();
partialSdkReadinessManagers.length = 0;
mySegmentsSyncManagers.length = 0;
params.clients = {};
});
// list of factory functions and their types (whether it ignores TT or not)
const testTargets = [
[sdkClientMethodCSWithTTFactory, false],
[sdkClientMethodCSFactory, true]
];
test.each(testTargets)('main client', (sdkClientMethodCSFactory) => {
// @ts-expect-error
const sdkClientMethod = sdkClientMethodCSFactory(params);
// should return a function
expect(typeof sdkClientMethod).toBe('function');
// calling the function should return a client instance
const client = sdkClientMethod();
assertClientApi(client, params.sdkReadinessManager.sdkStatus);
// multiple calls should return the same instance
expect(sdkClientMethod()).toBe(client);
// `client.destroy` method should stop internal components (other client methods where validated in `client.spec.ts`)
client.destroy().then(() => {
expect(params.sdkReadinessManager.readinessManager.destroy).toBeCalledTimes(1);
expect(params.storage.destroy).toBeCalledTimes(1);
expect(params.syncManager.stop).toBeCalledTimes(1);
expect(params.syncManager.flush).toBeCalledTimes(1);
expect(params.signalListener.stop).toBeCalledTimes(1);
});
});
test.each(testTargets)('multiple clients', async (sdkClientMethodCSFactory, ignoresTT) => {
// @ts-expect-error
const sdkClientMethod = sdkClientMethodCSFactory(params);
// calling the function with a diferent key than settings, should return a new client instance
const newClients = new Set([
sdkClientMethod('other-key'), // new client
sdkClientMethod('other-key', 'other-tt'), // new client
sdkClientMethod({ matchingKey: 'other-key', bucketingKey: 'buck' }) // new client
]);
if (ignoresTT) expect(newClients.size).toBe(2);
else expect(newClients.size).toBe(3);
// each new client must follog the Client API
newClients.forEach(newClient => {
assertClientApi(newClient);
expect(newClient).not.toBe(sdkClientMethod());
});
// shared methods call once per each new client
expect(params.storage.shared).toBeCalledTimes(newClients.size);
expect(params.sdkReadinessManager.shared).toBeCalledTimes(newClients.size);
expect(params.syncManager.shared).toBeCalledTimes(newClients.size + 1);
// `client.destroy` should flush and stop partial components
await Promise.all(Array.from(newClients).map(newClient => newClient.destroy()));
partialSdkReadinessManagers.forEach((partialSdkReadinessManager) => expect(partialSdkReadinessManager.readinessManager.destroy).toBeCalledTimes(1));
partialStorages.forEach((partialStorage) => expect(partialStorage.destroy).toBeCalledTimes(1));
mySegmentsSyncManagers.slice(1).forEach((mySegmentsSyncManager) => expect(mySegmentsSyncManager.stop).toBeCalledTimes(1));
expect(params.syncManager.flush).toBeCalledTimes(newClients.size);
// `client.destroy` shouldn't stop main components
expect(params.sdkReadinessManager.readinessManager.destroy).not.toBeCalled();
expect(params.storage.destroy).not.toBeCalled();
expect(params.syncManager.stop).not.toBeCalled();
expect(params.signalListener.stop).not.toBeCalled();
// Except the last client is destroyed
await sdkClientMethod().destroy();
expect(params.sdkReadinessManager.readinessManager.destroy).toBeCalledTimes(1);
expect(params.storage.destroy).toBeCalledTimes(1);
expect(params.syncManager.stop).toBeCalledTimes(1);
expect(params.syncManager.flush).toBeCalledTimes(newClients.size + 1);
expect(params.signalListener.stop).toBeCalledTimes(1);
});
test.each(testTargets)('return main client instance if called with same key', (sdkClientMethodCSFactory) => {
params.settings = settingsWithKey;
// @ts-expect-error
const sdkClientMethod = sdkClientMethodCSFactory(params);
expect(sdkClientMethod()).toBe(sdkClientMethod(settingsWithKey.core.key));
expect(params.storage.shared).not.toBeCalled();
expect(params.sdkReadinessManager.shared).not.toBeCalled();
expect(params.syncManager.shared).toBeCalledTimes(1);
});
test.each(testTargets)('return main client instance if called with same key and TT', (sdkClientMethodCSFactory) => {
params.settings = settingsWithKeyAndTT;
// @ts-expect-error
const sdkClientMethod = sdkClientMethodCSFactory(params);
expect(sdkClientMethod()).toBe(sdkClientMethod(settingsWithKeyAndTT.core.key, settingsWithKeyAndTT.core.trafficType));
expect(params.storage.shared).not.toBeCalled();
expect(params.sdkReadinessManager.shared).not.toBeCalled();
expect(params.syncManager.shared).toBeCalledTimes(1);
});
test.each(testTargets)('return main client instance if called with same key object', (sdkClientMethodCSFactory) => {
// @ts-expect-error
params.settings = settingsWithKeyObject;
// @ts-expect-error
const sdkClientMethod = sdkClientMethodCSFactory(params);
expect(sdkClientMethod()).toBe(sdkClientMethod({ matchingKey: settingsWithKeyObject.core.key.matchingKey, bucketingKey: settingsWithKeyObject.core.key.bucketingKey }));
expect(params.storage.shared).not.toBeCalled();
expect(params.sdkReadinessManager.shared).not.toBeCalled();
expect(params.syncManager.shared).toBeCalledTimes(1);
});
test.each(testTargets)('return same client instance if called with same key or traffic type (input validation)', (sdkClientMethodCSFactory, ignoresTT) => {
// @ts-expect-error
const sdkClientMethod = sdkClientMethodCSFactory(params);
const clientInstance = sdkClientMethod('key', 'tt');
expect(sdkClientMethod('key', 'tT')).toBe(clientInstance); // No new client created: TT is lowercased / ignored
expect(sdkClientMethod(' key ', 'tt')).toBe(clientInstance); // No new client created: key is trimmed
expect(sdkClientMethod({ matchingKey: 'key ', bucketingKey: ' key' }, 'TT')).toBe(clientInstance); // No new client created: key object is equivalent to 'key' string
expect(params.storage.shared).toBeCalledTimes(1);
expect(params.sdkReadinessManager.shared).toBeCalledTimes(1);
expect(params.syncManager.shared).toBeCalledTimes(2);
expect(sdkClientMethod('KEY', 'tt')).not.toBe(clientInstance); // New client created: key is case-sensitive
if (!ignoresTT) expect(sdkClientMethod('key', 'TT ')).not.toBe(clientInstance); // New client created: TT is not trimmed
const clientCount = ignoresTT ? 2 : 3;
expect(params.storage.shared).toBeCalledTimes(clientCount);
expect(params.sdkReadinessManager.shared).toBeCalledTimes(clientCount);
expect(params.syncManager.shared).toBeCalledTimes(clientCount + 1);
});
test.each(testTargets)('invalid calls throw an error', (sdkClientMethodCSFactory, ignoresTT) => {
// @ts-expect-error
const sdkClientMethod = sdkClientMethodCSFactory(params);
expect(() => sdkClientMethod({ matchingKey: settingsWithKey.core.key, bucketingKey: undefined })).toThrow('Shared Client needs a valid key.');
if (!ignoresTT) expect(() => sdkClientMethod('valid-key', ['invalid-TT'])).toThrow('Shared Client needs a valid traffic type or no traffic type at all.');
});
test.each(testTargets)('attributes binding - main client', (sdkClientMethodCSFactory) => {
// @ts-expect-error
const sdkClientMethod = sdkClientMethodCSFactory(params);
// should return a function
expect(typeof sdkClientMethod).toBe('function');
// calling the function should return a client instance
const client = sdkClientMethod();
assertClientApi(client, params.sdkReadinessManager.sdkStatus);
expect(client.setAttribute('attributeName1', 'attributeValue1')).toEqual(true);
expect(client.setAttribute('attributeName2', 'attributeValue2')).toEqual(true);
expect(client.setAttribute('', 'empty')).toEqual(false); // Attribute name should not be an empty string
expect(client.setAttribute({ '': 'empty' })).toEqual(false); // Attribute name should not be an empty string
invalidAttributes.forEach(invalidValue => {
expect(client.setAttribute('attributeName', invalidValue)).toEqual(false);
expect(client.setAttributes({ attributeName: invalidValue })).toEqual(false);
});
expect(client.getAttributes()).toEqual({ attributeName1: 'attributeValue1', attributeName2: 'attributeValue2' });
validAttributes.forEach(validValue => {
expect(client.setAttribute('attributeName', validValue)).toEqual(true);
});
validAttributes.forEach(validValue => {
expect(client.setAttributes({ attributeName: validValue })).toEqual(true);
});
expect(client.getAttributes()).toEqual({ attributeName: false, attributeName1: 'attributeValue1', attributeName2: 'attributeValue2' });
expect(client.removeAttribute('attributeName1')).toEqual(true);
expect(client.getAttribute('attributeName1')).toEqual(undefined);
expect(client.getAttribute('attributeName2')).toEqual('attributeValue2');
expect(client.setAttributes({
'attributeName3': 'attributeValue3',
'attributeName4': 'attributeValue4'
})).toEqual(true);
expect(client.getAttribute('attributeName2')).toEqual('attributeValue2');
expect(client.getAttribute('attributeName3')).toEqual('attributeValue3');
expect(client.getAttribute('attributeName4')).toEqual('attributeValue4');
expect(client.clearAttributes()).toEqual(true);
expect(client.getAttributes()).toEqual({});
});
test.each(testTargets)('attributes binding - shared clients', (sdkClientMethodCSFactory) => {
// @ts-expect-error
const sdkClientMethod = sdkClientMethodCSFactory(params);
// should return a function
expect(typeof sdkClientMethod).toBe('function');
// calling the function should return a client instance
const emmanuelClient = sdkClientMethod('emmanuel@split.io');
const emilianoClient = sdkClientMethod('emiliano@split.io');
assertClientApi(emmanuelClient);
assertClientApi(emilianoClient);
expect(emmanuelClient.setAttribute('name', 'Emmanuel')).toEqual(true);
expect(emilianoClient.setAttribute('name', 'Emiliano')).toEqual(true);
expect(emmanuelClient.getAttribute('name')).toEqual('Emmanuel');
expect(emilianoClient.getAttribute('name')).toEqual('Emiliano');
expect(emmanuelClient.setAttributes({ email: 'emmanuel@split.io' })).toEqual(true);
expect(emilianoClient.setAttributes({ email: 'emiliano@split.io' })).toEqual(true);
expect(emmanuelClient.getAttributes()).toEqual({ name: 'Emmanuel', email: 'emmanuel@split.io' });
expect(emilianoClient.getAttributes()).toEqual({ name: 'Emiliano', email: 'emiliano@split.io' });
expect(emmanuelClient.clearAttributes()).toEqual(true);
expect(emmanuelClient.getAttributes()).toEqual({});
expect(emilianoClient.getAttributes()).toEqual({ name: 'Emiliano', email: 'emiliano@split.io' });
});
});