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