This repository was archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 405
Expand file tree
/
Copy pathbrowser.spec.ts
More file actions
2667 lines (2287 loc) · 99.6 KB
/
browser.spec.ts
File metadata and controls
2667 lines (2287 loc) · 99.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
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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {patchFilteredProperties} from '../../lib/browser/property-descriptor';
import {patchEventTarget} from '../../lib/common/events';
import {isIEOrEdge, zoneSymbol} from '../../lib/common/utils';
import {getEdgeVersion, getIEVersion, ifEnvSupports, ifEnvSupportsWithDone, isEdge} from '../test-util';
import Spy = jasmine.Spy;
declare const global: any;
const noop = function() {};
function windowPrototype() {
return !!(global['Window'] && global['Window'].prototype);
}
function promiseUnhandleRejectionSupport() {
return !!global['PromiseRejectionEvent'];
}
function canPatchOnProperty(obj: any, prop: string) {
const func = function() {
if (!obj) {
return false;
}
const desc = Object.getOwnPropertyDescriptor(obj, prop);
if (!desc || !desc.configurable) {
return false;
}
return true;
};
(func as any).message = 'patchOnProperties';
return func;
}
let supportsPassive = false;
try {
const opts = Object.defineProperty({}, 'passive', {
get: function() {
supportsPassive = true;
}
});
window.addEventListener('test', opts as any, opts);
window.removeEventListener('test', opts as any, opts);
} catch (e) {
}
function supportEventListenerOptions() {
return supportsPassive;
}
(supportEventListenerOptions as any).message = 'supportsEventListenerOptions';
function supportCanvasTest() {
const HTMLCanvasElement = (window as any)['HTMLCanvasElement'];
const supportCanvas = typeof HTMLCanvasElement !== 'undefined' && HTMLCanvasElement.prototype &&
HTMLCanvasElement.prototype.toBlob;
const FileReader = (window as any)['FileReader'];
const supportFileReader = typeof FileReader !== 'undefined';
return supportCanvas && supportFileReader;
}
(supportCanvasTest as any).message = 'supportCanvasTest';
function ieOrEdge() {
return isIEOrEdge();
}
(ieOrEdge as any).message = 'IE/Edge Test';
class TestEventListener {
logs: string[] = [];
addEventListener(eventName: string, listener: any, options: any) {
this.logs.push(options);
}
removeEventListener(eventName: string, listener: any, options: any) {}
}
describe('Zone', function() {
const rootZone = Zone.current;
(Zone as any)[zoneSymbol('ignoreConsoleErrorUncaughtError')] = true;
describe('hooks', function() {
it('should allow you to override alert/prompt/confirm', function() {
const alertSpy = jasmine.createSpy('alert');
const promptSpy = jasmine.createSpy('prompt');
const confirmSpy = jasmine.createSpy('confirm');
const spies:
{[k: string]: Function} = {'alert': alertSpy, 'prompt': promptSpy, 'confirm': confirmSpy};
const myZone = Zone.current.fork({
name: 'spy',
onInvoke: (
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
callback: Function, applyThis?: any, applyArgs?: any[], source?: string): any => {
if (source) {
spies[source].apply(null, applyArgs);
} else {
return parentZoneDelegate.invoke(targetZone, callback, applyThis, applyArgs, source);
}
}
});
myZone.run(function() {
alert('alertMsg');
prompt('promptMsg', 'default');
confirm('confirmMsg');
});
expect(alertSpy).toHaveBeenCalledWith('alertMsg');
expect(promptSpy).toHaveBeenCalledWith('promptMsg', 'default');
expect(confirmSpy).toHaveBeenCalledWith('confirmMsg');
});
describe(
'DOM onProperty hooks',
ifEnvSupports(canPatchOnProperty(HTMLElement.prototype, 'onclick'), function() {
let mouseEvent = document.createEvent('Event');
let hookSpy: Spy, eventListenerSpy: Spy;
const zone = rootZone.fork({
name: 'spy',
onScheduleTask:
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task):
any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
}
});
beforeEach(function() {
mouseEvent.initEvent('mousedown', true, true);
hookSpy = jasmine.createSpy('hook');
eventListenerSpy = jasmine.createSpy('eventListener');
});
function checkIsOnPropertiesPatched(target: any, ignoredProperties?: string[]) {
for (let prop in target) {
if (ignoredProperties &&
ignoredProperties.filter(ignoreProp => ignoreProp === prop).length > 0) {
continue;
}
if (prop.substr(0, 2) === 'on' && prop.length > 2) {
target[prop] = noop;
if (!target[Zone.__symbol__('ON_PROPERTY' + prop.substr(2))]) {
console.log('onProp is null:', prop);
} else {
target[prop] = null;
expect(!target[Zone.__symbol__('ON_PROPERTY' + prop.substr(2))]).toBeTruthy();
}
}
}
}
it('should patch all possbile on properties on element', function() {
const htmlElementTagNames: string[] = [
'a', 'area', 'audio', 'base', 'basefont', 'blockquote', 'br',
'button', 'canvas', 'caption', 'col', 'colgroup', 'data', 'datalist',
'del', 'dir', 'div', 'dl', 'embed', 'fieldset', 'font',
'form', 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4',
'h5', 'h6', 'head', 'hr', 'html', 'iframe', 'img',
'input', 'ins', 'isindex', 'label', 'legend', 'li', 'link',
'listing', 'map', 'marquee', 'menu', 'meta', 'meter', 'nextid',
'ol', 'optgroup', 'option', 'output', 'p', 'param', 'picture',
'pre', 'progress', 'q', 'script', 'select', 'source', 'span',
'style', 'table', 'tbody', 'td', 'template', 'textarea', 'tfoot',
'th', 'thead', 'time', 'title', 'tr', 'track', 'ul',
'video'
];
htmlElementTagNames.forEach(tagName => {
checkIsOnPropertiesPatched(document.createElement(tagName), ['onorientationchange']);
});
});
it('should patch all possbile on properties on body', function() {
checkIsOnPropertiesPatched(document.body, ['onorientationchange']);
});
it('should patch all possbile on properties on Document', function() {
checkIsOnPropertiesPatched(document, ['onorientationchange']);
});
it('should patch all possbile on properties on Window', function() {
checkIsOnPropertiesPatched(window, [
'onvrdisplayactivate', 'onvrdisplayblur', 'onvrdisplayconnect',
'onvrdisplaydeactivate', 'onvrdisplaydisconnect', 'onvrdisplayfocus',
'onvrdisplaypointerrestricted', 'onvrdisplaypointerunrestricted',
'onorientationchange', 'onerror'
]);
});
it('should patch all possbile on properties on xhr', function() {
checkIsOnPropertiesPatched(new XMLHttpRequest());
});
it('should not patch ignored on properties', function() {
const TestTarget: any = (window as any)['TestTarget'];
patchFilteredProperties(
TestTarget.prototype, ['prop1', 'prop2'], global['__Zone_ignore_on_properties']);
const testTarget = new TestTarget();
Zone.current.fork({name: 'test'}).run(() => {
testTarget.onprop1 = function() {
// onprop1 should not be patched
expect(Zone.current.name).toEqual('test1');
};
testTarget.onprop2 = function() {
// onprop2 should be patched
expect(Zone.current.name).toEqual('test');
};
});
Zone.current.fork({name: 'test1'}).run(() => {
testTarget.dispatchEvent('prop1');
testTarget.dispatchEvent('prop2');
});
});
it('should not patch ignored eventListener', function() {
let scrollEvent = document.createEvent('Event');
scrollEvent.initEvent('scroll', true, true);
const zone = Zone.current.fork({name: 'run'});
Zone.current.fork({name: 'scroll'}).run(() => {
document.addEventListener('scroll', () => {
expect(Zone.current.name).toEqual(zone.name);
});
});
zone.run(() => {
document.dispatchEvent(scrollEvent);
});
});
it('should be able to clear on handler added before load zone.js', function() {
const TestTarget: any = (window as any)['TestTarget'];
patchFilteredProperties(
TestTarget.prototype, ['prop3'], global['__Zone_ignore_on_properties']);
const testTarget = new TestTarget();
Zone.current.fork({name: 'test'}).run(() => {
expect(testTarget.onprop3).toBeTruthy();
const newProp3Handler = function() {};
testTarget.onprop3 = newProp3Handler;
expect(testTarget.onprop3).toBe(newProp3Handler);
testTarget.onprop3 = null;
expect(!testTarget.onprop3).toBeTruthy();
testTarget.onprop3 = function() {
// onprop1 should not be patched
expect(Zone.current.name).toEqual('test');
};
});
Zone.current.fork({name: 'test1'}).run(() => {
testTarget.dispatchEvent('prop3');
});
});
it('window onclick should be in zone',
ifEnvSupports(canPatchOnProperty(window, 'onmousedown'), function() {
zone.run(function() {
window.onmousedown = eventListenerSpy;
});
window.dispatchEvent(mouseEvent);
expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).toHaveBeenCalled();
window.removeEventListener('mousedown', eventListenerSpy);
}));
it('window onresize should be patched',
ifEnvSupports(canPatchOnProperty(window, 'onmousedown'), function() {
window.onresize = eventListenerSpy;
const innerResizeProp: any = (window as any)[zoneSymbol('ON_PROPERTYresize')];
expect(innerResizeProp).toBeTruthy();
innerResizeProp();
expect(eventListenerSpy).toHaveBeenCalled();
window.removeEventListener('resize', eventListenerSpy);
}));
it('document onclick should be in zone',
ifEnvSupports(canPatchOnProperty(Document.prototype, 'onmousedown'), function() {
zone.run(function() {
document.onmousedown = eventListenerSpy;
});
document.dispatchEvent(mouseEvent);
expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).toHaveBeenCalled();
document.removeEventListener('mousedown', eventListenerSpy);
}));
it('event handler with null context should use event.target',
ifEnvSupports(canPatchOnProperty(Document.prototype, 'onmousedown'), function() {
const ieVer = getIEVersion();
if (ieVer && ieVer === 9) {
// in ie9, this is window object even we call func.apply(undefined)
return;
}
const logs: string[] = [];
const EventTarget = (window as any)['EventTarget'];
let oriAddEventListener = EventTarget && EventTarget.prototype ?
(EventTarget.prototype as any)[zoneSymbol('addEventListener')] :
(HTMLSpanElement.prototype as any)[zoneSymbol('addEventListener')];
if (!oriAddEventListener) {
// no patched addEventListener found
return;
}
let handler1: Function;
let handler2: Function;
const listener = function() {
logs.push('listener1');
};
const listener1 = function() {
logs.push('listener2');
};
HTMLSpanElement.prototype.addEventListener = function(
eventName: string, callback: any) {
if (eventName === 'click') {
handler1 = callback;
} else if (eventName === 'mousedown') {
handler2 = callback;
}
return oriAddEventListener.apply(this, arguments);
};
(HTMLSpanElement.prototype as any)[zoneSymbol('addEventListener')] = null;
patchEventTarget(window, [HTMLSpanElement.prototype]);
const span = document.createElement('span');
document.body.appendChild(span);
zone.run(function() {
span.addEventListener('click', listener);
span.onmousedown = listener1;
});
expect(handler1!).toBe(handler2!);
handler1!.apply(undefined, [{type: 'click', target: span}]);
handler2!.apply(undefined, [{type: 'mousedown', target: span}]);
expect(hookSpy).toHaveBeenCalled();
expect(logs).toEqual(['listener1', 'listener2']);
document.body.removeChild(span);
if (EventTarget) {
(EventTarget.prototype as any)[zoneSymbol('addEventListener')] =
oriAddEventListener;
} else {
(HTMLSpanElement.prototype as any)[zoneSymbol('addEventListener')] =
oriAddEventListener;
}
}));
it('SVGElement onclick should be in zone',
ifEnvSupports(
canPatchOnProperty(SVGElement && SVGElement.prototype, 'onmousedown'), function() {
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
document.body.appendChild(svg);
zone.run(function() {
svg.onmousedown = eventListenerSpy;
});
svg.dispatchEvent(mouseEvent);
expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).toHaveBeenCalled();
svg.removeEventListener('mouse', eventListenerSpy);
document.body.removeChild(svg);
}));
it('get window onerror should not throw error',
ifEnvSupports(canPatchOnProperty(window, 'onerror'), function() {
const testFn = function() {
let onerror = window.onerror;
window.onerror = function() {};
onerror = window.onerror;
};
expect(testFn).not.toThrow();
}));
it('window.onerror callback signiture should be (message, source, lineno, colno, error)',
ifEnvSupportsWithDone(canPatchOnProperty(window, 'onerror'), function(done: DoneFn) {
let testError = new Error('testError');
window.onerror = function(
message: any, source?: string, lineno?: number, colno?: number, error?: any) {
expect(message).toContain('testError');
if (getEdgeVersion() !== 14) {
// Edge 14, error will be undefined.
expect(error).toBe(testError);
}
(window as any).onerror = null;
setTimeout(done);
return true;
};
setTimeout(() => {
throw testError;
}, 100);
}));
}));
describe('eventListener hooks', function() {
let button: HTMLButtonElement;
let clickEvent: Event;
beforeEach(function() {
button = document.createElement('button');
clickEvent = document.createEvent('Event');
clickEvent.initEvent('click', true, true);
document.body.appendChild(button);
});
afterEach(function() {
document.body.removeChild(button);
});
it('should support addEventListener', function() {
const hookSpy = jasmine.createSpy('hook');
const eventListenerSpy = jasmine.createSpy('eventListener');
const zone = rootZone.fork({
name: 'spy',
onScheduleTask:
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task):
any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
}
});
zone.run(function() {
button.addEventListener('click', eventListenerSpy);
});
button.dispatchEvent(clickEvent);
expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).toHaveBeenCalled();
});
it('should be able to access addEventListener information in onScheduleTask', function() {
const hookSpy = jasmine.createSpy('hook');
const eventListenerSpy = jasmine.createSpy('eventListener');
let scheduleButton;
let scheduleEventName;
let scheduleCapture;
let scheduleTask;
const zone = rootZone.fork({
name: 'spy',
onScheduleTask:
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task):
any => {
hookSpy();
scheduleButton = (task.data as any).taskData.target;
scheduleEventName = (task.data as any).taskData.eventName;
scheduleCapture = (task.data as any).taskData.capture;
scheduleTask = task;
return parentZoneDelegate.scheduleTask(targetZone, task);
}
});
zone.run(function() {
button.addEventListener('click', eventListenerSpy);
});
button.dispatchEvent(clickEvent);
expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).toHaveBeenCalled();
expect(scheduleButton).toBe(button);
expect(scheduleEventName).toBe('click');
expect(scheduleCapture).toBe(false);
expect(scheduleTask && (scheduleTask as any).data.taskData).toBe(null);
});
it('should support addEventListener on window', ifEnvSupports(windowPrototype, function() {
const hookSpy = jasmine.createSpy('hook');
const eventListenerSpy = jasmine.createSpy('eventListener');
const zone = rootZone.fork({
name: 'spy',
onScheduleTask: (
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task):
any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
}
});
zone.run(function() {
window.addEventListener('click', eventListenerSpy);
});
window.dispatchEvent(clickEvent);
expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).toHaveBeenCalled();
}));
it('should support removeEventListener', function() {
const hookSpy = jasmine.createSpy('hook');
const eventListenerSpy = jasmine.createSpy('eventListener');
const zone = rootZone.fork({
name: 'spy',
onCancelTask:
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task):
any => {
hookSpy();
return parentZoneDelegate.cancelTask(targetZone, task);
}
});
zone.run(function() {
button.addEventListener('click', eventListenerSpy);
button.removeEventListener('click', eventListenerSpy);
});
button.dispatchEvent(clickEvent);
expect(hookSpy).toHaveBeenCalled();
expect(eventListenerSpy).not.toHaveBeenCalled();
});
describe(
'should support addEventListener/removeEventListener with AddEventListenerOptions with capture setting',
ifEnvSupports(supportEventListenerOptions, function() {
let hookSpy: Spy;
let cancelSpy: Spy;
let logs: string[];
const zone = rootZone.fork({
name: 'spy',
onScheduleTask:
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
task: Task): any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
},
onCancelTask:
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
task: Task): any => {
cancelSpy();
return parentZoneDelegate.cancelTask(targetZone, task);
}
});
const docListener = () => {
logs.push('document');
};
const btnListener = () => {
logs.push('button');
};
beforeEach(() => {
logs = [];
hookSpy = jasmine.createSpy('hook');
cancelSpy = jasmine.createSpy('cancel');
});
it('should handle child event when addEventListener with capture true', () => {
// test capture true
zone.run(function() {
(document as any).addEventListener('click', docListener, {capture: true});
button.addEventListener('click', btnListener);
});
button.dispatchEvent(clickEvent);
expect(hookSpy).toHaveBeenCalled();
expect(logs).toEqual(['document', 'button']);
logs = [];
(document as any).removeEventListener('click', docListener, {capture: true});
button.removeEventListener('click', btnListener);
expect(cancelSpy).toHaveBeenCalled();
button.dispatchEvent(clickEvent);
expect(logs).toEqual([]);
});
it('should handle child event when addEventListener with capture true', () => {
// test capture false
zone.run(function() {
(document as any).addEventListener('click', docListener, {capture: false});
button.addEventListener('click', btnListener);
});
button.dispatchEvent(clickEvent);
expect(hookSpy).toHaveBeenCalled();
expect(logs).toEqual(['button', 'document']);
logs = [];
(document as any).removeEventListener('click', docListener, {capture: false});
button.removeEventListener('click', btnListener);
expect(cancelSpy).toHaveBeenCalled();
button.dispatchEvent(clickEvent);
expect(logs).toEqual([]);
});
}));
describe(
'should ignore duplicate event handler',
ifEnvSupports(supportEventListenerOptions, function() {
let hookSpy: Spy;
let cancelSpy: Spy;
let logs: string[];
const zone = rootZone.fork({
name: 'spy',
onScheduleTask:
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
task: Task): any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
},
onCancelTask:
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
task: Task): any => {
cancelSpy();
return parentZoneDelegate.cancelTask(targetZone, task);
}
});
const docListener = () => {
logs.push('document options');
};
beforeEach(() => {
logs = [];
hookSpy = jasmine.createSpy('hook');
cancelSpy = jasmine.createSpy('cancel');
});
const testDuplicate = function(args1?: any, args2?: any) {
zone.run(function() {
if (args1) {
(document as any).addEventListener('click', docListener, args1);
} else {
(document as any).addEventListener('click', docListener);
}
if (args2) {
(document as any).addEventListener('click', docListener, args2);
} else {
(document as any).addEventListener('click', docListener);
}
});
button.dispatchEvent(clickEvent);
expect(hookSpy).toHaveBeenCalled();
expect(logs).toEqual(['document options']);
logs = [];
(document as any).removeEventListener('click', docListener, args1);
expect(cancelSpy).toHaveBeenCalled();
button.dispatchEvent(clickEvent);
expect(logs).toEqual([]);
};
it('should ignore duplicate handler', () => {
let captureFalse = [
undefined, false, {capture: false}, {capture: false, passive: false},
{passive: false}, {}
];
let captureTrue = [true, {capture: true}, {capture: true, passive: false}];
for (let i = 0; i < captureFalse.length; i++) {
for (let j = 0; j < captureFalse.length; j++) {
testDuplicate(captureFalse[i], captureFalse[j]);
}
}
for (let i = 0; i < captureTrue.length; i++) {
for (let j = 0; j < captureTrue.length; j++) {
testDuplicate(captureTrue[i], captureTrue[j]);
}
}
});
}));
describe(
'should support mix useCapture with AddEventListenerOptions capture',
ifEnvSupports(supportEventListenerOptions, function() {
let hookSpy: Spy;
let cancelSpy: Spy;
let logs: string[];
const zone = rootZone.fork({
name: 'spy',
onScheduleTask:
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
task: Task): any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
},
onCancelTask:
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
task: Task): any => {
cancelSpy();
return parentZoneDelegate.cancelTask(targetZone, task);
}
});
const docListener = () => {
logs.push('document options');
};
const docListener1 = () => {
logs.push('document useCapture');
};
const btnListener = () => {
logs.push('button');
};
beforeEach(() => {
logs = [];
hookSpy = jasmine.createSpy('hook');
cancelSpy = jasmine.createSpy('cancel');
});
const testAddRemove = function(args1?: any, args2?: any) {
zone.run(function() {
if (args1) {
(document as any).addEventListener('click', docListener, args1);
} else {
(document as any).addEventListener('click', docListener);
}
if (args2) {
(document as any).removeEventListener('click', docListener, args2);
} else {
(document as any).removeEventListener('click', docListener);
}
});
button.dispatchEvent(clickEvent);
expect(cancelSpy).toHaveBeenCalled();
expect(logs).toEqual([]);
};
it('should be able to add/remove same handler with mix options and capture',
function() {
let captureFalse = [
undefined, false, {capture: false}, {capture: false, passive: false},
{passive: false}, {}
];
let captureTrue = [true, {capture: true}, {capture: true, passive: false}];
for (let i = 0; i < captureFalse.length; i++) {
for (let j = 0; j < captureFalse.length; j++) {
testAddRemove(captureFalse[i], captureFalse[j]);
}
}
for (let i = 0; i < captureTrue.length; i++) {
for (let j = 0; j < captureTrue.length; j++) {
testAddRemove(captureTrue[i], captureTrue[j]);
}
}
});
const testDifferent = function(args1?: any, args2?: any) {
zone.run(function() {
if (args1) {
(document as any).addEventListener('click', docListener, args1);
} else {
(document as any).addEventListener('click', docListener);
}
if (args2) {
(document as any).addEventListener('click', docListener1, args2);
} else {
(document as any).addEventListener('click', docListener1);
}
});
button.dispatchEvent(clickEvent);
expect(hookSpy).toHaveBeenCalled();
expect(logs.sort()).toEqual(['document options', 'document useCapture']);
logs = [];
if (args1) {
(document as any).removeEventListener('click', docListener, args1);
} else {
(document as any).removeEventListener('click', docListener);
}
button.dispatchEvent(clickEvent);
expect(logs).toEqual(['document useCapture']);
logs = [];
if (args2) {
(document as any).removeEventListener('click', docListener1, args2);
} else {
(document as any).removeEventListener('click', docListener1);
}
button.dispatchEvent(clickEvent);
expect(logs).toEqual([]);
};
it('should be able to add different handlers for same event', function() {
let captureFalse = [
undefined, false, {capture: false}, {capture: false, passive: false},
{passive: false}, {}
];
let captureTrue = [true, {capture: true}, {capture: true, passive: false}];
for (let i = 0; i < captureFalse.length; i++) {
for (let j = 0; j < captureTrue.length; j++) {
testDifferent(captureFalse[i], captureTrue[j]);
}
}
for (let i = 0; i < captureTrue.length; i++) {
for (let j = 0; j < captureFalse.length; j++) {
testDifferent(captureTrue[i], captureFalse[j]);
}
}
});
it('should handle options.capture true with capture true correctly', function() {
zone.run(function() {
(document as any).addEventListener('click', docListener, {capture: true});
document.addEventListener('click', docListener1, true);
button.addEventListener('click', btnListener);
});
button.dispatchEvent(clickEvent);
expect(hookSpy).toHaveBeenCalled();
expect(logs).toEqual(['document options', 'document useCapture', 'button']);
logs = [];
(document as any).removeEventListener('click', docListener, {capture: true});
button.dispatchEvent(clickEvent);
expect(logs).toEqual(['document useCapture', 'button']);
logs = [];
document.removeEventListener('click', docListener1, true);
button.dispatchEvent(clickEvent);
expect(logs).toEqual(['button']);
logs = [];
button.removeEventListener('click', btnListener);
expect(cancelSpy).toHaveBeenCalled();
button.dispatchEvent(clickEvent);
expect(logs).toEqual([]);
});
}));
it('should support addEventListener with AddEventListenerOptions once setting',
ifEnvSupports(supportEventListenerOptions, function() {
let hookSpy = jasmine.createSpy('hook');
let logs: string[] = [];
const zone = rootZone.fork({
name: 'spy',
onScheduleTask: (
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task):
any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
}
});
zone.run(function() {
(button as any).addEventListener('click', function() {
logs.push('click');
}, {once: true});
});
button.dispatchEvent(clickEvent);
expect(hookSpy).toHaveBeenCalled();
expect(logs.length).toBe(1);
expect(logs).toEqual(['click']);
logs = [];
button.dispatchEvent(clickEvent);
expect(logs.length).toBe(0);
}));
it('should support addEventListener with AddEventListenerOptions once setting and capture',
ifEnvSupports(supportEventListenerOptions, function() {
let hookSpy = jasmine.createSpy('hook');
let logs: string[] = [];
const zone = rootZone.fork({
name: 'spy',
onScheduleTask: (
parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, task: Task):
any => {
hookSpy();
return parentZoneDelegate.scheduleTask(targetZone, task);
}
});
zone.run(function() {
(button as any).addEventListener('click', function() {
logs.push('click');
}, {once: true, capture: true});
});
button.dispatchEvent(clickEvent);
expect(hookSpy).toHaveBeenCalled();
expect(logs.length).toBe(1);
expect(logs).toEqual(['click']);
logs = [];
button.dispatchEvent(clickEvent);
expect(logs.length).toBe(0);
}));
it('should support add multipe listeners with AddEventListenerOptions once setting and same capture after normal listener',
ifEnvSupports(supportEventListenerOptions, function() {
let logs: string[] = [];
button.addEventListener('click', function() {
logs.push('click');
}, true);
(button as any).addEventListener('click', function() {
logs.push('once click');
}, {once: true, capture: true});
button.dispatchEvent(clickEvent);
expect(logs.length).toBe(2);
expect(logs).toEqual(['click', 'once click']);
logs = [];
button.dispatchEvent(clickEvent);
expect(logs.length).toBe(1);
expect(logs).toEqual(['click']);
}));
it('should support add multipe listeners with AddEventListenerOptions once setting and mixed capture after normal listener',
ifEnvSupports(supportEventListenerOptions, function() {
let logs: string[] = [];
button.addEventListener('click', function() {
logs.push('click');
});
(button as any).addEventListener('click', function() {
logs.push('once click');
}, {once: true, capture: true});
button.dispatchEvent(clickEvent);
expect(logs.length).toBe(2);
expect(logs).toEqual(['click', 'once click']);
logs = [];
button.dispatchEvent(clickEvent);
expect(logs.length).toBe(1);
expect(logs).toEqual(['click']);
}));
it('should support add multipe listeners with AddEventListenerOptions once setting before normal listener',
ifEnvSupports(supportEventListenerOptions, function() {
let logs: string[] = [];
(button as any).addEventListener('click', function() {
logs.push('once click');
}, {once: true});
button.addEventListener('click', function() {
logs.push('click');
});
button.dispatchEvent(clickEvent);
expect(logs.length).toBe(2);
expect(logs).toEqual(['once click', 'click']);
logs = [];
button.dispatchEvent(clickEvent);
expect(logs.length).toBe(1);
expect(logs).toEqual(['click']);
}));
it('should support add multipe listeners with AddEventListenerOptions once setting with same capture before normal listener',
ifEnvSupports(supportEventListenerOptions, function() {
let logs: string[] = [];
(button as any).addEventListener('click', function() {
logs.push('once click');
}, {once: true, capture: true});
button.addEventListener('click', function() {
logs.push('click');
}, true);
button.dispatchEvent(clickEvent);
expect(logs.length).toBe(2);
expect(logs).toEqual(['once click', 'click']);
logs = [];
button.dispatchEvent(clickEvent);
expect(logs.length).toBe(1);
expect(logs).toEqual(['click']);