@newskit-render/feature-flags 0.0.0-014af081

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,665 @@
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
13
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
14
+ return new (P || (P = Promise))(function (resolve, reject) {
15
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
16
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
17
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
18
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
19
+ });
20
+ };
21
+ var __generator = (this && this.__generator) || function (thisArg, body) {
22
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
23
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
24
+ function verb(n) { return function (v) { return step([n, v]); }; }
25
+ function step(op) {
26
+ if (f) throw new TypeError("Generator is already executing.");
27
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
28
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
29
+ if (y = 0, t) op = [op[0] & 2, t.value];
30
+ switch (op[0]) {
31
+ case 0: case 1: t = op; break;
32
+ case 4: _.label++; return { value: op[1], done: false };
33
+ case 5: _.label++; y = op[1]; op = [0]; continue;
34
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
35
+ default:
36
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
37
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
38
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
39
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
40
+ if (t[2]) _.ops.pop();
41
+ _.trys.pop(); continue;
42
+ }
43
+ op = body.call(thisArg, _);
44
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
45
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
46
+ }
47
+ };
48
+ import { setLogLevel, setLogger, } from '@optimizely/optimizely-sdk';
49
+ import { _resetInstance, initAndGetFeatureFlag } from '../utils';
50
+ var originalEnv = process.env;
51
+ jest.mock('@optimizely/optimizely-sdk', function () { return (__assign(__assign({}, jest.requireActual('@optimizely/optimizely-sdk')), { createInstance: function () { return (__assign(__assign({}, jest.requireActual('@optimizely/optimizely-sdk').createInstance), { createUserContext: function () { return ({
52
+ decide: jest
53
+ .fn()
54
+ .mockImplementation(function (key, options) {
55
+ return flagsMock[key];
56
+ }),
57
+ decideForKeys: jest
58
+ .fn()
59
+ .mockImplementation(function (keys, options) {
60
+ var filtered = {};
61
+ keys.forEach(function (k) { return (filtered[k] = flagsMock[k]); });
62
+ return filtered;
63
+ }),
64
+ decideAll: jest
65
+ .fn()
66
+ .mockImplementation(function (options) {
67
+ return flagsMock;
68
+ }),
69
+ }); }, onReady: jest.fn().mockResolvedValue({ success: true }), close: jest.fn().mockResolvedValue({ success: true }) })); }, setLogLevel: jest.fn(), setLogger: jest.fn() })); });
70
+ var flagsMock = {
71
+ qualtrics_flag: {
72
+ variationKey: 'on',
73
+ enabled: true,
74
+ variables: {},
75
+ ruleKey: 'default-rollout-216689-29200910123',
76
+ flagKey: 'qualtrics_flag',
77
+ userContext: {
78
+ _qualifiedSegments: null,
79
+ // optimizely: [e],
80
+ userId: 'user123',
81
+ attributes: {},
82
+ forcedDecisionsMap: {},
83
+ },
84
+ reasons: [],
85
+ },
86
+ testing_flag: {
87
+ variationKey: 'max_10',
88
+ enabled: true,
89
+ variables: { max_family_members: 10 },
90
+ ruleKey: 'default-rollout-223709-29200910123',
91
+ flagKey: 'testing_flag',
92
+ userContext: {
93
+ _qualifiedSegments: null,
94
+ // optimizely: [e],
95
+ userId: 'user123',
96
+ attributes: {},
97
+ forcedDecisionsMap: {},
98
+ },
99
+ reasons: [],
100
+ },
101
+ another_test_flag_1: {
102
+ variationKey: 'off',
103
+ enabled: false,
104
+ variables: {},
105
+ ruleKey: 'default-rollout-225089-29200910123',
106
+ flagKey: 'another_test_flag_1',
107
+ userContext: {
108
+ _qualifiedSegments: null,
109
+ // optimizely: [e],
110
+ userId: 'user123',
111
+ attributes: {},
112
+ forcedDecisionsMap: {},
113
+ },
114
+ reasons: [],
115
+ },
116
+ testing_flag_with_variations_and_variables: {
117
+ variationKey: 'primary_with_new_weclome_message_and_max_accounts',
118
+ enabled: true,
119
+ variables: {
120
+ family_primary_welcome_message: 'This is the new message',
121
+ family_primary_enable_secondary_accounts: true,
122
+ family_primary_max_secondary_accounts: 10,
123
+ family_primary_metadata_json: {
124
+ metadata1: 'a',
125
+ },
126
+ },
127
+ ruleKey: 'default-rollout-225097-29200910123',
128
+ flagKey: 'testing_flag_with_variations_and_variables',
129
+ userContext: {
130
+ _qualifiedSegments: null,
131
+ // optimizely: [e],
132
+ userId: 'user123',
133
+ attributes: {},
134
+ forcedDecisionsMap: {},
135
+ },
136
+ reasons: [],
137
+ },
138
+ };
139
+ // TO DO: Improve test coverage
140
+ beforeEach(function () {
141
+ jest.resetModules();
142
+ process.env = __assign(__assign({}, originalEnv), { OPTIMIZELY_SDK_KEY: 'optimizely-sdk-key' });
143
+ });
144
+ afterEach(function () {
145
+ _resetInstance();
146
+ });
147
+ describe('initAndGetFeatureFlag', function () {
148
+ it('should return all flags data in flat mode with default options', function () { return __awaiter(void 0, void 0, void 0, function () {
149
+ var flagsAll;
150
+ return __generator(this, function (_a) {
151
+ switch (_a.label) {
152
+ case 0: return [4 /*yield*/, initAndGetFeatureFlag()];
153
+ case 1:
154
+ flagsAll = _a.sent();
155
+ expect(flagsAll).toEqual({
156
+ another_test_flag_1: false,
157
+ qualtrics_flag: true,
158
+ testing_flag: true,
159
+ testing_flag_with_variations_and_variables: true,
160
+ });
161
+ return [2 /*return*/];
162
+ }
163
+ });
164
+ }); });
165
+ it('should return all flags with included variable data when "includeFlagVariables: true" is added in the options', function () { return __awaiter(void 0, void 0, void 0, function () {
166
+ var options, flagsAll;
167
+ return __generator(this, function (_a) {
168
+ switch (_a.label) {
169
+ case 0:
170
+ options = {
171
+ includeFlagVariables: true,
172
+ };
173
+ return [4 /*yield*/, initAndGetFeatureFlag(options)];
174
+ case 1:
175
+ flagsAll = _a.sent();
176
+ expect(flagsAll).toEqual({
177
+ qualtrics_flag: { variationKey: 'on', enabled: true, variables: {} },
178
+ testing_flag: {
179
+ variationKey: 'max_10',
180
+ enabled: true,
181
+ variables: { max_family_members: 10 },
182
+ },
183
+ another_test_flag_1: {
184
+ variationKey: 'off',
185
+ enabled: false,
186
+ variables: {},
187
+ },
188
+ testing_flag_with_variations_and_variables: {
189
+ variationKey: 'primary_with_new_weclome_message_and_max_accounts',
190
+ enabled: true,
191
+ variables: {
192
+ family_primary_welcome_message: 'This is the new message',
193
+ family_primary_enable_secondary_accounts: true,
194
+ family_primary_max_secondary_accounts: 10,
195
+ family_primary_metadata_json: {
196
+ metadata1: 'a',
197
+ },
198
+ },
199
+ },
200
+ });
201
+ return [2 /*return*/];
202
+ }
203
+ });
204
+ }); });
205
+ it('should return empty object if no flags are fetched', function () { return __awaiter(void 0, void 0, void 0, function () {
206
+ var initAndGetFeatureFlag, flagsAll;
207
+ return __generator(this, function (_a) {
208
+ switch (_a.label) {
209
+ case 0:
210
+ jest.doMock('@optimizely/optimizely-sdk', function () { return (__assign(__assign({}, jest.requireActual('@optimizely/optimizely-sdk')), { createInstance: function () { return (__assign(__assign({}, jest.requireActual('@optimizely/optimizely-sdk').createInstance), { createUserContext: function () { return ({
211
+ decideAll: jest
212
+ .fn()
213
+ .mockImplementation(function (options) {
214
+ return undefined;
215
+ }),
216
+ }); }, onReady: jest.fn().mockResolvedValue({ success: true }), close: jest.fn().mockResolvedValue({ success: true }) })); } })); });
217
+ initAndGetFeatureFlag = require('../utils').initAndGetFeatureFlag;
218
+ return [4 /*yield*/, initAndGetFeatureFlag()];
219
+ case 1:
220
+ flagsAll = _a.sent();
221
+ expect(flagsAll).toEqual({});
222
+ return [2 /*return*/];
223
+ }
224
+ });
225
+ }); });
226
+ it('should return all flags with custom options', function () { return __awaiter(void 0, void 0, void 0, function () {
227
+ var options, flagsAll;
228
+ return __generator(this, function (_a) {
229
+ switch (_a.label) {
230
+ case 0:
231
+ process.env = __assign(__assign({}, originalEnv), { OPTIMIZELY_SDK_KEY: undefined });
232
+ options = {
233
+ sdkConfig: {
234
+ sdkKey: 'custom-optimizely-sdk',
235
+ },
236
+ userData: {
237
+ userId: 'user456',
238
+ attributes: { logged_in: true, cpn: 'AAA123', subs_type: 'digi-print' },
239
+ },
240
+ includeFlagVariables: true,
241
+ };
242
+ return [4 /*yield*/, initAndGetFeatureFlag(options)];
243
+ case 1:
244
+ flagsAll = _a.sent();
245
+ expect(flagsAll).toEqual({
246
+ qualtrics_flag: { variationKey: 'on', enabled: true, variables: {} },
247
+ testing_flag: {
248
+ variationKey: 'max_10',
249
+ enabled: true,
250
+ variables: { max_family_members: 10 },
251
+ },
252
+ another_test_flag_1: {
253
+ variationKey: 'off',
254
+ enabled: false,
255
+ variables: {},
256
+ },
257
+ testing_flag_with_variations_and_variables: {
258
+ variationKey: 'primary_with_new_weclome_message_and_max_accounts',
259
+ enabled: true,
260
+ variables: {
261
+ family_primary_welcome_message: 'This is the new message',
262
+ family_primary_enable_secondary_accounts: true,
263
+ family_primary_max_secondary_accounts: 10,
264
+ family_primary_metadata_json: {
265
+ metadata1: 'a',
266
+ },
267
+ },
268
+ },
269
+ });
270
+ return [2 /*return*/];
271
+ }
272
+ });
273
+ }); });
274
+ it('should return empty object when called without sdk key', function () { return __awaiter(void 0, void 0, void 0, function () {
275
+ var flagsAll;
276
+ return __generator(this, function (_a) {
277
+ switch (_a.label) {
278
+ case 0:
279
+ process.env = __assign(__assign({}, originalEnv), { OPTIMIZELY_SDK_KEY: undefined });
280
+ return [4 /*yield*/, initAndGetFeatureFlag()];
281
+ case 1:
282
+ flagsAll = _a.sent();
283
+ expect(flagsAll).toEqual({});
284
+ return [2 /*return*/];
285
+ }
286
+ });
287
+ }); });
288
+ it('should return empty object when sdk not initialized', function () { return __awaiter(void 0, void 0, void 0, function () {
289
+ var initAndGetFeatureFlag, flagsAll;
290
+ return __generator(this, function (_a) {
291
+ switch (_a.label) {
292
+ case 0:
293
+ jest.doMock('@optimizely/optimizely-sdk', function () { return ({
294
+ createInstance: jest.fn().mockReturnValue(null),
295
+ }); });
296
+ initAndGetFeatureFlag = require('../utils').initAndGetFeatureFlag;
297
+ return [4 /*yield*/, initAndGetFeatureFlag()];
298
+ case 1:
299
+ flagsAll = _a.sent();
300
+ expect(flagsAll).toEqual({});
301
+ return [2 /*return*/];
302
+ }
303
+ });
304
+ }); });
305
+ it('should return empty object when user context not initialized', function () { return __awaiter(void 0, void 0, void 0, function () {
306
+ var initAndGetFeatureFlag, flagsAll;
307
+ return __generator(this, function (_a) {
308
+ switch (_a.label) {
309
+ case 0:
310
+ jest.doMock('@optimizely/optimizely-sdk', function () { return ({
311
+ createInstance: function () { return (__assign(__assign({}, jest.requireActual('@optimizely/optimizely-sdk').createInstance), { createUserContext: jest.fn().mockReturnValue(null), onReady: jest.fn().mockResolvedValue({ success: true }) })); },
312
+ }); });
313
+ initAndGetFeatureFlag = require('../utils').initAndGetFeatureFlag;
314
+ return [4 /*yield*/, initAndGetFeatureFlag()];
315
+ case 1:
316
+ flagsAll = _a.sent();
317
+ expect(flagsAll).toEqual({});
318
+ return [2 /*return*/];
319
+ }
320
+ });
321
+ }); });
322
+ it('should instantiate logger with log level', function () { return __awaiter(void 0, void 0, void 0, function () {
323
+ var options;
324
+ return __generator(this, function (_a) {
325
+ switch (_a.label) {
326
+ case 0:
327
+ options = {
328
+ logLevel: 'critical',
329
+ };
330
+ return [4 /*yield*/, initAndGetFeatureFlag(options)];
331
+ case 1:
332
+ _a.sent();
333
+ expect(setLogLevel).toBeCalledWith('critical');
334
+ expect(setLogger).toHaveBeenCalled();
335
+ return [2 /*return*/];
336
+ }
337
+ });
338
+ }); });
339
+ it('should return defaultFeatureFlags when they have been passed and optimizely client is not ready', function () { return __awaiter(void 0, void 0, void 0, function () {
340
+ var initAndGetFeatureFlag, options, flagsAll;
341
+ return __generator(this, function (_a) {
342
+ switch (_a.label) {
343
+ case 0:
344
+ jest.doMock('@optimizely/optimizely-sdk', function () { return ({
345
+ createInstance: function () { return (__assign(__assign({}, jest.requireActual('@optimizely/optimizely-sdk').createInstance), { createUserContext: function () { return ({
346
+ decideAll: jest
347
+ .fn()
348
+ .mockImplementation(function (options) {
349
+ return flagsMock;
350
+ }),
351
+ }); }, onReady: jest
352
+ .fn()
353
+ .mockResolvedValue({ success: false, reason: 'Promise rejected' }) })); },
354
+ }); });
355
+ initAndGetFeatureFlag = require('../utils').initAndGetFeatureFlag;
356
+ options = {
357
+ defaultFeatureFlags: {
358
+ default_flag: {
359
+ variationKey: 'off',
360
+ enabled: true,
361
+ variables: {},
362
+ },
363
+ },
364
+ };
365
+ return [4 /*yield*/, initAndGetFeatureFlag(options)];
366
+ case 1:
367
+ flagsAll = _a.sent();
368
+ expect(flagsAll).toEqual(options.defaultFeatureFlags);
369
+ return [2 /*return*/];
370
+ }
371
+ });
372
+ }); });
373
+ });
374
+ // describe('getFlagsAll', () => {
375
+ // it('should return defaultFeatureFlags when they have been passed and optimizely client is not ready', async () => {
376
+ // jest.doMock('@optimizely/optimizely-sdk', () => ({
377
+ // createInstance: () => ({
378
+ // ...jest.requireActual('@optimizely/optimizely-sdk').createInstance,
379
+ // createUserContext: () => ({
380
+ // decideAll: jest
381
+ // .fn()
382
+ // .mockImplementation((options?: OptimizelyDecideOption[]) => {
383
+ // return flagsMock as {
384
+ // [key: string]: Omit<OptimizelyDecision, 'userContext'>
385
+ // }
386
+ // }),
387
+ // }),
388
+ // onReady: jest
389
+ // .fn()
390
+ // .mockResolvedValue({ success: false, reason: 'Promise rejected' }),
391
+ // }),
392
+ // }))
393
+ // const { getFlagsAll } = require('../utils')
394
+ // const options: SDKConfig = {
395
+ // defaultFeatureFlags: {
396
+ // default_flag: {
397
+ // variationKey: 'off',
398
+ // enabled: true,
399
+ // variables: {},
400
+ // },
401
+ // },
402
+ // }
403
+ // const flagsAll = await getFlagsAll(options)
404
+ // expect(flagsAll).toEqual(options.defaultFeatureFlags)
405
+ // })
406
+ // it('should return empty object when no default flag have been passed and optimizely client is not ready', async () => {
407
+ // jest.doMock('@optimizely/optimizely-sdk', () => ({
408
+ // createInstance: () => ({
409
+ // ...jest.requireActual('@optimizely/optimizely-sdk').createInstance,
410
+ // createUserContext: () => ({
411
+ // decideAll: jest
412
+ // .fn()
413
+ // .mockImplementation((options?: OptimizelyDecideOption[]) => {
414
+ // return flagsMock as {
415
+ // [key: string]: Omit<OptimizelyDecision, 'userContext'>
416
+ // }
417
+ // }),
418
+ // }),
419
+ // onReady: jest
420
+ // .fn()
421
+ // .mockResolvedValue({ success: false, reason: 'Promise rejected' }),
422
+ // }),
423
+ // }))
424
+ // const { getFlagsAll } = require('../utils')
425
+ // const flagsAll = await getFlagsAll()
426
+ // expect(flagsAll).toEqual({})
427
+ // })
428
+ // })
429
+ // describe('closeFlagsInstance', () => {
430
+ // it('should close sdk instance successfully', async () => {
431
+ // const spy = jest.spyOn(console, 'log')
432
+ // await getFlagsAll()
433
+ // await closeFlagsInstance()
434
+ // expect(spy).toHaveBeenCalledWith('optimizely instance closed successfully')
435
+ // })
436
+ // it('should not close sdk instance sucessfully', async () => {
437
+ // const spy = jest.spyOn(console, 'log')
438
+ // jest.doMock('@optimizely/optimizely-sdk', () => ({
439
+ // createInstance: () => ({
440
+ // ...jest.requireActual('@optimizely/optimizely-sdk').createInstance,
441
+ // createUserContext: () => ({
442
+ // decide: jest
443
+ // .fn()
444
+ // .mockImplementation(
445
+ // (key: string, options?: OptimizelyDecideOption[]) => {
446
+ // return getFlagsAll as Omit<OptimizelyDecision, 'userContext'>
447
+ // }
448
+ // ),
449
+ // }),
450
+ // onReady: jest.fn().mockResolvedValue({ success: true }),
451
+ // close: jest
452
+ // .fn()
453
+ // .mockResolvedValue({ success: false, reason: 'Closing rejected' }),
454
+ // }),
455
+ // }))
456
+ // const { getFlagsAll, closeFlagsInstance } = require('../utils')
457
+ // await getFlagsAll()
458
+ // await closeFlagsInstance()
459
+ // expect(spy).toHaveBeenCalledWith(
460
+ // 'closing Optimizely instance failed, reason: Closing rejected'
461
+ // )
462
+ // })
463
+ // })
464
+ // describe('getFlag', () => {
465
+ // it('should return a flag with default options', async () => {
466
+ // const flag = await getFlag('testing_flag_with_variations_and_variables')
467
+ // expect(flag).toEqual({
468
+ // testing_flag_with_variations_and_variables: {
469
+ // variationKey: 'primary_with_new_weclome_message_and_max_accounts',
470
+ // enabled: true,
471
+ // variables: {
472
+ // family_primary_welcome_message: 'This is the new message',
473
+ // family_primary_enable_secondary_accounts: true,
474
+ // family_primary_max_secondary_accounts: 10,
475
+ // family_primary_metadata_json: {
476
+ // metadata1: 'a',
477
+ // },
478
+ // },
479
+ // },
480
+ // })
481
+ // })
482
+ // it('should return empty object if no flags are fetched', async () => {
483
+ // jest.doMock('@optimizely/optimizely-sdk', () => ({
484
+ // ...jest.requireActual('@optimizely/optimizely-sdk'),
485
+ // createInstance: () => ({
486
+ // ...jest.requireActual('@optimizely/optimizely-sdk').createInstance,
487
+ // createUserContext: () => ({
488
+ // decide: jest
489
+ // .fn()
490
+ // .mockImplementation((options?: OptimizelyDecideOption[]) => {
491
+ // return undefined
492
+ // }),
493
+ // }),
494
+ // onReady: jest.fn().mockResolvedValue({ success: true }),
495
+ // close: jest.fn().mockResolvedValue({ success: true }),
496
+ // }),
497
+ // }))
498
+ // const { getFlag } = require('../utils')
499
+ // const flagsAll = await getFlag('testing_flag_with_variations_and_variables')
500
+ // expect(flagsAll).toEqual({})
501
+ // })
502
+ // it('should return defaultFeatureFlags when they have been passed and optimizely client is not ready', async () => {
503
+ // jest.doMock('@optimizely/optimizely-sdk', () => ({
504
+ // createInstance: () => ({
505
+ // ...jest.requireActual('@optimizely/optimizely-sdk').createInstance,
506
+ // createUserContext: () => ({
507
+ // decide: jest
508
+ // .fn()
509
+ // .mockImplementation(
510
+ // (key: string, options?: OptimizelyDecideOption[]) => {
511
+ // return flagsMock[key] as Omit<OptimizelyDecision, 'userContext'>
512
+ // }
513
+ // ),
514
+ // }),
515
+ // onReady: jest
516
+ // .fn()
517
+ // .mockResolvedValue({ success: false, reason: 'Promise rejected' }),
518
+ // }),
519
+ // }))
520
+ // const { getFlag } = require('../utils')
521
+ // const options: SDKConfig = {
522
+ // defaultFeatureFlags: {
523
+ // default_flag: {
524
+ // variationKey: 'off',
525
+ // enabled: true,
526
+ // variables: {},
527
+ // },
528
+ // },
529
+ // }
530
+ // const flag = await getFlag(
531
+ // 'testing_flag_with_variations_and_variables',
532
+ // options
533
+ // )
534
+ // expect(flag).toEqual(options.defaultFeatureFlags)
535
+ // })
536
+ // it('should return empty object when no default flag have been passed and optimizely client is not ready', async () => {
537
+ // jest.doMock('@optimizely/optimizely-sdk', () => ({
538
+ // createInstance: () => ({
539
+ // ...jest.requireActual('@optimizely/optimizely-sdk').createInstance,
540
+ // createUserContext: () => ({
541
+ // decide: jest
542
+ // .fn()
543
+ // .mockImplementation(
544
+ // (key: string, options?: OptimizelyDecideOption[]) => {
545
+ // return flagsMock[key] as Omit<OptimizelyDecision, 'userContext'>
546
+ // }
547
+ // ),
548
+ // }),
549
+ // onReady: jest
550
+ // .fn()
551
+ // .mockResolvedValue({ success: false, reason: 'Promise rejected' }),
552
+ // }),
553
+ // }))
554
+ // const { getFlag } = require('../utils')
555
+ // const flag = await getFlag('testing_flag_with_variations_and_variables')
556
+ // expect(flag).toEqual({})
557
+ // })
558
+ // })
559
+ // describe('getFlagsByKeys', () => {
560
+ // it('should return flags by keys with default options', async () => {
561
+ // const flagsByKeys = await getFlagsByKeys([
562
+ // 'another_test_flag_1',
563
+ // 'qualtrics_flag',
564
+ // ])
565
+ // expect(flagsByKeys).toEqual({
566
+ // qualtrics_flag: { variationKey: 'on', enabled: true, variables: {} },
567
+ // another_test_flag_1: {
568
+ // variationKey: 'off',
569
+ // enabled: false,
570
+ // variables: {},
571
+ // },
572
+ // })
573
+ // })
574
+ // it('should return empty object if no flags are fetched', async () => {
575
+ // jest.doMock('@optimizely/optimizely-sdk', () => ({
576
+ // ...jest.requireActual('@optimizely/optimizely-sdk'),
577
+ // createInstance: () => ({
578
+ // ...jest.requireActual('@optimizely/optimizely-sdk').createInstance,
579
+ // createUserContext: () => ({
580
+ // decideForKeys: jest
581
+ // .fn()
582
+ // .mockImplementation((options?: OptimizelyDecideOption[]) => {
583
+ // return undefined
584
+ // }),
585
+ // }),
586
+ // onReady: jest.fn().mockResolvedValue({ success: true }),
587
+ // close: jest.fn().mockResolvedValue({ success: true }),
588
+ // }),
589
+ // }))
590
+ // const { getFlagsByKeys } = require('../utils')
591
+ // const flagsAll = await getFlagsByKeys([
592
+ // 'another_test_flag_1',
593
+ // 'qualtrics_flag',
594
+ // ])
595
+ // expect(flagsAll).toEqual({})
596
+ // })
597
+ // it('should return defaultFeatureFlags when they have been passed and optimizely client is not ready', async () => {
598
+ // jest.doMock('@optimizely/optimizely-sdk', () => ({
599
+ // createInstance: () => ({
600
+ // ...jest.requireActual('@optimizely/optimizely-sdk').createInstance,
601
+ // createUserContext: () => ({
602
+ // decideForKeys: jest
603
+ // .fn()
604
+ // .mockImplementation(
605
+ // (keys: string[], options?: OptimizelyDecideOption[]) => {
606
+ // const filtered: {
607
+ // [key: string]: Omit<OptimizelyDecision, 'userContext'>
608
+ // } = {}
609
+ // keys.forEach((k) => (filtered[k] = flagsMock[k]))
610
+ // return filtered
611
+ // }
612
+ // ),
613
+ // }),
614
+ // onReady: jest
615
+ // .fn()
616
+ // .mockResolvedValue({ success: false, reason: 'Promise rejected' }),
617
+ // }),
618
+ // }))
619
+ // const { getFlagsByKeys } = require('../utils')
620
+ // const options: SDKConfig = {
621
+ // defaultFeatureFlags: {
622
+ // default_flag: {
623
+ // variationKey: 'off',
624
+ // enabled: true,
625
+ // variables: {},
626
+ // },
627
+ // },
628
+ // }
629
+ // const flagsByKeys = await getFlagsByKeys(
630
+ // ['another_test_flag_1', 'qualtrics_flag'],
631
+ // options
632
+ // )
633
+ // expect(flagsByKeys).toEqual(options.defaultFeatureFlags)
634
+ // })
635
+ // it('should return empty object when no default flag have been passed and optimizely client is not ready', async () => {
636
+ // jest.doMock('@optimizely/optimizely-sdk', () => ({
637
+ // createInstance: () => ({
638
+ // ...jest.requireActual('@optimizely/optimizely-sdk').createInstance,
639
+ // createUserContext: () => ({
640
+ // decideForKeys: jest
641
+ // .fn()
642
+ // .mockImplementation(
643
+ // (keys: string[], options?: OptimizelyDecideOption[]) => {
644
+ // const filtered: {
645
+ // [key: string]: Omit<OptimizelyDecision, 'userContext'>
646
+ // } = {}
647
+ // keys.forEach((k) => (filtered[k] = flagsMock[k]))
648
+ // return filtered
649
+ // }
650
+ // ),
651
+ // }),
652
+ // onReady: jest
653
+ // .fn()
654
+ // .mockResolvedValue({ success: false, reason: 'Promise rejected' }),
655
+ // }),
656
+ // }))
657
+ // const { getFlagsByKeys } = require('../utils')
658
+ // const flagsByKeys = await getFlagsByKeys([
659
+ // 'another_test_flag_1',
660
+ // 'qualtrics_flag',
661
+ // ])
662
+ // expect(flagsByKeys).toEqual({})
663
+ // })
664
+ // })
665
+ //# sourceMappingURL=utils.tests.js.map