-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtests.js
More file actions
874 lines (759 loc) · 29.6 KB
/
tests.js
File metadata and controls
874 lines (759 loc) · 29.6 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
/* eslint-env es6, mocha */
/* eslint-parser babel-eslint */
const waitForCondition = async (conditionFn, timeout = 200, interval = 10) => {
return new Promise((resolve, reject) => {
const startTime = Date.now();
(function poll() {
if (conditionFn()) {
return resolve(undefined);
} else if (Date.now() - startTime > timeout) {
return reject(new Error('Timeout waiting for condition'));
} else {
setTimeout(poll, interval);
}
})();
});
};
describe('Rokt Forwarder', () => {
var ReportingService = function () {
var self = this;
this.id = null;
this.event = null;
this.cb = function (forwarder, event) {
self.id = forwarder.id;
self.event = event;
};
this.reset = function () {
this.id = null;
this.event = null;
};
};
var reportService = new ReportingService();
// -------------------DO NOT EDIT ANYTHING ABOVE THIS LINE-----------------------
// -------------------START EDITING BELOW:-----------------------
// -------------------mParticle stubs - Add any additional stubbing to our methods as needed-----------------------
mParticle.getEnvironment = function () {
return 'development';
};
mParticle.getVersion = function () {
return '1.2.3';
};
mParticle.Identity = {
getCurrentUser: function () {
return {
getMPID: function () {
return '123';
},
};
},
};
mParticle._getActiveForwarders = function () {
return [];
};
// -------------------START EDITING BELOW:-----------------------
var MockRoktForwarder = function () {
var self = this;
this.initializeCalled = false;
this.isInitialized = false;
this.accountId = null;
this.sandbox = null;
this.integrationName = null;
this.noFunctional = null;
this.noTargeting = null;
this.createLauncherCalled = false;
this.createLauncher = function (options) {
self.accountId = options.accountId;
self.integrationName = options.integrationName;
self.noFunctional = options.noFunctional;
self.noTargeting = options.noTargeting;
self.createLauncherCalled = true;
self.isInitialized = true;
self.sandbox = options.sandbox;
return Promise.resolve({
then: function (callback) {
callback();
},
});
};
this.currentLauncher = function () {};
};
before(() => {});
beforeEach(() => {
window.Rokt = new MockRoktForwarder();
window.mParticle.Rokt = window.Rokt;
});
afterEach(() => {
window.mParticle.forwarder.userAttributes = {};
delete window.mParticle.forwarder.launcherOptions;
delete window.mParticle.Rokt.launcherOptions;
});
describe('#initForwarder', () => {
beforeEach(() => {
window.Rokt = new MockRoktForwarder();
window.mParticle.Rokt = window.Rokt;
window.mParticle.Rokt.attachKitCalled = false;
window.mParticle.Rokt.attachKit = async (kit) => {
window.mParticle.Rokt.attachKitCalled = true;
window.mParticle.Rokt.kit = kit;
Promise.resolve();
};
window.mParticle.Rokt.filters = {
userAttributesFilters: [],
filterUserAttributes: function (attributes) {
return attributes;
},
filteredUser: {
getMPID: function () {
return '123';
},
},
};
});
it('should initialize the Rokt Web SDK', async () => {
await mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{}
);
window.Rokt.accountId.should.equal('123456');
window.Rokt.createLauncherCalled.should.equal(true);
});
it('should set sandbox to true if sandbox is true in managerOptions', async () => {
window.mParticle.Rokt.managerOptions = {
sandbox: true,
};
await mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true
);
window.Rokt.createLauncherCalled.should.equal(true);
window.Rokt.sandbox.should.equal(true);
});
it('should set sandbox to false if sandbox is false in managerOptions', async () => {
window.mParticle.Rokt.managerOptions = {
sandbox: false,
};
await mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true
);
window.Rokt.createLauncherCalled.should.equal(true);
window.Rokt.sandbox.should.equal(false);
});
it('should set optional settings from launcherOptions', async () => {
window.mParticle.Rokt.launcherOptions = {
integrationName: 'customName',
noFunctional: true,
noTargeting: true,
};
await mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{},
null,
null,
null
);
var expectedIntegrationName =
'mParticle_wsdkv_1.2.3_kitv_' +
require('../../package.json').version +
'_customName';
window.Rokt.createLauncherCalled.should.equal(true);
window.Rokt.accountId.should.equal('123456');
window.Rokt.integrationName.should.equal(expectedIntegrationName);
window.Rokt.noFunctional.should.equal(true);
window.Rokt.noTargeting.should.equal(true);
});
it('should set the filters on the forwarder', async () => {
await mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{}
);
await waitForCondition(() => window.mParticle.Rokt.attachKitCalled);
window.mParticle.Rokt.kit.filters.should.deepEqual({
userAttributesFilters: [],
filterUserAttributes: function (attributes) {
return attributes;
},
filteredUser: {
getMPID: function () {
return '123';
},
},
});
window.mParticle.Rokt.kit.filteredUser
.getMPID()
.should.equal('123');
});
it('should set integrationName in the correct format', async () => {
const packageVersion = require('../../package.json').version;
window.Rokt = new MockRoktForwarder();
window.mParticle.Rokt = window.Rokt;
window.mParticle.Rokt.attachKitCalled = false;
window.mParticle.Rokt.attachKit = async () => {
window.mParticle.Rokt.attachKitCalled = true;
return Promise.resolve();
};
await mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true
);
window.Rokt.integrationName.should.equal(
'mParticle_wsdkv_1.2.3_kitv_' + packageVersion
);
});
it('should append custom integration name to integrationName if passed in launcherOptions', async () => {
const packageVersion = require('../../package.json').version;
const customIntegrationName = 'myCustomIntegration';
window.Rokt = new MockRoktForwarder();
window.mParticle.Rokt = window.Rokt;
window.mParticle.Rokt.attachKitCalled = false;
window.mParticle.Rokt.attachKit = async () => {
window.mParticle.Rokt.attachKitCalled = true;
return Promise.resolve();
};
window.mParticle.Rokt.launcherOptions = {
integrationName: customIntegrationName,
};
// Simulate the forwarder appending the custom integration name
window.Rokt.integrationName =
'mParticle_wsdkv_1.2.3_kitv_' +
packageVersion +
'_' +
customIntegrationName;
await mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{},
null,
null,
null
);
window.Rokt.integrationName.should.equal(
'mParticle_wsdkv_1.2.3_kitv_' +
packageVersion +
'_' +
customIntegrationName
);
});
});
describe('#hashAttributes', () => {
beforeEach(() => {
window.Rokt = new MockRoktForwarder();
window.mParticle.Rokt = window.Rokt;
window.mParticle.Rokt.attachKitCalled = false;
window.mParticle.Rokt.attachKit = async (kit) => {
window.mParticle.Rokt.attachKitCalled = true;
window.mParticle.Rokt.kit = kit;
Promise.resolve();
};
window.mParticle.forwarder.launcher = {
hashAttributes: function (attributes) {
// Mocking the hashAttributes method to show that
// the attributes will be transformed by the launcher's
// hashAttributes method.
const hashedAttributes = {};
for (const key in attributes) {
if (attributes.hasOwnProperty(key)) {
hashedAttributes[key + '-hash'] =
'hashed-' + attributes[key];
}
}
window.mParticle.Rokt.hashedAttributes = hashedAttributes;
window.mParticle.Rokt.hashAttributesCalled = true;
return Promise.resolve(hashedAttributes);
},
};
});
it('should call launcher.hashAttributes with passed through attributes when fully initialized', function () {
// Ensure both initialization conditions are met
window.mParticle.forwarder.isInitialized = true;
window.mParticle.forwarder.launcher = {
hashAttributes: function (attributes) {
window.mParticle.Rokt.hashAttributesOptions = attributes;
window.mParticle.Rokt.hashAttributesCalled = true;
return {
'test-attribute': 'hashed-value',
};
},
};
var attributes = {
'test-attribute': 'test-value',
};
window.mParticle.forwarder.hashAttributes(attributes);
window.Rokt.hashAttributesCalled.should.equal(true);
window.Rokt.hashAttributesOptions.should.deepEqual(attributes);
});
it('should return null when launcher exists but kit is not initialized', function () {
// Set launcher but ensure isInitialized is false
window.mParticle.forwarder.isInitialized = false;
window.mParticle.forwarder.launcher = {
hashAttributes: function () {},
};
var result = window.mParticle.forwarder.hashAttributes({
'test-attribute': 'test-value',
});
(result === null).should.equal(true);
});
it('should log an error when called before initialization', function () {
var errorLogged = false;
var errorMessage = null;
window.console.error = function (message) {
errorLogged = true;
errorMessage = message;
};
// Ensure kit is not initialized
window.mParticle.forwarder.isInitialized = false;
window.mParticle.forwarder.launcher = null;
window.mParticle.forwarder.hashAttributes({
'test-attribute': 'test-value',
});
errorLogged.should.equal(true);
errorMessage.should.equal('Rokt Kit: Not initialized');
});
it('should return null when kit is initialized but launcher is missing', function () {
// Mock isInitialized but remove launcher
window.mParticle.forwarder.isInitialized = true;
window.mParticle.forwarder.launcher = null;
var result = window.mParticle.forwarder.hashAttributes({
'test-attribute': 'test-value',
});
(result === null).should.equal(true);
});
it('should log an error when kit is initialized but launcher is missing', function () {
var errorLogged = false;
var errorMessage = null;
window.console.error = function (message) {
errorLogged = true;
errorMessage = message;
};
window.mParticle.forwarder.isInitialized = true;
window.mParticle.forwarder.launcher = null;
window.mParticle.forwarder.hashAttributes({
'test-attribute': 'test-value',
});
errorLogged.should.equal(true);
errorMessage.should.equal('Rokt Kit: Not initialized');
});
it('should return hashed attributes from launcher', async () => {
await window.mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{}
);
const result = await window.mParticle.forwarder.hashAttributes({
'test-attribute': 'test-value',
});
result.should.deepEqual({
'test-attribute-hash': 'hashed-test-value',
});
});
});
describe('#selectPlacements', () => {
beforeEach(() => {
window.Rokt = new MockRoktForwarder();
window.mParticle.Rokt = window.Rokt;
window.mParticle.Rokt.attachKit = async () => {
window.mParticle.Rokt.attachKitCalled = true;
return Promise.resolve();
};
window.mParticle.forwarder.launcher = {
selectPlacements: function (options) {
window.mParticle.Rokt.selectPlacementsOptions = options;
window.mParticle.Rokt.selectPlacementsCalled = true;
},
};
window.mParticle.forwarder.filters = {
userAttributesFilters: [],
filterUserAttributes: function (attributes) {
return attributes;
},
filteredUser: {
getMPID: function () {
return '123';
},
},
};
});
it('should call launcher.selectPlacements with all passed through options', async () => {
await window.mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{}
);
await window.mParticle.forwarder.selectPlacements({
identifier: 'test-placement',
attributes: {
test: 'test',
},
});
window.Rokt.selectPlacementsCalled.should.equal(true);
window.Rokt.selectPlacementsOptions.should.deepEqual({
identifier: 'test-placement',
attributes: {
test: 'test',
mpid: '123',
},
});
});
it('should call launcher.selectPlacements with filtered user attributes', async () => {
window.mParticle.forwarder.filters.filterUserAttributes =
function () {
return {
'user-attribute': 'user-attribute-value',
'unfiltered-attribute': 'unfiltered-value',
};
};
await window.mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{}
);
await window.mParticle.forwarder.selectPlacements({
identifier: 'test-placement',
attributes: {
'unfiltered-attribute': 'unfiltered-value',
'filtered-attribute': 'filtered-value',
},
});
window.Rokt.selectPlacementsCalled.should.equal(true);
window.Rokt.selectPlacementsOptions.should.deepEqual({
identifier: 'test-placement',
attributes: {
'user-attribute': 'user-attribute-value',
'unfiltered-attribute': 'unfiltered-value',
mpid: '123',
},
});
});
it('should filter user attributes through filterUserAttributes function before sending to selectPlacements', async () => {
// Mocked filterUserAttributes function will return filtered attributes
// based on the config passed in the init method and will ultimately
// remove any attributes from the init method that are filtered.
// Also, any initial attributes from the init call that have updated
// durring runtime should be returned by the filterUserAttribute method.
window.mParticle.forwarder.filters.filterUserAttributes =
function () {
return {
'user-attribute': 'user-attribute-value',
'unfiltered-attribute': 'unfiltered-value',
'changed-attribute': 'new-value',
};
};
await window.mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{
// These should be filtered out
'blocked-attribute': 'blocked-value',
'initial-user-attribute': 'initial-user-attribute-value',
// This should be updated
'changed-attribute': 'old-value',
}
);
await window.mParticle.forwarder.selectPlacements({
identifier: 'test-placement',
attributes: {
// This should pass through
'unfiltered-attribute': 'unfiltered-value',
// This should be filtered out
'filtered-attribute': 'filtered-value',
},
});
window.Rokt.selectPlacementsCalled.should.equal(true);
window.Rokt.selectPlacementsOptions.should.deepEqual({
identifier: 'test-placement',
attributes: {
'user-attribute': 'user-attribute-value',
'unfiltered-attribute': 'unfiltered-value',
'changed-attribute': 'new-value',
mpid: '123',
},
});
});
it('should collect mpid and send to launcher.selectPlacements', async () => {
await window.mParticle.forwarder.init(
{
accountId: '123456',
},
reportService.cb,
true,
null,
{
'user-attribute': 'user-attribute-value',
}
);
await window.mParticle.forwarder.selectPlacements({
identifier: 'test-placement',
attributes: {
'user-attribute': 'user-attribute-value',
},
});
window.Rokt.selectPlacementsCalled.should.equal(true);
window.Rokt.selectPlacementsOptions.should.deepEqual({
identifier: 'test-placement',
attributes: {
'user-attribute': 'user-attribute-value',
mpid: '123',
},
});
});
});
describe('#setUserAttribute', () => {
it('should set the user attribute', async () => {
window.mParticle.forwarder.setUserAttribute(
'test-attribute',
'test-value'
);
window.mParticle.forwarder.userAttributes.should.deepEqual({
'test-attribute': 'test-value',
});
});
});
describe('#removeUserAttribute', () => {
it('should remove the user attribute', async () => {
window.mParticle.forwarder.setUserAttribute(
'test-attribute',
'test-value'
);
window.mParticle.forwarder.removeUserAttribute('test-attribute');
window.mParticle.forwarder.userAttributes.should.deepEqual({});
});
});
describe('#onUserIdentified', () => {
it('should set the filtered user', async () => {
window.mParticle.forwarder.onUserIdentified({
getAllUserAttributes: function () {
return {
'test-attribute': 'test-value',
};
},
getMPID: function () {
return '123';
},
});
window.mParticle.forwarder.userAttributes.should.deepEqual({
'test-attribute': 'test-value',
});
window.mParticle.forwarder.filteredUser
.getMPID()
.should.equal('123');
});
});
describe('#fetchOptimizely', () => {
// Helper functions for setting up Optimizely mocks
function setupValidOptimizelyMock(experiments) {
window.optimizely = {
get: function (key) {
if (key === 'state') {
return {
getActiveExperimentIds: function () {
return Object.keys(experiments);
},
getVariationMap: function () {
return experiments;
},
};
}
},
};
}
function setupInvalidOptimizelyMock(stateObject) {
window.optimizely = {
get: function (key) {
if (key === 'state') {
return stateObject;
}
},
};
}
// Common test setup
async function initAndSelectPlacements(settings = {}) {
await window.mParticle.forwarder.init(
{
accountId: '123456',
...settings,
},
reportService.cb,
true,
null,
{}
);
await window.mParticle.forwarder.selectPlacements({
identifier: 'test-placement',
attributes: {
test: 'test',
},
});
}
beforeEach(() => {
window.Rokt = new MockRoktForwarder();
window.mParticle.Rokt = window.Rokt;
window.mParticle.Rokt.attachKitCalled = false;
window.mParticle.Rokt.attachKit = async (kit) => {
window.mParticle.Rokt.attachKitCalled = true;
window.mParticle.Rokt.kit = kit;
Promise.resolve();
};
window.mParticle.forwarder.launcher = {
selectPlacements: function (options) {
window.mParticle.Rokt.selectPlacementsOptions = options;
window.mParticle.Rokt.selectPlacementsCalled = true;
},
};
window.mParticle.Rokt.filters = {
userAttributesFilters: [],
filterUserAttributes: function (attributes) {
return attributes;
},
filteredUser: {
getMPID: function () {
return '123';
},
},
};
window.mParticle._getActiveForwarders = function () {
return [{ name: 'Optimizely' }];
};
});
afterEach(() => {
delete window.optimizely;
});
describe('when Optimizely is properly configured', () => {
it('should fetch experiment data for single experiment', async () => {
setupValidOptimizelyMock({
exp1: { id: 'var1' },
});
await initAndSelectPlacements({
onboardingExpProvider: 'Optimizely',
});
window.Rokt.selectPlacementsOptions.attributes.should.have.property(
'rokt.custom.optimizely.experiment.exp1.variationId',
'var1'
);
});
it('should fetch experiment data for multiple experiments', async () => {
setupValidOptimizelyMock({
exp1: { id: 'var1' },
exp2: { id: 'var2' },
});
await initAndSelectPlacements({
onboardingExpProvider: 'Optimizely',
});
const attributes =
window.Rokt.selectPlacementsOptions.attributes;
attributes.should.have.property(
'rokt.custom.optimizely.experiment.exp1.variationId',
'var1'
);
attributes.should.have.property(
'rokt.custom.optimizely.experiment.exp2.variationId',
'var2'
);
});
});
describe('when Optimizely is not properly configured', () => {
it('should return empty object when Optimizely is not available', async () => {
delete window.optimizely;
await initAndSelectPlacements({
onboardingExpProvider: 'Optimizely',
});
window.Rokt.selectPlacementsOptions.attributes.should.not.have.property(
'rokt.custom.optimizely'
);
});
it('should return empty object when Optimizely state is undefined', async () => {
setupInvalidOptimizelyMock(undefined);
await initAndSelectPlacements({
onboardingExpProvider: 'Optimizely',
});
window.Rokt.selectPlacementsOptions.attributes.should.not.have.property(
'rokt.custom.optimizely'
);
});
it('should return empty object when Optimizely state has invalid format', async () => {
setupInvalidOptimizelyMock({
someOtherProperty: 'value',
invalidFunction: function () {
return null;
},
});
await initAndSelectPlacements({
onboardingExpProvider: 'Optimizely',
});
window.Rokt.selectPlacementsOptions.attributes.should.not.have.property(
'rokt.custom.optimizely'
);
});
it('should return empty object when Optimizely state is missing required methods', async () => {
setupInvalidOptimizelyMock({
getVariationMap: function () {
return {};
},
// Mocking a scenario for when getActiveExperimentIds() method is missing
});
await initAndSelectPlacements({
onboardingExpProvider: 'Optimizely',
});
window.Rokt.selectPlacementsOptions.attributes.should.not.have.property(
'rokt.custom.optimizely'
);
});
});
describe('when Optimizely is not the provider', () => {
it('should not fetch Optimizely data', async () => {
setupValidOptimizelyMock({
exp1: { id: 'var1' },
});
await initAndSelectPlacements({
onboardingExpProvider: 'NotOptimizely',
});
window.Rokt.selectPlacementsOptions.attributes.should.not.have.property(
'rokt.custom.optimizely'
);
});
});
});
});