(Zone: ZoneType)
| 9 | import {ZoneType} from '../zone-impl'; |
| 10 | |
| 11 | export function patchPromiseTesting(Zone: ZoneType): void { |
| 12 | /** |
| 13 | * Promise for async/fakeAsync zoneSpec test |
| 14 | * can support async operation which not supported by zone.js |
| 15 | * such as |
| 16 | * it ('test jsonp in AsyncZone', async() => { |
| 17 | * new Promise(res => { |
| 18 | * jsonp(url, (data) => { |
| 19 | * // success callback |
| 20 | * res(data); |
| 21 | * }); |
| 22 | * }).then((jsonpResult) => { |
| 23 | * // get jsonp result. |
| 24 | * |
| 25 | * // user will expect AsyncZoneSpec wait for |
| 26 | * // then, but because jsonp is not zone aware |
| 27 | * // AsyncZone will finish before then is called. |
| 28 | * }); |
| 29 | * }); |
| 30 | */ |
| 31 | Zone.__load_patch('promisefortest', (global: any, Zone: ZoneType, api: _ZonePrivate) => { |
| 32 | const symbolState: string = api.symbol('state'); |
| 33 | const UNRESOLVED: null = null; |
| 34 | const symbolParentUnresolved = api.symbol('parentUnresolved'); |
| 35 | |
| 36 | // patch Promise.prototype.then to keep an internal |
| 37 | // number for tracking unresolved chained promise |
| 38 | // we will decrease this number when the parent promise |
| 39 | // being resolved/rejected and chained promise was |
| 40 | // scheduled as a microTask. |
| 41 | // so we can know such kind of chained promise still |
| 42 | // not resolved in AsyncTestZone |
| 43 | (Promise as any)[api.symbol('patchPromiseForTest')] = function patchPromiseForTest() { |
| 44 | let oriThen = (Promise as any)[Zone.__symbol__('ZonePromiseThen')]; |
| 45 | if (oriThen) { |
| 46 | return; |
| 47 | } |
| 48 | oriThen = (Promise as any)[Zone.__symbol__('ZonePromiseThen')] = Promise.prototype.then; |
| 49 | Promise.prototype.then = function () { |
| 50 | const chained = oriThen.apply(this, arguments); |
| 51 | if ((this as any)[symbolState] === UNRESOLVED) { |
| 52 | // parent promise is unresolved. |
| 53 | const asyncTestZoneSpec = Zone.current.get('AsyncTestZoneSpec'); |
| 54 | if (asyncTestZoneSpec) { |
| 55 | asyncTestZoneSpec.unresolvedChainedPromiseCount++; |
| 56 | chained[symbolParentUnresolved] = true; |
| 57 | } |
| 58 | } |
| 59 | return chained; |
| 60 | }; |
| 61 | }; |
| 62 | |
| 63 | (Promise as any)[api.symbol('unPatchPromiseForTest')] = function unpatchPromiseForTest() { |
| 64 | // restore origin then |
| 65 | const oriThen = (Promise as any)[Zone.__symbol__('ZonePromiseThen')]; |
| 66 | if (oriThen) { |
| 67 | Promise.prototype.then = oriThen; |
| 68 | (Promise as any)[Zone.__symbol__('ZonePromiseThen')] = undefined; |
no test coverage detected
searching dependent graphs…