forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_spec.js
1328 lines (1145 loc) · 41.5 KB
/
utils_spec.js
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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {getAdServerTargeting} from 'test/fixtures/fixtures.js';
import {expect} from 'chai';
import {TARGETING_KEYS} from 'src/constants.js';
import * as utils from 'src/utils.js';
import {binarySearch, deepEqual, encodeMacroURI, memoize, sizesToSizeTuples, waitForElementToLoad} from 'src/utils.js';
import {convertCamelToUnderscore} from '../../libraries/appnexusUtils/anUtils.js';
var assert = require('assert');
describe('Utils', function () {
var obj_string = 's',
obj_number = 1,
obj_object = {},
obj_array = [],
obj_function = function () {};
var type_string = 'String',
type_number = 'Number',
type_object = 'Object',
type_array = 'Array',
type_function = 'Function';
describe('canAccessWindowTop', function () {
let sandbox;
beforeEach(function () {
sandbox = sinon.sandbox.create();
});
afterEach(function () {
sandbox.restore();
});
it('should return true if window.top is accessible', function () {
assert.equal(utils.canAccessWindowTop(), true);
});
it('should return false if window.top is not accessible', function () {
sandbox.stub(utils.internal, 'getWindowTop').returns(false);
assert.equal(utils.canAccessWindowTop(), false);
});
});
describe('isSafeFrameWindow', function () {
// SafeFrames implementation
// https://iabtechlab.com/wp-content/uploads/2016/03/SafeFrames_v1.1_final.pdf
const $sf = {
ext: {
geom: function() {}
}
};
afterEach(function() {
delete window.$sf;
})
it('should return true if window.$sf is accessible', function () {
window.$sf = $sf;
assert.equal(utils.isSafeFrameWindow(), true);
});
it('should return false if window.$sf is missimplemented', function () {
window.$sf = {};
assert.equal(utils.isSafeFrameWindow(), false);
});
it('should return false if window.$sf is missing', function () {
assert.equal(utils.isSafeFrameWindow(), false);
});
});
describe('getBidIdParameter', function () {
it('should return value of the key in input object', function () {
var obj = {
a: 'valueA',
b: 'valueB'
};
var output = utils.getBidIdParameter('a', obj);
assert.equal(output, 'valueA');
});
it('should return empty string, if the key is not existsed in the object', function () {
var obj = {
a: 'valueA',
b: 'valueB'
};
var output = utils.getBidIdParameter('c', obj);
assert.equal(output, '');
});
});
describe('parseQueryStringParameters', function () {
it('should append query string to existing using the input obj', function () {
var obj = {
a: 'http://example.com/?foo=bar&bar=foo',
b: 'abc["def"]'
};
var output = utils.parseQueryStringParameters(obj);
var expectedResult = 'a=' + encodeURIComponent('http://example.com/?foo=bar&bar=foo') + '&b=' + encodeURIComponent('abc["def"]');
assert.equal(output, expectedResult);
});
it('should return an empty string, if input obj is empty', function () {
var obj = {};
var output = utils.parseQueryStringParameters(obj);
assert.equal(output, '');
});
});
describe('transformAdServerTargetingObj', function () {
it('should append query string to existing using the input obj', function () {
var obj = getAdServerTargeting();
var output = utils.transformAdServerTargetingObj(obj[Object.keys(obj)[0]]);
var expected = 'foobar=0x0%2C300x250%2C300x600&' + TARGETING_KEYS.SIZE + '=300x250&' + TARGETING_KEYS.PRICE_BUCKET + '=10.00&' + TARGETING_KEYS.AD_ID + '=233bcbee889d46d&' + TARGETING_KEYS.BIDDER + '=appnexus&' + TARGETING_KEYS.SIZE + '_triplelift=0x0&' + TARGETING_KEYS.PRICE_BUCKET + '_triplelift=10.00&' + TARGETING_KEYS.AD_ID + '_triplelift=222bb26f9e8bd&' + TARGETING_KEYS.BIDDER + '_triplelift=triplelift&' + TARGETING_KEYS.SIZE + '_appnexus=300x250&' + TARGETING_KEYS.PRICE_BUCKET + '_appnexus=10.00&' + TARGETING_KEYS.AD_ID + '_appnexus=233bcbee889d46d&' + TARGETING_KEYS.BIDDER + '_appnexus=appnexus&' + TARGETING_KEYS.SIZE + '_pagescience=300x250&' + TARGETING_KEYS.PRICE_BUCKET + '_pagescience=10.00&' + TARGETING_KEYS.AD_ID + '_pagescience=25bedd4813632d7&' + TARGETING_KEYS.BIDDER + '_pagescienc=pagescience&' + TARGETING_KEYS.SIZE + '_brightcom=300x250&' + TARGETING_KEYS.PRICE_BUCKET + '_brightcom=10.00&' + TARGETING_KEYS.AD_ID + '_brightcom=26e0795ab963896&' + TARGETING_KEYS.BIDDER + '_brightcom=brightcom&' + TARGETING_KEYS.SIZE + '_brealtime=300x250&' + TARGETING_KEYS.PRICE_BUCKET + '_brealtime=10.00&' + TARGETING_KEYS.AD_ID + '_brealtime=275bd666f5a5a5d&' + TARGETING_KEYS.BIDDER + '_brealtime=brealtime&' + TARGETING_KEYS.SIZE + '_pubmatic=300x250&' + TARGETING_KEYS.PRICE_BUCKET + '_pubmatic=10.00&' + TARGETING_KEYS.AD_ID + '_pubmatic=28f4039c636b6a7&' + TARGETING_KEYS.BIDDER + '_pubmatic=pubmatic&' + TARGETING_KEYS.SIZE + '_rubicon=300x600&' + TARGETING_KEYS.PRICE_BUCKET + '_rubicon=10.00&' + TARGETING_KEYS.AD_ID + '_rubicon=29019e2ab586a5a&' + TARGETING_KEYS.BIDDER + '_rubicon=rubicon';
assert.equal(output, expected);
});
it('should return an empty string, if input obj is empty', function () {
var obj = {};
var output = utils.transformAdServerTargetingObj(obj);
assert.equal(output, '');
});
});
describe('extend', function () {
it('should merge two input object', function () {
var target = {
a: '1',
b: '2'
};
var source = {
c: '3'
};
var expectedResult = {
a: '1',
b: '2',
c: '3'
};
var output = Object.assign(target, source);
assert.deepEqual(output, expectedResult);
});
it('should merge two input object even though target object is empty', function () {
var target = {};
var source = {
c: '3'
};
var output = Object.assign(target, source);
assert.deepEqual(output, source);
});
it('just return target object, if the source object is empty', function () {
var target = {
a: '1',
b: '2'
};
var source = {};
var output = Object.assign(target, source);
assert.deepEqual(output, target);
});
});
describe('sizesToSizeTuples', () => {
Object.entries({
'single size, numerical': {
in: [1, 2],
out: [[1, 2]]
},
'single size, numerical, nested': {
in: [[1, 2]],
out: [[1, 2]]
},
'multiple sizes, numerical': {
in: [[1, 2], [3, 4]],
out: [[1, 2], [3, 4]]
},
'single size, string': {
in: '1x2',
out: [[1, 2]]
},
'multiple sizes, string': {
in: '1x2, 4x3',
out: [[1, 2], [4, 3]]
},
'incorrect size, numerical': {
in: [1],
out: []
},
'incorrect size, string': {
in: '1x',
out: []
}
}).forEach(([t, {in: input, out}]) => {
it(`can parse ${t}`, () => {
expect(sizesToSizeTuples(input)).to.eql(out);
})
})
})
describe('parseSizesInput', function () {
it('should return query string using multi size array', function () {
var sizes = [[728, 90], [970, 90]];
var output = utils.parseSizesInput(sizes);
assert.deepEqual(output, ['728x90', '970x90']);
});
it('should return query string using single size array', function () {
var sizes = [728, 90];
var output = utils.parseSizesInput(sizes);
assert.deepEqual(output, ['728x90']);
});
it('should return query string using string input', function () {
var sizes = '300x250,970x90';
var output = utils.parseSizesInput(sizes);
assert.deepEqual(output, ['300x250', '970x90']);
});
it('return undefined if input array is empty', function () {
var sizes = [];
var output = utils.parseSizesInput(sizes);
assert.deepEqual(output, []);
});
});
describe('parseGPTSingleSizeArray', function () {
it('should return size string with input single size array', function () {
var size = [300, 250];
var output = utils.parseGPTSingleSizeArray(size);
assert.equal(output, '300x250');
});
it('should return size string with input single size array', function () {
var size = ['300', '250'];
var output = utils.parseGPTSingleSizeArray(size);
assert.equal(output, '300x250');
});
it('return undefined using string input', function () {
var size = '1';
var output = utils.parseGPTSingleSizeArray(size);
assert.equal(output, undefined);
});
it('return undefined using number input', function () {
var size = 1;
var output = utils.parseGPTSingleSizeArray(size);
assert.equal(output, undefined);
});
it('return undefined using one length single array', function () {
var size = [300];
var output = utils.parseGPTSingleSizeArray(size);
assert.equal(output, undefined);
});
it('return undefined if the input is empty', function () {
var size = '';
var output = utils.parseGPTSingleSizeArray(size);
assert.equal(output, undefined);
});
it('return undefined if the input is not a number', function () {
var size = ['foo', 'bar'];
var output = utils.parseGPTSingleSizeArray(size);
assert.equal(output, undefined);
});
it('return undefined if the input is not a number 2', function () {
var size = ['foo', 300];
var output = utils.parseGPTSingleSizeArray(size);
assert.equal(output, undefined);
});
});
describe('parseGPTSingleSizeArrayToRtbSize', function () {
it('should return size string with input single size array', function () {
var size = [300, 250];
var output = utils.parseGPTSingleSizeArrayToRtbSize(size);
assert.deepEqual(output, {w: 300, h: 250});
});
it('should return size string with input single size array', function () {
var size = ['300', '250'];
var output = utils.parseGPTSingleSizeArrayToRtbSize(size);
assert.deepEqual(output, {w: 300, h: 250});
});
it('return undefined using string input', function () {
var size = '1';
var output = utils.parseGPTSingleSizeArrayToRtbSize(size);
assert.equal(output, undefined);
});
it('return undefined using number input', function () {
var size = 1;
var output = utils.parseGPTSingleSizeArrayToRtbSize(size);
assert.equal(output, undefined);
});
it('return undefined using one length single array', function () {
var size = [300];
var output = utils.parseGPTSingleSizeArrayToRtbSize(size);
assert.equal(output, undefined);
});
it('return undefined if the input is empty', function () {
var size = '';
var output = utils.parseGPTSingleSizeArrayToRtbSize(size);
assert.equal(output, undefined);
});
it('return undefined if the input is not a number', function () {
var size = ['foo', 'bar'];
var output = utils.parseGPTSingleSizeArrayToRtbSize(size);
assert.equal(output, undefined);
});
it('return undefined if the input is not a number 2', function () {
var size = [300, 'foo'];
var output = utils.parseGPTSingleSizeArrayToRtbSize(size);
assert.equal(output, undefined);
});
});
describe('isA', function () {
it('should return true with string object', function () {
var output = utils.isA(obj_string, type_string);
assert.deepEqual(output, true);
});
it('should return false with object', function () {
var output = utils.isA(obj_object, type_string);
assert.deepEqual(output, false);
});
it('should return true with object', function () {
var output = utils.isA(obj_object, type_object);
assert.deepEqual(output, true);
});
it('should return false with array object', function () {
var output = utils.isA(obj_array, type_object);
assert.deepEqual(output, false);
});
it('should return true with array object', function () {
var output = utils.isA(obj_array, type_array);
assert.deepEqual(output, true);
});
it('should return false with array object', function () {
var output = utils.isA(obj_array, type_function);
assert.deepEqual(output, false);
});
it('should return true with function', function () {
var output = utils.isA(obj_function, type_function);
assert.deepEqual(output, true);
});
it('should return false with number', function () {
var output = utils.isA(obj_function, type_number);
assert.deepEqual(output, false);
});
it('should return true with number', function () {
var output = utils.isA(obj_number, type_number);
assert.deepEqual(output, true);
});
});
describe('isFn', function () {
it('should return true with input function', function () {
var output = utils.isFn(obj_function);
assert.deepEqual(output, true);
});
it('should return false with input string', function () {
var output = utils.isFn(obj_string);
assert.deepEqual(output, false);
});
it('should return false with input number', function () {
var output = utils.isFn(obj_number);
assert.deepEqual(output, false);
});
it('should return false with input Array', function () {
var output = utils.isFn(obj_array);
assert.deepEqual(output, false);
});
it('should return false with input object', function () {
var output = utils.isFn(obj_object);
assert.deepEqual(output, false);
});
});
describe('isStr', function () {
it('should return true with input string', function () {
var output = utils.isStr(obj_string);
assert.deepEqual(output, true);
});
it('should return false with input number', function () {
var output = utils.isStr(obj_number);
assert.deepEqual(output, false);
});
it('should return false with input object', function () {
var output = utils.isStr(obj_object);
assert.deepEqual(output, false);
});
it('should return false with input array', function () {
var output = utils.isStr(obj_array);
assert.deepEqual(output, false);
});
it('should return false with input function', function () {
var output = utils.isStr(obj_function);
assert.deepEqual(output, false);
});
});
describe('isArray', function () {
it('should return false with input string', function () {
var output = utils.isArray(obj_string);
assert.deepEqual(output, false);
});
it('should return false with input number', function () {
var output = utils.isArray(obj_number);
assert.deepEqual(output, false);
});
it('should return false with input object', function () {
var output = utils.isArray(obj_object);
assert.deepEqual(output, false);
});
it('should return true with input array', function () {
var output = utils.isArray(obj_array);
assert.deepEqual(output, true);
});
it('should return false with input function', function () {
var output = utils.isArray(obj_function);
assert.deepEqual(output, false);
});
});
describe('isPlainObject', function () {
it('should return false with input string', function () {
var output = utils.isPlainObject(obj_string);
assert.deepEqual(output, false);
});
it('should return false with input number', function () {
var output = utils.isPlainObject(obj_number);
assert.deepEqual(output, false);
});
it('should return true with input object', function () {
var output = utils.isPlainObject(obj_object);
assert.deepEqual(output, true);
});
it('should return false with input array', function () {
var output = utils.isPlainObject(obj_array);
assert.deepEqual(output, false);
});
it('should return false with input function', function () {
var output = utils.isPlainObject(obj_function);
assert.deepEqual(output, false);
});
});
describe('isEmpty', function () {
it('should return true with empty object', function () {
var output = utils.isEmpty(obj_object);
assert.deepEqual(output, true);
});
it('should return false with non-empty object', function () {
var obj = { a: 'b' };
var output = utils.isEmpty(obj);
assert.deepEqual(output, false);
});
it('should return false with null', function () {
var obj = null;
var output = utils.isEmpty(obj);
assert.deepEqual(output, true);
});
});
describe('contains', function () {
it('should return true if the input string contains in the input obj', function () {
var output = utils.contains('123', '1');
assert.deepEqual(output, true);
});
it('should return false if the input string do not contain in the input obj', function () {
var output = utils.contains('234', '1');
assert.deepEqual(output, false);
});
it('should return false if the input string is empty', function () {
var output = utils.contains();
assert.ok(!output, 'an empty string returns false');
});
});
describe('_map', function () {
it('return empty array when input object is empty', function () {
var input = {};
var callback = function () {};
var output = utils._map(input, callback);
assert.deepEqual(output, []);
});
it('return value array with vaild input object', function () {
var input = { a: 'A', b: 'B' };
var callback = function (v) { return v; };
var output = utils._map(input, callback);
assert.deepEqual(output, ['A', 'B']);
});
it('return value array with vaild input object_callback func changed 1', function () {
var input = { a: 'A', b: 'B' };
var callback = function (v, k) { return v + k; };
var output = utils._map(input, callback);
assert.deepEqual(output, ['Aa', 'Bb']);
});
it('return value array with vaild input object_callback func changed 2', function () {
var input = { a: 'A', b: 'B' };
var callback = function (v, k, o) { return o; };
var output = utils._map(input, callback);
assert.deepEqual(output, [input, input]);
});
});
describe('createInvisibleIframe', function () {
var output = utils.createInvisibleIframe();
it('return iframe - id', function () {
assert.ok(output.id);
});
it('return iframe - height', function () {
assert.deepEqual(output.height, 0);
});
it('return iframe - width', function () {
assert.deepEqual(output.width, 0);
});
it('return iframe - hspace', function () {
assert.deepEqual(output.hspace, '0');
});
it('return iframe - vspace', function () {
assert.deepEqual(output.vspace, '0');
});
it('return iframe - marginWidth', function () {
assert.deepEqual(output.marginWidth, '0');
});
it('return iframe - marginHeight', function () {
assert.deepEqual(output.marginHeight, '0');
});
it('return iframe - scrolling', function () {
assert.deepEqual(output.scrolling, 'no');
});
it('return iframe - frameBorder', function () {
assert.deepEqual(output.frameBorder, '0');
});
it('return iframe - src', function () {
assert.deepEqual(output.src, 'about:blank');
});
it('return iframe - style', function () {
assert.ok(output.style);
});
});
describe('polyfill test', function () {
it('should not add polyfill to array', function() {
var arr = ['hello', 'world'];
var count = 0;
for (var key in arr) {
count++;
}
assert.equal(arr.length, count, 'Polyfill test fails');
});
});
describe('delayExecution', function () {
it('should execute the core function after the correct number of calls', function () {
const callback = sinon.spy();
const delayed = utils.delayExecution(callback, 5);
for (let i = 0; i < 4; i++) {
delayed();
}
assert(callback.notCalled);
delayed(3);
assert(callback.called)
assert.equal(callback.firstCall.args[0], 3);
});
});
describe('deepAccess', function() {
var obj = {
1: 2,
test: {
first: 11
}
};
it('should allow deep access of object properties', function() {
var value1 = utils.deepAccess(obj, 'test');
assert.deepEqual(value1, obj.test);
var value2 = utils.deepAccess(obj, 'test.first');
assert.equal(value2, 11);
var value3 = utils.deepAccess(obj, '1');
assert.equal(value3, 2);
});
it('should allow safe access (returning undefined for missing properties and not throwing exceptions)', function() {
var value;
assert.doesNotThrow(function() {
value = utils.deepAccess(obj, 'test.second.third');
});
assert.equal(value, undefined);
});
});
describe('deepSetValue', function() {
it('should set existing properties at various depths', function() {
const testObj = {
prop: 'value',
nestedObj: {
nestedProp: 'nestedValue'
}
};
utils.deepSetValue(testObj, 'prop', 'newValue');
assert.equal(testObj.prop, 'newValue');
utils.deepSetValue(testObj, 'nestedObj.nestedProp', 'newNestedValue');
assert.equal(testObj.nestedObj.nestedProp, 'newNestedValue');
});
it('should create object levels between top and bottom of given path if they do not exist', function() {
const testObj = {};
utils.deepSetValue(testObj, 'level1.level2', 'value');
assert.notEqual(testObj.level1, undefined);
assert.notEqual(testObj.level1.level2, undefined);
assert.equal(testObj.level1.level2, 'value');
});
});
describe('getDefinedParams', function () {
it('builds an object consisting of defined params', function () {
const adUnit = {
mediaType: 'video',
comeWithMe: 'ifuwant2live',
notNeeded: 'do not include',
};
const builtObject = utils.getDefinedParams(adUnit, [
'mediaType', 'comeWithMe'
]);
assert.deepEqual(builtObject, {
mediaType: 'video',
comeWithMe: 'ifuwant2live',
});
});
});
describe('deepClone', function () {
it('deep copies objects', function () {
const adUnit = [{
code: 'swan',
mediaTypes: {video: {context: 'outstream'}},
renderer: {
render: bid => player.render(bid),
url: '/video/renderer.js'
},
bids: [{
bidder: 'dharmaInitiative',
params: { placementId: '481516', }
}],
}];
const adUnitCopy = utils.deepClone(adUnit);
expect(adUnitCopy[0].renderer.url).to.be.a('string');
expect(adUnitCopy[0].renderer.render).to.be.a('function');
});
});
describe('getUserConfiguredParams', function () {
const adUnits = [{
code: 'adUnit1',
bids: [{
bidder: 'bidder1',
params: {
key1: 'value1'
}
}, {
bidder: 'bidder2'
}]
}];
it('should return params configured', function () {
const output = utils.getUserConfiguredParams(adUnits, 'adUnit1', 'bidder1');
const expected = [{
key1: 'value1'
}];
assert.deepEqual(output, expected);
});
it('should return array containting empty object, if bidder present and no params are configured', function () {
const output = utils.getUserConfiguredParams(adUnits, 'adUnit1', 'bidder2');
const expected = [{}];
assert.deepEqual(output, expected);
});
it('should return empty array, if bidder is not present', function () {
const output = utils.getUserConfiguredParams(adUnits, 'adUnit1', 'bidder3');
const expected = [];
assert.deepEqual(output, expected);
});
it('should return empty array, if adUnit is not present', function () {
const output = utils.getUserConfiguredParams(adUnits, 'adUnit2', 'bidder3');
const expected = [];
assert.deepEqual(output, expected);
});
});
describe('convertCamelToUnderscore', function () {
it('returns converted string value using underscore syntax instead of camelCase', function () {
let var1 = 'placementIdTest';
let test1 = convertCamelToUnderscore(var1);
expect(test1).to.equal('placement_id_test');
let var2 = 'my_test_value';
let test2 = convertCamelToUnderscore(var2);
expect(test2).to.equal(var2);
});
});
describe('URL helpers', function () {
describe('parseUrl()', function () {
let parsed;
beforeEach(function () {
parsed = utils.parseUrl('http://example.com:3000/pathname/?search=test&foo=bar&bar=foo%26foo%3Dxxx#hash');
});
it('extracts the protocol', function () {
expect(parsed).to.have.property('protocol', 'http');
});
it('extracts the hostname', function () {
expect(parsed).to.have.property('hostname', 'example.com');
});
it('extracts the port', function () {
expect(parsed).to.have.property('port', 3000);
});
it('extracts the pathname', function () {
expect(parsed).to.have.property('pathname', '/pathname/');
});
it('extracts the search query', function () {
expect(parsed).to.have.property('search');
expect(parsed.search).to.eql({
foo: 'xxx',
search: 'test',
bar: 'foo',
});
});
it('extracts the hash', function () {
expect(parsed).to.have.property('hash', 'hash');
});
it('extracts the host', function () {
expect(parsed).to.have.property('host', 'example.com:3000');
});
});
describe('parseUrl(url, {noDecodeWholeURL: true})', function () {
let parsed;
beforeEach(function () {
parsed = utils.parseUrl('http://example.com:3000/pathname/?search=test&foo=bar&bar=foo%26foo%3Dxxx#hash', {noDecodeWholeURL: true});
});
it('extracts the search query', function () {
expect(parsed).to.have.property('search');
expect(parsed.search).to.eql({
foo: 'bar',
search: 'test',
bar: 'foo%26foo%3Dxxx',
});
});
});
describe('buildUrl()', function () {
it('formats an object in to a URL', function () {
expect(utils.buildUrl({
protocol: 'http',
hostname: 'example.com',
port: 3000,
pathname: '/pathname/',
search: {foo: 'bar', search: 'test', bar: 'foo%26foo%3Dxxx'},
hash: 'hash'
})).to.equal('http://example.com:3000/pathname/?foo=bar&search=test&bar=foo%26foo%3Dxxx#hash');
});
it('will use defaults for missing properties', function () {
expect(utils.buildUrl({
hostname: 'example.com'
})).to.equal('http://example.com');
});
});
describe('parseUrl(url, {decodeSearchAsString: true})', function () {
let parsed;
beforeEach(function () {
parsed = utils.parseUrl('http://example.com:3000/pathname/?search=test&foo=bar&bar=foo%26foo%3Dxxx#hash', {decodeSearchAsString: true});
});
it('extracts the search query', function () {
expect(parsed).to.have.property('search');
expect(parsed.search).to.equal('?search=test&foo=bar&bar=foo&foo=xxx');
});
});
describe('encodeMacroURI', () => {
[
['https://www.example.com', 'https://www.example.com'],
['https://www.example/${MACRO}', 'https://www.example/${MACRO}'],
['http://www.example/è', `http://www.example/${encodeURIComponent('è')}`],
['https://www.${MACRO_1}/${MACRO_1}/${MACRO_2}è', 'https://www.${MACRO_1}/${MACRO_1}/${MACRO_2}' + encodeURIComponent('è')],
['http://${MACRO}${MACRO}/${MACRO}', 'http://${MACRO}${MACRO}/${MACRO}'],
['{MACRO}${MACRO}', `${encodeURIComponent('{MACRO}')}\${MACRO}`],
['https://www.example.com?p=${AUCTION_PRICE}', 'https://www.example.com?p=${AUCTION_PRICE}']
].forEach(([input, expected]) => {
it(`can encode ${input} -> ${expected}`, () => {
expect(encodeMacroURI(input)).to.eql(expected);
})
})
})
});
describe('insertElement', function () {
it('returns a node at the top of the target by default', function () {
const toInsert = document.createElement('div');
const target = document.getElementsByTagName('body')[0];
const inserted = utils.insertElement(toInsert, document, 'body');
expect(inserted).to.equal(target.firstChild);
});
it('returns a node at bottom of target if 4th argument is true', function () {
const toInsert = document.createElement('div');
const target = document.getElementsByTagName('html')[0];
const inserted = utils.insertElement(toInsert, document, 'html', true);
expect(inserted).to.equal(target.lastChild);
});
it('returns a node at top of the head if no target is given', function () {
const toInsert = document.createElement('div');
const target = document.getElementsByTagName('head')[0];
const inserted = utils.insertElement(toInsert);
expect(inserted).to.equal(target.firstChild);
});
});
describe('isSafariBrowser', function () {
let userAgentStub;
let userAgent;
before(function () {
userAgentStub = sinon.stub(navigator, 'userAgent').get(function () {
return userAgent;
});
});
after(function () {
userAgentStub.restore();
});
it('properly detects safari', function () {
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/536.25 (KHTML, like Gecko) Version/6.0 Safari/536.25';
expect(utils.isSafariBrowser()).to.equal(true);
});
it('does not flag Chrome on MacOS', function () {
userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36';
expect(utils.isSafariBrowser()).to.equal(false);
});
it('does not flag Chrome iOS', function () {
userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/80.0.3987.95 Mobile/15E148 Safari/604.1';
expect(utils.isSafariBrowser()).to.equal(false);
});
it('does not flag Firefox iOS', function () {
userAgent = 'Mozilla/5.0 (iPhone; CPU OS 13_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/23.0 Mobile/15E148 Safari/605.1.15';
expect(utils.isSafariBrowser()).to.equal(false);
});
it('does not flag Windows Edge', function () {
userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.74 Safari/537.36 Edg/79.0.309.43';
expect(utils.isSafariBrowser()).to.equal(false);
});
});
describe('mergeDeep', function() {
it('properly merge objects that share same property names', function() {
const object1 = {
propA: {
subPropA: 'abc'
}
};
const object2 = {
propA: {
subPropB: 'def'
}
};
const resultWithoutMergeDeep = Object.assign({}, object1, object2);
expect(resultWithoutMergeDeep).to.deep.equal({
propA: {
subPropB: 'def'
}
});
const resultWithMergeDeep = utils.mergeDeep({}, object1, object2);
expect(resultWithMergeDeep).to.deep.equal({
propA: {
subPropA: 'abc',
subPropB: 'def'
}
});
});
it('properly merge objects that have different depths', function() {
const object1 = {
depth0_A: {
depth1_A: {
depth2_A: 123
}
}
};
const object2 = {
depth0_A: {
depth1_A: {
depth2_B: {
depth3_A: {
depth4_A: 'def'
}
}
},
depth1_B: 'abc'
}
};
const object3 = {
depth0_B: 456
};
const result = utils.mergeDeep({}, object1, object2, object3);
expect(result).to.deep.equal({
depth0_A: {
depth1_A: {
depth2_A: 123,
depth2_B: {
depth3_A: {
depth4_A: 'def'