@ -12,11 +12,17 @@ describe('AsyncTestZoneSpec', function() {
|
||||
let log: string[];
|
||||
const AsyncTestZoneSpec = (Zone as any)['AsyncTestZoneSpec'];
|
||||
|
||||
function finishCallback() { log.push('finish'); }
|
||||
function finishCallback() {
|
||||
log.push('finish');
|
||||
}
|
||||
|
||||
function failCallback() { log.push('fail'); }
|
||||
function failCallback() {
|
||||
log.push('fail');
|
||||
}
|
||||
|
||||
beforeEach(() => { log = []; });
|
||||
beforeEach(() => {
|
||||
log = [];
|
||||
});
|
||||
|
||||
it('should call finish after zone is run in sync call', (done) => {
|
||||
let finished = false;
|
||||
@ -27,7 +33,9 @@ describe('AsyncTestZoneSpec', function() {
|
||||
|
||||
const atz = Zone.current.fork(testZoneSpec);
|
||||
|
||||
atz.run(function() { finished = true; });
|
||||
atz.run(function() {
|
||||
finished = true;
|
||||
});
|
||||
});
|
||||
|
||||
it('should call finish after a setTimeout is done', (done) => {
|
||||
@ -38,11 +46,18 @@ describe('AsyncTestZoneSpec', function() {
|
||||
expect(finished).toBe(true);
|
||||
done();
|
||||
},
|
||||
() => { done.fail('async zone called failCallback unexpectedly'); }, 'name');
|
||||
() => {
|
||||
done.fail('async zone called failCallback unexpectedly');
|
||||
},
|
||||
'name');
|
||||
|
||||
const atz = Zone.current.fork(testZoneSpec);
|
||||
|
||||
atz.run(function() { setTimeout(() => { finished = true; }, 10); });
|
||||
atz.run(function() {
|
||||
setTimeout(() => {
|
||||
finished = true;
|
||||
}, 10);
|
||||
});
|
||||
});
|
||||
|
||||
it('should call finish after microtasks are done', (done) => {
|
||||
@ -53,11 +68,18 @@ describe('AsyncTestZoneSpec', function() {
|
||||
expect(finished).toBe(true);
|
||||
done();
|
||||
},
|
||||
() => { done.fail('async zone called failCallback unexpectedly'); }, 'name');
|
||||
() => {
|
||||
done.fail('async zone called failCallback unexpectedly');
|
||||
},
|
||||
'name');
|
||||
|
||||
const atz = Zone.current.fork(testZoneSpec);
|
||||
|
||||
atz.run(function() { Promise.resolve().then(() => { finished = true; }); });
|
||||
atz.run(function() {
|
||||
Promise.resolve().then(() => {
|
||||
finished = true;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should call finish after both micro and macrotasks are done', (done) => {
|
||||
@ -68,12 +90,19 @@ describe('AsyncTestZoneSpec', function() {
|
||||
expect(finished).toBe(true);
|
||||
done();
|
||||
},
|
||||
() => { done.fail('async zone called failCallback unexpectedly'); }, 'name');
|
||||
() => {
|
||||
done.fail('async zone called failCallback unexpectedly');
|
||||
},
|
||||
'name');
|
||||
|
||||
const atz = Zone.current.fork(testZoneSpec);
|
||||
|
||||
atz.run(function() {
|
||||
new Promise((resolve) => { setTimeout(() => { resolve(); }, 10); }).then(() => {
|
||||
new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, 10);
|
||||
}).then(() => {
|
||||
finished = true;
|
||||
});
|
||||
});
|
||||
@ -87,12 +116,19 @@ describe('AsyncTestZoneSpec', function() {
|
||||
expect(finished).toBe(true);
|
||||
done();
|
||||
},
|
||||
() => { done.fail('async zone called failCallback unexpectedly'); }, 'name');
|
||||
() => {
|
||||
done.fail('async zone called failCallback unexpectedly');
|
||||
},
|
||||
'name');
|
||||
|
||||
const atz = Zone.current.fork(testZoneSpec);
|
||||
|
||||
atz.run(function() {
|
||||
Promise.resolve().then(() => { setTimeout(() => { finished = true; }, 10); });
|
||||
Promise.resolve().then(() => {
|
||||
setTimeout(() => {
|
||||
finished = true;
|
||||
}, 10);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -102,7 +138,9 @@ describe('AsyncTestZoneSpec', function() {
|
||||
button = document.createElement('button');
|
||||
document.body.appendChild(button);
|
||||
});
|
||||
afterEach(function() { document.body.removeChild(button); });
|
||||
afterEach(function() {
|
||||
document.body.removeChild(button);
|
||||
});
|
||||
|
||||
it('should call finish because an event task is considered as sync', (done) => {
|
||||
let finished = false;
|
||||
@ -112,12 +150,17 @@ describe('AsyncTestZoneSpec', function() {
|
||||
expect(finished).toBe(true);
|
||||
done();
|
||||
},
|
||||
() => { done.fail('async zone called failCallback unexpectedly'); }, 'name');
|
||||
() => {
|
||||
done.fail('async zone called failCallback unexpectedly');
|
||||
},
|
||||
'name');
|
||||
|
||||
const atz = Zone.current.fork(testZoneSpec);
|
||||
|
||||
atz.run(function() {
|
||||
const listener = () => { finished = true; };
|
||||
const listener = () => {
|
||||
finished = true;
|
||||
};
|
||||
button.addEventListener('click', listener);
|
||||
|
||||
const clickEvent = document.createEvent('Event');
|
||||
@ -135,13 +178,19 @@ describe('AsyncTestZoneSpec', function() {
|
||||
expect(finished).toBe(true);
|
||||
done();
|
||||
},
|
||||
() => { done.fail('async zone called failCallback unexpectedly'); }, 'name');
|
||||
() => {
|
||||
done.fail('async zone called failCallback unexpectedly');
|
||||
},
|
||||
'name');
|
||||
|
||||
const atz = Zone.current.fork(testZoneSpec);
|
||||
|
||||
atz.run(function() {
|
||||
button.addEventListener(
|
||||
'click', () => { setTimeout(() => { finished = true; }, 10); });
|
||||
button.addEventListener('click', () => {
|
||||
setTimeout(() => {
|
||||
finished = true;
|
||||
}, 10);
|
||||
});
|
||||
|
||||
const clickEvent = document.createEvent('Event');
|
||||
clickEvent.initEvent('click', true, true);
|
||||
@ -161,7 +210,9 @@ describe('AsyncTestZoneSpec', function() {
|
||||
expect(finished).toBe(true);
|
||||
done();
|
||||
},
|
||||
(err: Error) => { done.fail('async zone called failCallback unexpectedly'); },
|
||||
(err: Error) => {
|
||||
done.fail('async zone called failCallback unexpectedly');
|
||||
},
|
||||
'name');
|
||||
|
||||
const atz = Zone.current.fork(testZoneSpec);
|
||||
@ -184,7 +235,9 @@ describe('AsyncTestZoneSpec', function() {
|
||||
let req: XMLHttpRequest;
|
||||
|
||||
const testZoneSpec = new AsyncTestZoneSpec(
|
||||
() => { done.fail('expected failCallback to be called'); },
|
||||
() => {
|
||||
done.fail('expected failCallback to be called');
|
||||
},
|
||||
(err: Error) => {
|
||||
expect(err.message).toEqual('bad url failure');
|
||||
done();
|
||||
@ -208,17 +261,28 @@ describe('AsyncTestZoneSpec', function() {
|
||||
|
||||
it('should not fail if setInterval is used and canceled', (done) => {
|
||||
const testZoneSpec = new AsyncTestZoneSpec(
|
||||
() => { done(); },
|
||||
(err: Error) => { done.fail('async zone called failCallback unexpectedly'); }, 'name');
|
||||
() => {
|
||||
done();
|
||||
},
|
||||
(err: Error) => {
|
||||
done.fail('async zone called failCallback unexpectedly');
|
||||
},
|
||||
'name');
|
||||
|
||||
const atz = Zone.current.fork(testZoneSpec);
|
||||
|
||||
atz.run(function() { let id = setInterval(() => { clearInterval(id); }, 100); });
|
||||
atz.run(function() {
|
||||
let id = setInterval(() => {
|
||||
clearInterval(id);
|
||||
}, 100);
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail if an error is thrown asynchronously', (done) => {
|
||||
const testZoneSpec = new AsyncTestZoneSpec(
|
||||
() => { done.fail('expected failCallback to be called'); },
|
||||
() => {
|
||||
done.fail('expected failCallback to be called');
|
||||
},
|
||||
(err: Error) => {
|
||||
expect(err.message).toEqual('my error');
|
||||
done();
|
||||
@ -227,12 +291,18 @@ describe('AsyncTestZoneSpec', function() {
|
||||
|
||||
const atz = Zone.current.fork(testZoneSpec);
|
||||
|
||||
atz.run(function() { setTimeout(() => { throw new Error('my error'); }, 10); });
|
||||
atz.run(function() {
|
||||
setTimeout(() => {
|
||||
throw new Error('my error');
|
||||
}, 10);
|
||||
});
|
||||
});
|
||||
|
||||
it('should fail if a promise rejection is unhandled', (done) => {
|
||||
const testZoneSpec = new AsyncTestZoneSpec(
|
||||
() => { done.fail('expected failCallback to be called'); },
|
||||
() => {
|
||||
done.fail('expected failCallback to be called');
|
||||
},
|
||||
(err: Error) => {
|
||||
expect(err.message).toEqual('Uncaught (in promise): my reason');
|
||||
done();
|
||||
@ -241,7 +311,9 @@ describe('AsyncTestZoneSpec', function() {
|
||||
|
||||
const atz = Zone.current.fork(testZoneSpec);
|
||||
|
||||
atz.run(function() { Promise.reject('my reason'); });
|
||||
atz.run(function() {
|
||||
Promise.reject('my reason');
|
||||
});
|
||||
});
|
||||
|
||||
const asyncTest: any = (Zone as any)[Zone.__symbol__('asyncTest')];
|
||||
@ -263,17 +335,25 @@ describe('AsyncTestZoneSpec', function() {
|
||||
let finished = false;
|
||||
const _global: any =
|
||||
typeof window !== 'undefined' && window || typeof self !== 'undefined' && self || global;
|
||||
beforeEach(() => { _global[Zone.__symbol__('supportWaitUnResolvedChainedPromise')] = true; });
|
||||
afterEach(() => { _global[Zone.__symbol__('supportWaitUnResolvedChainedPromise')] = false; });
|
||||
beforeEach(() => {
|
||||
_global[Zone.__symbol__('supportWaitUnResolvedChainedPromise')] = true;
|
||||
});
|
||||
afterEach(() => {
|
||||
_global[Zone.__symbol__('supportWaitUnResolvedChainedPromise')] = false;
|
||||
});
|
||||
it('should be able to detect non zone aware async task in promise',
|
||||
wrapAsyncTest(
|
||||
() => {
|
||||
new Promise((res, rej) => {
|
||||
const g: any = typeof window === 'undefined' ? global : window;
|
||||
g[Zone.__symbol__('setTimeout')](res, 100);
|
||||
}).then(() => { finished = true; });
|
||||
}).then(() => {
|
||||
finished = true;
|
||||
});
|
||||
},
|
||||
() => { expect(finished).toBe(true); }));
|
||||
() => {
|
||||
expect(finished).toBe(true);
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
@ -281,7 +361,11 @@ describe('AsyncTestZoneSpec', function() {
|
||||
const logs: string[] = [];
|
||||
it('should automatically done after async tasks finished',
|
||||
wrapAsyncTest(
|
||||
() => { setTimeout(() => { logs.push('timeout'); }, 100); },
|
||||
() => {
|
||||
setTimeout(() => {
|
||||
logs.push('timeout');
|
||||
}, 100);
|
||||
},
|
||||
() => {
|
||||
expect(logs).toEqual(['timeout']);
|
||||
logs.splice(0);
|
||||
@ -292,7 +376,9 @@ describe('AsyncTestZoneSpec', function() {
|
||||
() => {
|
||||
setTimeout(() => {
|
||||
logs.push('timeout');
|
||||
setTimeout(() => { logs.push('nested timeout'); }, 100);
|
||||
setTimeout(() => {
|
||||
logs.push('nested timeout');
|
||||
}, 100);
|
||||
}, 100);
|
||||
},
|
||||
() => {
|
||||
@ -303,9 +389,13 @@ describe('AsyncTestZoneSpec', function() {
|
||||
it('should automatically done after multiple async tasks finished',
|
||||
wrapAsyncTest(
|
||||
() => {
|
||||
setTimeout(() => { logs.push('1st timeout'); }, 100);
|
||||
setTimeout(() => {
|
||||
logs.push('1st timeout');
|
||||
}, 100);
|
||||
|
||||
setTimeout(() => { logs.push('2nd timeout'); }, 100);
|
||||
setTimeout(() => {
|
||||
logs.push('2nd timeout');
|
||||
}, 100);
|
||||
},
|
||||
() => {
|
||||
expect(logs).toEqual(['1st timeout', '2nd timeout']);
|
||||
@ -323,7 +413,11 @@ describe('AsyncTestZoneSpec', function() {
|
||||
|
||||
it('should automatically done after async tasks finished',
|
||||
wrapAsyncTest(
|
||||
() => { setTimeout(() => { logs.push('timeout'); }, 100); },
|
||||
() => {
|
||||
setTimeout(() => {
|
||||
logs.push('timeout');
|
||||
}, 100);
|
||||
},
|
||||
() => {
|
||||
expect(logs).toEqual(['beforeEach', 'timeout']);
|
||||
}));
|
||||
@ -341,7 +435,11 @@ describe('AsyncTestZoneSpec', function() {
|
||||
|
||||
it('should automatically done after async tasks finished',
|
||||
wrapAsyncTest(
|
||||
() => { setTimeout(() => { logs.push('timeout'); }, 100); },
|
||||
() => {
|
||||
setTimeout(() => {
|
||||
logs.push('timeout');
|
||||
}, 100);
|
||||
},
|
||||
() => {
|
||||
expect(logs).toEqual(['beforeEach', 'timeout']);
|
||||
}));
|
||||
@ -351,7 +449,9 @@ describe('AsyncTestZoneSpec', function() {
|
||||
() => {
|
||||
setTimeout(() => {
|
||||
logs.push('timeout');
|
||||
setTimeout(() => { logs.push('nested timeout'); }, 100);
|
||||
setTimeout(() => {
|
||||
logs.push('nested timeout');
|
||||
}, 100);
|
||||
}, 100);
|
||||
},
|
||||
() => {
|
||||
@ -361,9 +461,13 @@ describe('AsyncTestZoneSpec', function() {
|
||||
it('should automatically done after multiple async tasks finished',
|
||||
wrapAsyncTest(
|
||||
() => {
|
||||
setTimeout(() => { logs.push('1st timeout'); }, 100);
|
||||
setTimeout(() => {
|
||||
logs.push('1st timeout');
|
||||
}, 100);
|
||||
|
||||
setTimeout(() => { logs.push('2nd timeout'); }, 100);
|
||||
setTimeout(() => {
|
||||
logs.push('2nd timeout');
|
||||
}, 100);
|
||||
},
|
||||
() => {
|
||||
expect(logs).toEqual(['beforeEach', '1st timeout', '2nd timeout']);
|
||||
@ -380,11 +484,17 @@ describe('AsyncTestZoneSpec', function() {
|
||||
}, 100);
|
||||
}));
|
||||
|
||||
afterEach(() => { logs.splice(0); });
|
||||
afterEach(() => {
|
||||
logs.splice(0);
|
||||
});
|
||||
|
||||
it('should automatically done after async tasks finished',
|
||||
wrapAsyncTest(
|
||||
() => { setTimeout(() => { logs.push('timeout'); }, 100); },
|
||||
() => {
|
||||
setTimeout(() => {
|
||||
logs.push('timeout');
|
||||
}, 100);
|
||||
},
|
||||
() => {
|
||||
expect(logs).toEqual(['beforeEach', 'timeout']);
|
||||
}));
|
||||
@ -400,11 +510,19 @@ describe('AsyncTestZoneSpec', function() {
|
||||
}, 100);
|
||||
}));
|
||||
|
||||
afterEach(wrapAsyncTest(() => { setTimeout(() => { logs.splice(0); }, 100); }));
|
||||
afterEach(wrapAsyncTest(() => {
|
||||
setTimeout(() => {
|
||||
logs.splice(0);
|
||||
}, 100);
|
||||
}));
|
||||
|
||||
it('should automatically done after async tasks finished',
|
||||
wrapAsyncTest(
|
||||
() => { setTimeout(() => { logs.push('timeout'); }, 100); },
|
||||
() => {
|
||||
setTimeout(() => {
|
||||
logs.push('timeout');
|
||||
}, 100);
|
||||
},
|
||||
() => {
|
||||
expect(logs).toEqual(['beforeEach', 'timeout']);
|
||||
}));
|
||||
|
Reference in New Issue
Block a user