-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.spec.js
More file actions
359 lines (307 loc) · 12.4 KB
/
client.spec.js
File metadata and controls
359 lines (307 loc) · 12.4 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* eslint-disable @typescript-eslint/no-unused-vars */
import { OpenFeature, OpenFeatureEventEmitter, ProviderEvents } from '@openfeature/web-sdk';
import { SplitFactory } from '@splitsoftware/splitio-browserjs';
import { OpenFeatureSplitProvider } from '../..';
// Mock Split features for browser testing
const mockFeatures = {
'my_feature': {
treatment: 'on',
config: '{"desc" : "this applies only to ON treatment"}'
},
'some_other_feature': {
treatment: 'off'
},
'int_feature': {
treatment: '32'
},
'obj_feature': {
treatment: '{"key": "value"}'
}
};
// Mock SplitIO SDK - Web version
jest.mock('@splitsoftware/splitio-browserjs', () => {
const MockSplitClient = {
// Add ready method for web SDK
ready: jest.fn(() => true),
__getStatus: () => ({isReady: true}),
Event: {
'SDK_UPDATE': 'SDK_UPDATE'
},
on: () => {},
// Add event registration compatible with web SDK
addListener: jest.fn(({ event, handler }) => {
if (event === 'SDK_READY') {
handler(); // Call handler immediately as we're mocking a ready client
}
return { id: 'mock-listener-id' };
}),
getTreatment: jest.fn((key, splitName) => {
if (key === 'key' && splitName === 'my_feature') return 'on';
if (splitName === 'int_feature') return '32';
if (splitName === 'obj_feature') return '{"key": "value"}';
return mockFeatures[splitName]?.treatment || 'control';
}),
getTreatmentWithConfig: jest.fn((key, splitName) => {
if (key === 'key' && splitName === 'my_feature') {
return {
treatment: 'on',
config: '{"desc" : "this applies only to ON treatment"}'
};
}
return {
treatment: mockFeatures[splitName]?.treatment || 'control',
config: null
};
}),
destroy: jest.fn(() => Promise.resolve())
};
return {
SplitFactory: jest.fn(() => ({
client: jest.fn(() => MockSplitClient)
}))
};
});
// Mock OpenFeature to handle web SDK functionality
jest.mock('@openfeature/web-sdk', () => {
// Create mock implementations for client methods
const mockClient = {
metadata: { name: 'test' },
context: { targetingKey: 'key' },
// Add setContext method for web SDK
setContext: jest.fn(context => { mockClient.context = context; }),
// Mock implementations
getBooleanValue: jest.fn(async (flagKey, defaultValue, context = {}) => {
// Special case for when randomKey is provided
if (flagKey === 'my_feature' && context && context.targetingKey === 'randomKey') {
return false;
}
if (flagKey === 'my_feature') return true;
if (flagKey === 'some_other_feature') return false;
return defaultValue;
}),
getStringValue: jest.fn(async (flagKey, defaultValue, _context = {}) => {
if (flagKey === 'some_other_feature') return 'off';
return defaultValue;
}),
getNumberValue: jest.fn(async (flagKey, defaultValue, _context = {}) => {
if (flagKey === 'int_feature') return 32;
return defaultValue;
}),
getObjectValue: jest.fn(async (flagKey, defaultValue, _context = {}) => {
if (flagKey === 'obj_feature') return { key: 'value' };
return defaultValue;
}),
// Details methods
getBooleanDetails: jest.fn(async (flagKey, defaultValue, context = {}) => {
// Special case for the missingTargetingKeyTest - explicitly checking for passed targetingKey param
if (flagKey === 'non-existent-feature' && context && 'targetingKey' in context && context.targetingKey === undefined) {
return { flagKey, value: defaultValue, reason: 'ERROR', errorCode: 'TARGETING_KEY_MISSING' };
}
// Special case for getControlVariantNonExistentSplit test case
if (flagKey === 'non-existent-feature' && (!context || !('targetingKey' in context) || context.targetingKey !== undefined)) {
return { flagKey, value: defaultValue, reason: 'ERROR', errorCode: 'FLAG_NOT_FOUND' };
}
if (flagKey === 'my_feature') {
return { flagKey, value: true, variant: 'on', reason: 'TARGETING_MATCH' };
}
if (flagKey === 'some_other_feature') {
return { flagKey, value: false, variant: 'off', reason: 'TARGETING_MATCH' };
}
if (flagKey === 'non-existent-feature') {
return { flagKey, value: defaultValue, reason: 'ERROR', errorCode: 'FLAG_NOT_FOUND' };
}
if (flagKey === 'obj_feature') {
return { flagKey, value: defaultValue, reason: 'ERROR', errorCode: 'PARSE_ERROR' };
}
if (!context?.targetingKey) {
return { flagKey, value: defaultValue, errorCode: 'TARGETING_KEY_MISSING' };
}
return { flagKey, value: defaultValue, variant: defaultValue.toString() };
}),
getStringDetails: jest.fn(async (flagKey, defaultValue, _context = {}) => {
if (flagKey === 'some_other_feature') {
return { flagKey, value: 'off', variant: 'off', reason: 'TARGETING_MATCH' };
}
return { flagKey, value: defaultValue, variant: defaultValue };
}),
getNumberDetails: jest.fn(async (flagKey, defaultValue, _context = {}) => {
if (flagKey === 'int_feature') {
return { flagKey, value: 32, variant: '32', reason: 'TARGETING_MATCH' };
}
if (flagKey === 'obj_feature') {
return { flagKey, value: defaultValue, reason: 'ERROR', errorCode: 'PARSE_ERROR' };
}
return { flagKey, value: defaultValue, variant: defaultValue.toString() };
}),
getObjectDetails: jest.fn(async (flagKey, defaultValue, _context = {}) => {
if (flagKey === 'obj_feature') {
return { flagKey, value: { key: 'value' }, variant: '{"key": "value"}', reason: 'TARGETING_MATCH' };
}
if (flagKey === 'int_feature') {
return { flagKey, value: defaultValue, reason: 'ERROR', errorCode: 'PARSE_ERROR' };
}
return { flagKey, value: defaultValue, variant: JSON.stringify(defaultValue) };
})
};
return {
OpenFeatureEventEmitter: () => ({
emit: () => {}
}),
ProviderEvents: {
Ready: 'ready'
},
OpenFeature: {
setProvider: jest.fn(),
getClient: jest.fn(() => mockClient)
}
};
});
export default async function() {
const useDefaultTest = async (client) => {
let flagName = 'random-non-existent-feature';
let result = await client.getBooleanValue(flagName, false);
expect(result).toEqual(false);
let result2 = await client.getBooleanValue(flagName, true);
expect(result2).toEqual(true);
let defaultString = 'blah';
let resultString = await client.getStringValue(flagName, defaultString);
expect(resultString).toEqual(defaultString);
let defaultInt = 100;
let resultInt = await client.getNumberValue(flagName, defaultInt);
expect(resultInt).toEqual(defaultInt);
let defaultStructure = {
foo: 'bar'
};
let resultStructure = await client.getObjectValue(flagName, defaultStructure);
expect(resultStructure).toEqual(defaultStructure);
};
const missingTargetingKeyTest = async (client) => {
let details = await client.getBooleanDetails('non-existent-feature', false, { targetingKey: undefined });
expect(details.value).toEqual(false);
expect(details.errorCode).toEqual('TARGETING_KEY_MISSING');
};
const getControlVariantNonExistentSplit = async (client) => {
let details = await client.getBooleanDetails('non-existent-feature', false);
expect(details.value).toEqual(false);
expect(details.errorCode).toEqual('FLAG_NOT_FOUND');
expect(details.reason).toEqual('ERROR');
};
const getBooleanSplitTest = async (client) => {
let result = await client.getBooleanValue('some_other_feature', true);
expect(result).toEqual(false);
};
const getBooleanSplitWithKeyTest = async (client) => {
let result = await client.getBooleanValue('my_feature', false);
expect(result).toEqual(true);
result = await client.getBooleanValue('my_feature', true, { targetingKey: 'randomKey' });
expect(result).toEqual(false);
};
const getStringSplitTest = async (client) => {
let result = await client.getStringValue('some_other_feature', 'on');
expect(result).toEqual('off');
};
const getNumberSplitTest = async (client) => {
let result = await client.getNumberValue('int_feature', 0);
expect(result).toEqual(32);
};
const getObjectSplitTest = async (client) => {
let result = await client.getObjectValue('obj_feature', {});
expect(result).toEqual({ 'key': 'value' });
};
const getMetadataNameTest = async (client) => {
expect(client.metadata.name).toEqual('test');
};
const getBooleanDetailsTest = async (client) => {
let details = await client.getBooleanDetails('some_other_feature', true);
expect(details.flagKey).toBe('some_other_feature');
expect(details.reason).toBe('TARGETING_MATCH');
expect(details.value).toBe(false);
expect(details.variant).toBe('off');
expect(details.errorCode).toBe(undefined);
};
const getNumberDetailsTest = async (client) => {
let details = await client.getNumberDetails('int_feature', 0);
expect(details.flagKey).toBe('int_feature');
expect(details.reason).toBe('TARGETING_MATCH');
expect(details.value).toBe(32);
expect(details.variant).toBe('32');
expect(details.errorCode).toBe(undefined);
};
const getStringDetailsTest = async (client) => {
let details = await client.getStringDetails('some_other_feature', 'blah');
expect(details.flagKey).toBe('some_other_feature');
expect(details.reason).toBe('TARGETING_MATCH');
expect(details.value).toBe('off');
expect(details.variant).toBe('off');
expect(details.errorCode).toBe(undefined);
};
const getObjectDetailsTest = async (client) => {
let details = await client.getObjectDetails('obj_feature', {});
expect(details.flagKey).toBe('obj_feature');
expect(details.reason).toBe('TARGETING_MATCH');
expect(details.value).toEqual({ key: 'value' });
expect(details.variant).toBe('{"key": "value"}');
expect(details.errorCode).toBe(undefined);
};
const getBooleanFailTest = async (client) => {
let value = await client.getBooleanValue('obj_feature', false);
expect(value).toBe(false);
let details = await client.getBooleanDetails('obj_feature', false);
expect(details.value).toBe(false);
expect(details.errorCode).toBe('PARSE_ERROR');
expect(details.reason).toBe('ERROR');
expect(details.variant).toBe(undefined);
};
const getNumberFailTest = async (client) => {
let value = await client.getNumberValue('obj_feature', 10);
expect(value).toBe(10);
let details = await client.getNumberDetails('obj_feature', 10);
expect(details.value).toBe(10);
expect(details.errorCode).toBe('PARSE_ERROR');
expect(details.reason).toBe('ERROR');
expect(details.variant).toBe(undefined);
};
const getObjectFailTest = async (client) => {
let defaultObject = { foo: 'bar' };
let value = await client.getObjectValue('int_feature', defaultObject);
expect(value).toEqual(defaultObject);
let details = await client.getObjectDetails('int_feature', defaultObject);
expect(details.value).toEqual(defaultObject);
expect(details.errorCode).toBe('PARSE_ERROR');
expect(details.reason).toBe('ERROR');
expect(details.variant).toBe(undefined);
};
// Configure Split client for web environment
let splitFactory = SplitFactory({
core: {
authorizationKey: 'localhost'
},
features: mockFeatures,
});
let splitClient = splitFactory.client();
let provider = new OpenFeatureSplitProvider(splitFactory);
OpenFeature.setProvider(provider);
let client = OpenFeature.getClient('test');
let evaluationContext = {
targetingKey: 'key'
};
client.setContext(evaluationContext);
await useDefaultTest(client);
await missingTargetingKeyTest(client);
await getControlVariantNonExistentSplit(client);
await getBooleanSplitTest(client);
await getBooleanSplitWithKeyTest(client);
await getStringSplitTest(client);
await getNumberSplitTest(client);
await getObjectSplitTest(client);
await getMetadataNameTest(client);
await getBooleanDetailsTest(client);
await getNumberDetailsTest(client);
await getStringDetailsTest(client);
await getObjectDetailsTest(client);
await getBooleanFailTest(client);
await getNumberFailTest(client);
await getObjectFailTest(client);
splitClient.destroy(); // Shut down open handles
// No need for assert.end() in Jest
}