From 8ef29b65ffff5b82a0d605eb3591f69a43525cfa Mon Sep 17 00:00:00 2001 From: JiaLiPassion Date: Wed, 26 Feb 2020 15:14:11 +0900 Subject: [PATCH] fix: fix flaky test cases of passive events (#35679) PR Close #35679 --- packages/zone.js/test/browser/browser.spec.ts | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/packages/zone.js/test/browser/browser.spec.ts b/packages/zone.js/test/browser/browser.spec.ts index 7641071ecc..6be5a9d006 100644 --- a/packages/zone.js/test/browser/browser.spec.ts +++ b/packages/zone.js/test/browser/browser.spec.ts @@ -215,14 +215,19 @@ describe('Zone', function() { scrollEvent.initEvent('scroll', true, true); const zone = Zone.current.fork({name: 'run'}); + const div = document.createElement('div'); + document.body.appendChild(div); Zone.current.fork({name: 'scroll'}).run(() => { - document.addEventListener( - 'scroll', () => { expect(Zone.current.name).toEqual(zone.name); }); + const listener = () => { + expect(Zone.current.name).toEqual(zone.name); + div.removeEventListener('scroll', listener); + }; + div.addEventListener('scroll', listener); }); - zone.run(() => { document.dispatchEvent(scrollEvent); }); - (document as any).removeAllListeners('scroll'); + zone.run(() => { div.dispatchEvent(scrollEvent); }); + document.body.removeChild(div); }); it('should be able to clear on handler added before load zone.js', function() { @@ -1047,12 +1052,16 @@ describe('Zone', function() { const testPassive = function(eventName: string, expectedPassiveLog: string, options: any) { (button as any).addEventListener(eventName, listener, options); const evt = document.createEvent('Event'); - evt.initEvent(eventName, true, true); + evt.initEvent(eventName, false, true); button.dispatchEvent(evt); expect(logs).toEqual(['default will run', expectedPassiveLog]); (button as any).removeAllListeners(eventName); }; - beforeEach(() => { logs = []; }); + beforeEach(() => { + logs = []; + (button as any).removeAllListeners(); + }); + afterEach(() => { (button as any).removeAllListeners(); }); it('should be passive with global variable defined', () => { testPassive('touchstart', 'default will run', {passive: true}); }); it('should not be passive without global variable defined', @@ -1065,10 +1074,8 @@ describe('Zone', function() { () => { testPassive('touchstart', 'default will run', true); }); it('should not be passive with global variable defined with passive false option', () => { testPassive('touchstart', 'defaultPrevented', {passive: false}); }); - it('should be passive with global variable defined and also blacklisted', () => { - (document as any).removeAllListeners('scroll'); - testPassive('scroll', 'default will run', undefined); - }); + it('should be passive with global variable defined and also blacklisted', + () => { testPassive('scroll', 'default will run', undefined); }); it('should not be passive without global variable defined and also blacklisted', () => { testPassive('wheel', 'defaultPrevented', undefined); }); });