|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
| 9 | +import {zoneSymbol} from '../../lib/common/utils'; |
9 | 10 | import {ifEnvSupports} from '../test-util'; |
10 | 11 |
|
11 | 12 | function windowPrototype() { |
12 | 13 | return !!(global['Window'] && global['Window'].prototype); |
13 | 14 | } |
14 | 15 |
|
| 16 | +function promiseUnhandleRejectionSupport() { |
| 17 | + return !!global['PromiseRejectionEvent']; |
| 18 | +} |
| 19 | + |
15 | 20 | describe('Zone', function() { |
16 | 21 | const rootZone = Zone.current; |
17 | 22 |
|
@@ -142,6 +147,52 @@ describe('Zone', function() { |
142 | 147 | expect(button.onclick).not.toBe(null); |
143 | 148 | }); |
144 | 149 | }); |
| 150 | + |
| 151 | + it('should support window.addEventListener(unhandledrejection)', function(done) { |
| 152 | + if (!promiseUnhandleRejectionSupport()) { |
| 153 | + done(); |
| 154 | + return; |
| 155 | + } |
| 156 | + Zone[zoneSymbol('ignoreConsoleErrorUncaughtError')] = true; |
| 157 | + rootZone.fork({name: 'promise'}).run(function() { |
| 158 | + const listener = (evt: any) => { |
| 159 | + expect(evt.type).toEqual('unhandledrejection'); |
| 160 | + expect(evt.promise.constructor.name).toEqual('Promise'); |
| 161 | + expect(evt.reason.message).toBe('promise error'); |
| 162 | + window.removeEventListener('unhandledrejection', listener); |
| 163 | + done(); |
| 164 | + }; |
| 165 | + window.addEventListener('unhandledrejection', listener); |
| 166 | + new Promise((resolve, reject) => { |
| 167 | + throw new Error('promise error'); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should support window.addEventListener(rejectionhandled)', function(done) { |
| 173 | + if (!promiseUnhandleRejectionSupport()) { |
| 174 | + done(); |
| 175 | + return; |
| 176 | + } |
| 177 | + Zone[zoneSymbol('ignoreConsoleErrorUncaughtError')] = true; |
| 178 | + rootZone.fork({name: 'promise'}).run(function() { |
| 179 | + const listener = (evt: any) => { |
| 180 | + window.removeEventListener('unhandledrejection', listener); |
| 181 | + p.catch(reason => {}); |
| 182 | + }; |
| 183 | + window.addEventListener('unhandledrejection', listener); |
| 184 | + |
| 185 | + window.addEventListener('rejectionhandled', (evt: any) => { |
| 186 | + expect(evt.type).toEqual('rejectionhandled'); |
| 187 | + expect(evt.promise.constructor.name).toEqual('Promise'); |
| 188 | + expect(evt.reason.message).toBe('promise error'); |
| 189 | + done(); |
| 190 | + }); |
| 191 | + const p = new Promise((resolve, reject) => { |
| 192 | + throw new Error('promise error'); |
| 193 | + }); |
| 194 | + }); |
| 195 | + }); |
145 | 196 | }); |
146 | 197 | }); |
147 | 198 | }); |
0 commit comments