@ -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']);
|
||||
}));
|
||||
|
@ -39,21 +39,26 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
});
|
||||
|
||||
it('sets the FakeAsyncTestZoneSpec property', () => {
|
||||
fakeAsyncTestZone.run(
|
||||
() => { expect(Zone.current.get('FakeAsyncTestZoneSpec')).toEqual(testZoneSpec); });
|
||||
fakeAsyncTestZone.run(() => {
|
||||
expect(Zone.current.get('FakeAsyncTestZoneSpec')).toEqual(testZoneSpec);
|
||||
});
|
||||
});
|
||||
|
||||
describe('synchronous code', () => {
|
||||
it('should run', () => {
|
||||
let ran = false;
|
||||
fakeAsyncTestZone.run(() => { ran = true; });
|
||||
fakeAsyncTestZone.run(() => {
|
||||
ran = true;
|
||||
});
|
||||
|
||||
expect(ran).toEqual(true);
|
||||
});
|
||||
|
||||
it('should throw the error in the code', () => {
|
||||
expect(() => {
|
||||
fakeAsyncTestZone.run(() => { throw new Error('sync'); });
|
||||
fakeAsyncTestZone.run(() => {
|
||||
throw new Error('sync');
|
||||
});
|
||||
}).toThrowError('sync');
|
||||
});
|
||||
|
||||
@ -71,7 +76,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should run', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let thenRan = false;
|
||||
Promise.resolve(null).then((_) => { thenRan = true; });
|
||||
Promise.resolve(null).then((_) => {
|
||||
thenRan = true;
|
||||
});
|
||||
|
||||
expect(thenRan).toEqual(false);
|
||||
|
||||
@ -83,7 +90,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should rethrow the exception on flushMicroTasks for error thrown in Promise callback',
|
||||
() => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
Promise.resolve(null).then((_) => { throw new Error('async'); });
|
||||
Promise.resolve(null).then((_) => {
|
||||
throw new Error('async');
|
||||
});
|
||||
expect(() => {
|
||||
testZoneSpec.flushMicrotasks();
|
||||
}).toThrowError(/Uncaught \(in promise\): Error: async/);
|
||||
@ -124,7 +133,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should run queued zero duration timer on zero tick', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
setTimeout(() => { ran = true; }, 0);
|
||||
setTimeout(() => {
|
||||
ran = true;
|
||||
}, 0);
|
||||
|
||||
expect(ran).toEqual(false);
|
||||
|
||||
@ -136,7 +147,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should run queued immediate timer on zero tick', ifEnvSupports('setImmediate', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
setImmediate(() => { ran = true; });
|
||||
setImmediate(() => {
|
||||
ran = true;
|
||||
});
|
||||
|
||||
expect(ran).toEqual(false);
|
||||
|
||||
@ -178,7 +191,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should run queued timer after sufficient clock ticks', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
setTimeout(() => { ran = true; }, 10);
|
||||
setTimeout(() => {
|
||||
ran = true;
|
||||
}, 10);
|
||||
|
||||
testZoneSpec.tick(6);
|
||||
expect(ran).toEqual(false);
|
||||
@ -191,7 +206,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should run doTick callback even if no work ran', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let totalElapsed = 0;
|
||||
function doTick(elapsed: number) { totalElapsed += elapsed; }
|
||||
function doTick(elapsed: number) {
|
||||
totalElapsed += elapsed;
|
||||
}
|
||||
setTimeout(() => {}, 10);
|
||||
|
||||
testZoneSpec.tick(6, doTick);
|
||||
@ -231,7 +248,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should run queued timer only once', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let cycles = 0;
|
||||
setTimeout(() => { cycles++; }, 10);
|
||||
setTimeout(() => {
|
||||
cycles++;
|
||||
}, 10);
|
||||
|
||||
testZoneSpec.tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
@ -248,7 +267,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should not run cancelled timer', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
let id: any = setTimeout(() => { ran = true; }, 10);
|
||||
let id: any = setTimeout(() => {
|
||||
ran = true;
|
||||
}, 10);
|
||||
clearTimeout(id);
|
||||
|
||||
testZoneSpec.tick(10);
|
||||
@ -259,7 +280,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should pass arguments to times', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let value = 'genuine value';
|
||||
let id = setTimeout((arg1, arg2) => { value = arg1 + arg2; }, 0, 'expected', ' value');
|
||||
let id = setTimeout((arg1, arg2) => {
|
||||
value = arg1 + arg2;
|
||||
}, 0, 'expected', ' value');
|
||||
|
||||
testZoneSpec.tick();
|
||||
expect(value).toEqual('expected value');
|
||||
@ -269,7 +292,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should pass arguments to setImmediate', ifEnvSupports('setImmediate', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let value = 'genuine value';
|
||||
let id = setImmediate((arg1, arg2) => { value = arg1 + arg2; }, 'expected', ' value');
|
||||
let id = setImmediate((arg1, arg2) => {
|
||||
value = arg1 + arg2;
|
||||
}, 'expected', ' value');
|
||||
|
||||
testZoneSpec.tick();
|
||||
expect(value).toEqual('expected value');
|
||||
@ -279,7 +304,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should run periodic timers', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let cycles = 0;
|
||||
let id = setInterval(() => { cycles++; }, 10);
|
||||
let id = setInterval(() => {
|
||||
cycles++;
|
||||
}, 10);
|
||||
|
||||
expect(id).toBeGreaterThan(0);
|
||||
|
||||
@ -300,7 +327,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should pass arguments to periodic timers', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let value = 'genuine value';
|
||||
let id = setInterval((arg1, arg2) => { value = arg1 + arg2; }, 10, 'expected', ' value');
|
||||
let id = setInterval((arg1, arg2) => {
|
||||
value = arg1 + arg2;
|
||||
}, 10, 'expected', ' value');
|
||||
|
||||
testZoneSpec.tick(10);
|
||||
expect(value).toEqual('expected value');
|
||||
@ -310,7 +339,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should not run cancelled periodic timer', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
let id = setInterval(() => { ran = true; }, 10);
|
||||
let id = setInterval(() => {
|
||||
ran = true;
|
||||
}, 10);
|
||||
|
||||
testZoneSpec.tick(10);
|
||||
expect(ran).toEqual(true);
|
||||
@ -328,9 +359,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
let id: number;
|
||||
|
||||
id = setInterval(() => {
|
||||
cycles++;
|
||||
clearInterval(id);
|
||||
}, 10) as any as number;
|
||||
cycles++;
|
||||
clearInterval(id);
|
||||
}, 10) as any as number;
|
||||
|
||||
testZoneSpec.tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
@ -387,8 +418,12 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
|
||||
it('should throw the exception from tick for error thrown in timer callback', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
setTimeout(() => { throw new Error('timer'); }, 10);
|
||||
expect(() => { testZoneSpec.tick(10); }).toThrowError('timer');
|
||||
setTimeout(() => {
|
||||
throw new Error('timer');
|
||||
}, 10);
|
||||
expect(() => {
|
||||
testZoneSpec.tick(10);
|
||||
}).toThrowError('timer');
|
||||
});
|
||||
// There should be no pending timers after the error in timer callback.
|
||||
expect(testZoneSpec.pendingTimers.length).toBe(0);
|
||||
@ -402,7 +437,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
throw new Error(count.toString());
|
||||
}, 10);
|
||||
|
||||
expect(() => { testZoneSpec.tick(10); }).toThrowError('1');
|
||||
expect(() => {
|
||||
testZoneSpec.tick(10);
|
||||
}).toThrowError('1');
|
||||
|
||||
// Periodic timer is cancelled on first error.
|
||||
expect(count).toBe(1);
|
||||
@ -417,9 +454,15 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should be able to resume processing timer callbacks after handling an error', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
setTimeout(() => { throw new Error('timer'); }, 10);
|
||||
setTimeout(() => { ran = true; }, 10);
|
||||
expect(() => { testZoneSpec.tick(10); }).toThrowError('timer');
|
||||
setTimeout(() => {
|
||||
throw new Error('timer');
|
||||
}, 10);
|
||||
setTimeout(() => {
|
||||
ran = true;
|
||||
}, 10);
|
||||
expect(() => {
|
||||
testZoneSpec.tick(10);
|
||||
}).toThrowError('timer');
|
||||
expect(ran).toBe(false);
|
||||
|
||||
// Restart timer queue processing.
|
||||
@ -437,9 +480,15 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
let y = false;
|
||||
let z = false;
|
||||
|
||||
setTimeout(() => { x = true; }, 10);
|
||||
setTimeout(() => { y = true; }, 100);
|
||||
setTimeout(() => { z = true; }, 70);
|
||||
setTimeout(() => {
|
||||
x = true;
|
||||
}, 10);
|
||||
setTimeout(() => {
|
||||
y = true;
|
||||
}, 100);
|
||||
setTimeout(() => {
|
||||
z = true;
|
||||
}, 70);
|
||||
|
||||
let elapsed = testZoneSpec.flush();
|
||||
|
||||
@ -456,7 +505,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
let y = true;
|
||||
setTimeout(() => {
|
||||
x = true;
|
||||
setTimeout(() => { y = true; }, 100);
|
||||
setTimeout(() => {
|
||||
y = true;
|
||||
}, 100);
|
||||
}, 200);
|
||||
|
||||
let elapsed = testZoneSpec.flush();
|
||||
@ -473,9 +524,15 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
let y = false;
|
||||
let z = 0;
|
||||
|
||||
setTimeout(() => { x = true; }, 50);
|
||||
setTimeout(() => { y = true; }, 141);
|
||||
setInterval(() => { z++; }, 10);
|
||||
setTimeout(() => {
|
||||
x = true;
|
||||
}, 50);
|
||||
setTimeout(() => {
|
||||
y = true;
|
||||
}, 141);
|
||||
setInterval(() => {
|
||||
z++;
|
||||
}, 10);
|
||||
|
||||
let elapsed = testZoneSpec.flush();
|
||||
|
||||
@ -490,7 +547,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let z = 0;
|
||||
|
||||
setInterval(() => { z++; }, 10);
|
||||
setInterval(() => {
|
||||
z++;
|
||||
}, 10);
|
||||
|
||||
let elapsed = testZoneSpec.flush();
|
||||
|
||||
@ -524,8 +583,12 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
|
||||
it('should throw the exception from tick for error thrown in timer callback', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
setTimeout(() => { throw new Error('timer'); }, 10);
|
||||
expect(() => { testZoneSpec.flush(); }).toThrowError('timer');
|
||||
setTimeout(() => {
|
||||
throw new Error('timer');
|
||||
}, 10);
|
||||
expect(() => {
|
||||
testZoneSpec.flush();
|
||||
}).toThrowError('timer');
|
||||
});
|
||||
// There should be no pending timers after the error in timer callback.
|
||||
expect(testZoneSpec.pendingTimers.length).toBe(0);
|
||||
@ -575,7 +638,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let x = 0;
|
||||
|
||||
setInterval(() => { x++; }, 10);
|
||||
setInterval(() => {
|
||||
x++;
|
||||
}, 10);
|
||||
|
||||
let elapsed = testZoneSpec.flush(20, true);
|
||||
|
||||
@ -589,9 +654,13 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
|
||||
setInterval(() => { x++; }, 10);
|
||||
setInterval(() => {
|
||||
x++;
|
||||
}, 10);
|
||||
|
||||
setInterval(() => { y++; }, 100);
|
||||
setInterval(() => {
|
||||
y++;
|
||||
}, 100);
|
||||
|
||||
let elapsed = testZoneSpec.flush(20, true);
|
||||
|
||||
@ -606,11 +675,15 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
|
||||
setInterval(() => { x++; }, 10);
|
||||
setInterval(() => {
|
||||
x++;
|
||||
}, 10);
|
||||
|
||||
// This shouldn't cause the flush to throw an exception even though
|
||||
// it would require 100 iterations of the shorter timer.
|
||||
setInterval(() => { y++; }, 1000);
|
||||
setInterval(() => {
|
||||
y++;
|
||||
}, 1000);
|
||||
|
||||
let elapsed = testZoneSpec.flush(20, true);
|
||||
|
||||
@ -644,7 +717,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('should schedule a requestAnimationFrame with timeout of 16ms', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
requestAnimationFrame(() => { ran = true; });
|
||||
requestAnimationFrame(() => {
|
||||
ran = true;
|
||||
});
|
||||
|
||||
testZoneSpec.tick(6);
|
||||
expect(ran).toEqual(false);
|
||||
@ -654,14 +729,18 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
});
|
||||
});
|
||||
it('does not count as a pending timer', () => {
|
||||
fakeAsyncTestZone.run(() => { requestAnimationFrame(() => {}); });
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrame(() => {});
|
||||
});
|
||||
expect(testZoneSpec.pendingTimers.length).toBe(0);
|
||||
expect(testZoneSpec.pendingPeriodicTimers.length).toBe(0);
|
||||
});
|
||||
it('should cancel a scheduled requestAnimatiomFrame', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let ran = false;
|
||||
const id = requestAnimationFrame(() => { ran = true; });
|
||||
const id = requestAnimationFrame(() => {
|
||||
ran = true;
|
||||
});
|
||||
|
||||
testZoneSpec.tick(6);
|
||||
expect(ran).toEqual(false);
|
||||
@ -675,7 +754,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('is not flushed when flushPeriodic is false', () => {
|
||||
let ran = false;
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrame(() => { ran = true; });
|
||||
requestAnimationFrame(() => {
|
||||
ran = true;
|
||||
});
|
||||
testZoneSpec.flush(20);
|
||||
expect(ran).toEqual(false);
|
||||
});
|
||||
@ -683,7 +764,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
it('is flushed when flushPeriodic is true', () => {
|
||||
let ran = false;
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrame(() => { ran = true; });
|
||||
requestAnimationFrame(() => {
|
||||
ran = true;
|
||||
});
|
||||
const elapsed = testZoneSpec.flush(20, true);
|
||||
expect(elapsed).toEqual(16);
|
||||
expect(ran).toEqual(true);
|
||||
@ -695,7 +778,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
requestAnimationFrame((ts) => {
|
||||
timestamp = ts;
|
||||
requestAnimationFrame(ts1 => { timestamp1 = ts1; });
|
||||
requestAnimationFrame(ts1 => {
|
||||
timestamp1 = ts1;
|
||||
});
|
||||
});
|
||||
const elapsed = testZoneSpec.flush(20, true);
|
||||
const elapsed1 = testZoneSpec.flush(20, true);
|
||||
@ -746,7 +831,10 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
expect(strArg).toEqual('stringArg');
|
||||
cbArg();
|
||||
},
|
||||
'stringArg', () => { cbArgRun = true; });
|
||||
'stringArg',
|
||||
() => {
|
||||
cbArgRun = true;
|
||||
});
|
||||
|
||||
expect(tickRun).toEqual(false);
|
||||
|
||||
@ -802,7 +890,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
({name: 'TestClass.myTimeout', target: self, cbIdx: 0, args: args}));
|
||||
|
||||
const testClass = new TestClass();
|
||||
testClass.myTimeout(() => { ran = true; });
|
||||
testClass.myTimeout(() => {
|
||||
ran = true;
|
||||
});
|
||||
|
||||
expect(ran).toEqual(false);
|
||||
|
||||
@ -819,7 +909,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
fakeAsyncTestZone.run(() => {
|
||||
let cycle = 0;
|
||||
class TestClass {
|
||||
myInterval(callback: Function, interval: number): any { return null; }
|
||||
myInterval(callback: Function, interval: number): any {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
patchMacroTask(
|
||||
TestClass.prototype, 'myInterval',
|
||||
@ -827,7 +919,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
({name: 'TestClass.myInterval', target: self, cbIdx: 0, args: args}));
|
||||
|
||||
const testClass = new TestClass();
|
||||
const id = testClass.myInterval(() => { cycle++; }, 10);
|
||||
const id = testClass.myInterval(() => {
|
||||
cycle++;
|
||||
}, 10);
|
||||
|
||||
expect(cycle).toEqual(0);
|
||||
|
||||
@ -843,7 +937,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
|
||||
describe('return promise', () => {
|
||||
let log: string[];
|
||||
beforeEach(() => { log = []; });
|
||||
beforeEach(() => {
|
||||
log = [];
|
||||
});
|
||||
|
||||
it('should wait for promise to resolve', () => {
|
||||
return new Promise((res, _) => {
|
||||
@ -854,7 +950,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(() => { expect(log).toEqual(['resolved']); });
|
||||
afterEach(() => {
|
||||
expect(log).toEqual(['resolved']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fakeAsyncTest should patch Date', () => {
|
||||
@ -918,7 +1016,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
describe(
|
||||
'fakeAsyncTest should work without patch jasmine.clock',
|
||||
ifEnvSupports(
|
||||
() => { return !supportClock() && supportNode(); },
|
||||
() => {
|
||||
return !supportClock() && supportNode();
|
||||
},
|
||||
() => {
|
||||
const fakeAsync = (Zone as any)[Zone.__symbol__('fakeAsyncTest')].fakeAsync;
|
||||
let spy: any;
|
||||
@ -927,7 +1027,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
jasmine.clock().install();
|
||||
});
|
||||
|
||||
afterEach(() => { jasmine.clock().uninstall(); });
|
||||
afterEach(() => {
|
||||
jasmine.clock().uninstall();
|
||||
});
|
||||
|
||||
it('should check date type correctly', fakeAsync(() => {
|
||||
const d: any = new Date();
|
||||
@ -1023,7 +1125,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
jasmine.clock().install();
|
||||
});
|
||||
|
||||
afterEach(() => { jasmine.clock().uninstall(); });
|
||||
afterEach(() => {
|
||||
jasmine.clock().uninstall();
|
||||
});
|
||||
|
||||
it('should check date type correctly', () => {
|
||||
const d: any = new Date();
|
||||
@ -1091,7 +1195,9 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
subscribe.next('hello');
|
||||
subscribe.complete();
|
||||
});
|
||||
observable.pipe(delay(1000)).subscribe((v: any) => { result = v; });
|
||||
observable.pipe(delay(1000)).subscribe((v: any) => {
|
||||
result = v;
|
||||
});
|
||||
expect(result).toBe(null);
|
||||
testZoneSpec.tick(1000);
|
||||
expect(result).toBe('hello');
|
||||
@ -1104,9 +1210,13 @@ describe('FakeAsyncTestZoneSpec', () => {
|
||||
class Log {
|
||||
logItems: any[];
|
||||
|
||||
constructor() { this.logItems = []; }
|
||||
constructor() {
|
||||
this.logItems = [];
|
||||
}
|
||||
|
||||
add(value: any /** TODO #9100 */): void { this.logItems.push(value); }
|
||||
add(value: any /** TODO #9100 */): void {
|
||||
this.logItems.push(value);
|
||||
}
|
||||
|
||||
fn(value: any /** TODO #9100 */) {
|
||||
return (a1: any = null, a2: any = null, a3: any = null, a4: any = null, a5: any = null) => {
|
||||
@ -1114,9 +1224,13 @@ class Log {
|
||||
};
|
||||
}
|
||||
|
||||
clear(): void { this.logItems = []; }
|
||||
clear(): void {
|
||||
this.logItems = [];
|
||||
}
|
||||
|
||||
result(): string { return this.logItems.join('; '); }
|
||||
result(): string {
|
||||
return this.logItems.join('; ');
|
||||
}
|
||||
}
|
||||
|
||||
const resolvedPromise = Promise.resolve(null);
|
||||
@ -1128,7 +1242,9 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
describe('fake async', () => {
|
||||
it('should run synchronous code', () => {
|
||||
let ran = false;
|
||||
fakeAsync(() => { ran = true; })();
|
||||
fakeAsync(() => {
|
||||
ran = true;
|
||||
})();
|
||||
|
||||
expect(ran).toEqual(true);
|
||||
});
|
||||
@ -1143,26 +1259,35 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
|
||||
it('should throw on nested calls', () => {
|
||||
expect(() => {
|
||||
fakeAsync(() => { fakeAsync((): any /** TODO #9100 */ => null)(); })();
|
||||
fakeAsync(() => {
|
||||
fakeAsync((): any /** TODO #9100 */ => null)();
|
||||
})();
|
||||
}).toThrowError('fakeAsync() calls can not be nested');
|
||||
});
|
||||
|
||||
it('should flush microtasks before returning', () => {
|
||||
let thenRan = false;
|
||||
|
||||
fakeAsync(() => { resolvedPromise.then(_ => { thenRan = true; }); })();
|
||||
fakeAsync(() => {
|
||||
resolvedPromise.then(_ => {
|
||||
thenRan = true;
|
||||
});
|
||||
})();
|
||||
|
||||
expect(thenRan).toEqual(true);
|
||||
});
|
||||
|
||||
|
||||
it('should propagate the return value',
|
||||
() => { expect(fakeAsync(() => 'foo')()).toEqual('foo'); });
|
||||
it('should propagate the return value', () => {
|
||||
expect(fakeAsync(() => 'foo')()).toEqual('foo');
|
||||
});
|
||||
|
||||
describe('Promise', () => {
|
||||
it('should run asynchronous code', fakeAsync(() => {
|
||||
let thenRan = false;
|
||||
resolvedPromise.then((_) => { thenRan = true; });
|
||||
resolvedPromise.then((_) => {
|
||||
thenRan = true;
|
||||
});
|
||||
|
||||
expect(thenRan).toEqual(false);
|
||||
|
||||
@ -1198,21 +1323,29 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
it('should complain if the test throws an exception during async calls', () => {
|
||||
expect(() => {
|
||||
fakeAsync(() => {
|
||||
resolvedPromise.then((_) => { throw new Error('async'); });
|
||||
resolvedPromise.then((_) => {
|
||||
throw new Error('async');
|
||||
});
|
||||
flushMicrotasks();
|
||||
})();
|
||||
}).toThrowError(/Uncaught \(in promise\): Error: async/);
|
||||
});
|
||||
|
||||
it('should complain if a test throws an exception', () => {
|
||||
expect(() => { fakeAsync(() => { throw new Error('sync'); })(); }).toThrowError('sync');
|
||||
expect(() => {
|
||||
fakeAsync(() => {
|
||||
throw new Error('sync');
|
||||
})();
|
||||
}).toThrowError('sync');
|
||||
});
|
||||
});
|
||||
|
||||
describe('timers', () => {
|
||||
it('should run queued zero duration timer on zero tick', fakeAsync(() => {
|
||||
let ran = false;
|
||||
setTimeout(() => { ran = true; }, 0);
|
||||
setTimeout(() => {
|
||||
ran = true;
|
||||
}, 0);
|
||||
|
||||
expect(ran).toEqual(false);
|
||||
|
||||
@ -1223,7 +1356,9 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
|
||||
it('should run queued timer after sufficient clock ticks', fakeAsync(() => {
|
||||
let ran = false;
|
||||
setTimeout(() => { ran = true; }, 10);
|
||||
setTimeout(() => {
|
||||
ran = true;
|
||||
}, 10);
|
||||
|
||||
tick(6);
|
||||
expect(ran).toEqual(false);
|
||||
@ -1234,7 +1369,9 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
|
||||
it('should run queued timer only once', fakeAsync(() => {
|
||||
let cycles = 0;
|
||||
setTimeout(() => { cycles++; }, 10);
|
||||
setTimeout(() => {
|
||||
cycles++;
|
||||
}, 10);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
@ -1248,7 +1385,9 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
|
||||
it('should not run cancelled timer', fakeAsync(() => {
|
||||
let ran = false;
|
||||
const id = setTimeout(() => { ran = true; }, 10);
|
||||
const id = setTimeout(() => {
|
||||
ran = true;
|
||||
}, 10);
|
||||
clearTimeout(id);
|
||||
|
||||
tick(10);
|
||||
@ -1257,19 +1396,25 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
|
||||
it('should throw an error on dangling timers', () => {
|
||||
expect(() => {
|
||||
fakeAsync(() => { setTimeout(() => {}, 10); })();
|
||||
fakeAsync(() => {
|
||||
setTimeout(() => {}, 10);
|
||||
})();
|
||||
}).toThrowError('1 timer(s) still in the queue.');
|
||||
});
|
||||
|
||||
it('should throw an error on dangling periodic timers', () => {
|
||||
expect(() => {
|
||||
fakeAsync(() => { setInterval(() => {}, 10); })();
|
||||
fakeAsync(() => {
|
||||
setInterval(() => {}, 10);
|
||||
})();
|
||||
}).toThrowError('1 periodic timer(s) still in the queue.');
|
||||
});
|
||||
|
||||
it('should run periodic timers', fakeAsync(() => {
|
||||
let cycles = 0;
|
||||
const id = setInterval(() => { cycles++; }, 10);
|
||||
const id = setInterval(() => {
|
||||
cycles++;
|
||||
}, 10);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
@ -1284,7 +1429,9 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
|
||||
it('should not run cancelled periodic timer', fakeAsync(() => {
|
||||
let ran = false;
|
||||
const id = setInterval(() => { ran = true; }, 10);
|
||||
const id = setInterval(() => {
|
||||
ran = true;
|
||||
}, 10);
|
||||
clearInterval(id);
|
||||
|
||||
tick(10);
|
||||
@ -1309,7 +1456,9 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
|
||||
it('should clear periodic timers', fakeAsync(() => {
|
||||
let cycles = 0;
|
||||
const id = setInterval(() => { cycles++; }, 10);
|
||||
const id = setInterval(() => {
|
||||
cycles++;
|
||||
}, 10);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
@ -1369,7 +1518,9 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
|
||||
it('should flush tasks', fakeAsync(() => {
|
||||
let ran = false;
|
||||
setTimeout(() => { ran = true; }, 10);
|
||||
setTimeout(() => {
|
||||
ran = true;
|
||||
}, 10);
|
||||
|
||||
flush();
|
||||
expect(ran).toEqual(true);
|
||||
@ -1378,8 +1529,12 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
it('should flush multiple tasks', fakeAsync(() => {
|
||||
let ran = false;
|
||||
let ran2 = false;
|
||||
setTimeout(() => { ran = true; }, 10);
|
||||
setTimeout(() => { ran2 = true; }, 30);
|
||||
setTimeout(() => {
|
||||
ran = true;
|
||||
}, 10);
|
||||
setTimeout(() => {
|
||||
ran2 = true;
|
||||
}, 30);
|
||||
|
||||
let elapsed = flush();
|
||||
|
||||
@ -1391,8 +1546,12 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
it('should move periodic tasks', fakeAsync(() => {
|
||||
let ran = false;
|
||||
let count = 0;
|
||||
setInterval(() => { count++; }, 10);
|
||||
setTimeout(() => { ran = true; }, 35);
|
||||
setInterval(() => {
|
||||
count++;
|
||||
}, 10);
|
||||
setTimeout(() => {
|
||||
ran = true;
|
||||
}, 35);
|
||||
|
||||
let elapsed = flush();
|
||||
|
||||
@ -1433,7 +1592,9 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
describe('only one `fakeAsync` zone per test', () => {
|
||||
let zoneInBeforeEach: Zone;
|
||||
let zoneInTest1: Zone;
|
||||
beforeEach(fakeAsync(() => { zoneInBeforeEach = Zone.current; }));
|
||||
beforeEach(fakeAsync(() => {
|
||||
zoneInBeforeEach = Zone.current;
|
||||
}));
|
||||
|
||||
it('should use the same zone as in beforeEach', fakeAsync(() => {
|
||||
zoneInTest1 = Zone.current;
|
||||
@ -1481,9 +1642,13 @@ const {fakeAsync, tick, discardPeriodicTasks, flush, flushMicrotasks} = fakeAsyn
|
||||
});
|
||||
|
||||
describe('ProxyZone', () => {
|
||||
beforeEach(() => { ProxyZoneSpec.assertPresent(); });
|
||||
beforeEach(() => {
|
||||
ProxyZoneSpec.assertPresent();
|
||||
});
|
||||
|
||||
afterEach(() => { ProxyZoneSpec.assertPresent(); });
|
||||
afterEach(() => {
|
||||
ProxyZoneSpec.assertPresent();
|
||||
});
|
||||
|
||||
it('should allow fakeAsync zone to retroactively set a zoneSpec outside of fakeAsync', () => {
|
||||
ProxyZoneSpec.assertPresent();
|
||||
|
@ -21,19 +21,22 @@ describe(
|
||||
beforeEach(function() {
|
||||
lstz = Zone.current.fork(longStackTraceZoneSpec).fork({
|
||||
name: 'long-stack-trace-zone-test',
|
||||
onHandleError: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
|
||||
error: any): boolean => {
|
||||
parentZoneDelegate.handleError(targetZone, error);
|
||||
log.push(error);
|
||||
return false;
|
||||
}
|
||||
onHandleError:
|
||||
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any):
|
||||
boolean => {
|
||||
parentZoneDelegate.handleError(targetZone, error);
|
||||
log.push(error);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
log = [];
|
||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
|
||||
});
|
||||
|
||||
afterEach(function() { jasmine.DEFAULT_TIMEOUT_INTERVAL = defaultTimeout; });
|
||||
afterEach(function() {
|
||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = defaultTimeout;
|
||||
});
|
||||
|
||||
function expectElapsed(stack: string, expectedCount: number) {
|
||||
try {
|
||||
@ -52,7 +55,7 @@ describe(
|
||||
setTimeout(function() {
|
||||
setTimeout(function() {
|
||||
setTimeout(function() {
|
||||
expectElapsed(log[0].stack !, 3);
|
||||
expectElapsed(log[0].stack!, 3);
|
||||
done();
|
||||
}, 0);
|
||||
throw new Error('Hello');
|
||||
@ -69,7 +72,9 @@ describe(
|
||||
clickEvent.initEvent('click', true, true);
|
||||
document.body.appendChild(button);
|
||||
|
||||
button.addEventListener('click', function() { expectElapsed(log[0].stack !, 1); });
|
||||
button.addEventListener('click', function() {
|
||||
expectElapsed(log[0].stack!, 1);
|
||||
});
|
||||
|
||||
button.dispatchEvent(clickEvent);
|
||||
|
||||
@ -90,9 +95,13 @@ describe(
|
||||
enterEvent.initEvent('mouseenter', true, true);
|
||||
document.body.appendChild(div);
|
||||
|
||||
button.addEventListener('click', function() { throw new Error('clickError'); });
|
||||
button.addEventListener('click', function() {
|
||||
throw new Error('clickError');
|
||||
});
|
||||
|
||||
div.addEventListener('mouseenter', function() { throw new Error('enterError'); });
|
||||
div.addEventListener('mouseenter', function() {
|
||||
throw new Error('enterError');
|
||||
});
|
||||
|
||||
button.dispatchEvent(clickEvent);
|
||||
div.dispatchEvent(enterEvent);
|
||||
@ -113,9 +122,15 @@ describe(
|
||||
defineProperty(error, 'stack', {
|
||||
configurable: false,
|
||||
get: () => 'someStackTrace',
|
||||
set: (v: any) => { throw new Error('no writes'); }
|
||||
set: (v: any) => {
|
||||
throw new Error('no writes');
|
||||
}
|
||||
});
|
||||
lstz.run(() => {
|
||||
setTimeout(() => {
|
||||
throw error;
|
||||
});
|
||||
});
|
||||
lstz.run(() => { setTimeout(() => { throw error; }); });
|
||||
setTimeout(() => {
|
||||
const e = log[0];
|
||||
expect((e as any).longStack).toBeTruthy();
|
||||
@ -128,11 +143,15 @@ describe(
|
||||
setTimeout(function() {
|
||||
setTimeout(function() {
|
||||
let promise = new Promise(function(resolve, reject) {
|
||||
setTimeout(function() { reject(new Error('Hello Promise')); }, 0);
|
||||
setTimeout(function() {
|
||||
reject(new Error('Hello Promise'));
|
||||
}, 0);
|
||||
});
|
||||
promise.then(function() {
|
||||
fail('should not get here');
|
||||
});
|
||||
promise.then(function() { fail('should not get here'); });
|
||||
setTimeout(function() {
|
||||
expectElapsed(log[0].stack !, 5);
|
||||
expectElapsed(log[0].stack!, 5);
|
||||
done();
|
||||
}, 0);
|
||||
}, 0);
|
||||
@ -171,7 +190,7 @@ describe(
|
||||
setTimeout(function() {
|
||||
setTimeout(function() {
|
||||
if (log[0].stack) {
|
||||
expectElapsed(log[0].stack !, 1);
|
||||
expectElapsed(log[0].stack!, 1);
|
||||
}
|
||||
Error.stackTraceLimit = originalStackTraceLimit;
|
||||
done();
|
||||
|
@ -21,8 +21,9 @@ describe('ProxySpec', () => {
|
||||
});
|
||||
|
||||
describe('properties', () => {
|
||||
it('should expose ProxyZone in the properties',
|
||||
() => { expect(proxyZone.get('ProxyZoneSpec')).toBe(proxyZoneSpec); });
|
||||
it('should expose ProxyZone in the properties', () => {
|
||||
expect(proxyZone.get('ProxyZoneSpec')).toBe(proxyZoneSpec);
|
||||
});
|
||||
|
||||
it('should assert that it is in or out of ProxyZone', () => {
|
||||
let rootZone = Zone.current;
|
||||
@ -79,12 +80,13 @@ describe('ProxySpec', () => {
|
||||
let called = false;
|
||||
proxyZoneSpec.setDelegate({
|
||||
name: '.',
|
||||
onFork: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
|
||||
zoneSpec: ZoneSpec) => {
|
||||
expect(currentZone).toBe(proxyZone);
|
||||
expect(targetZone).toBe(proxyZone), expect(zoneSpec.name).toBe('fork2');
|
||||
called = true;
|
||||
}
|
||||
onFork:
|
||||
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
|
||||
zoneSpec: ZoneSpec) => {
|
||||
expect(currentZone).toBe(proxyZone);
|
||||
expect(targetZone).toBe(proxyZone), expect(zoneSpec.name).toBe('fork2');
|
||||
called = true;
|
||||
}
|
||||
});
|
||||
proxyZone.fork({name: 'fork2'});
|
||||
expect(called).toBe(true);
|
||||
@ -95,8 +97,11 @@ describe('ProxySpec', () => {
|
||||
expect(proxyZone.wrap(fn, 'test')('works')).toEqual('works');
|
||||
proxyZoneSpec.setDelegate({
|
||||
name: '.',
|
||||
onIntercept: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
|
||||
delegate: Function, source: string): Function => { return () => '(works)'; }
|
||||
onIntercept:
|
||||
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
|
||||
delegate: Function, source: string): Function => {
|
||||
return () => '(works)';
|
||||
}
|
||||
});
|
||||
expect(proxyZone.wrap(fn, 'test')('works')).toEqual('(works)');
|
||||
});
|
||||
@ -106,26 +111,30 @@ describe('ProxySpec', () => {
|
||||
expect(proxyZone.run(fn)).toEqual('works');
|
||||
proxyZoneSpec.setDelegate({
|
||||
name: '.',
|
||||
onInvoke: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
|
||||
delegate: Function, applyThis: any, applyArgs: any[], source: string) => {
|
||||
return `(${
|
||||
onInvoke:
|
||||
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
|
||||
delegate: Function, applyThis: any, applyArgs: any[], source: string) => {
|
||||
return `(${
|
||||
parentZoneDelegate.invoke(targetZone, delegate, applyThis, applyArgs, source)})`;
|
||||
}
|
||||
}
|
||||
});
|
||||
expect(proxyZone.run(fn)).toEqual('(works)');
|
||||
});
|
||||
|
||||
it('should handleError', () => {
|
||||
const error = new Error('TestError');
|
||||
const fn = () => { throw error; };
|
||||
const fn = () => {
|
||||
throw error;
|
||||
};
|
||||
expect(() => proxyZone.run(fn)).toThrow(error);
|
||||
proxyZoneSpec.setDelegate({
|
||||
name: '.',
|
||||
onHandleError: (parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone,
|
||||
error: any): boolean => {
|
||||
expect(error).toEqual(error);
|
||||
return false;
|
||||
}
|
||||
onHandleError:
|
||||
(parentZoneDelegate: ZoneDelegate, currentZone: Zone, targetZone: Zone, error: any):
|
||||
boolean => {
|
||||
expect(error).toEqual(error);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
expect(() => proxyZone.runGuarded(fn)).not.toThrow();
|
||||
});
|
||||
@ -140,7 +149,9 @@ describe('ProxySpec', () => {
|
||||
|
||||
describe('delegateSpec change', () => {
|
||||
let log: string[] = [];
|
||||
beforeEach(() => { log = []; });
|
||||
beforeEach(() => {
|
||||
log = [];
|
||||
});
|
||||
it('should trigger hasTask when invoke', (done: Function) => {
|
||||
const zoneSpec1 = {
|
||||
name: 'zone1',
|
||||
@ -157,13 +168,29 @@ describe('ProxySpec', () => {
|
||||
}
|
||||
};
|
||||
proxyZoneSpec.setDelegate(zoneSpec1);
|
||||
proxyZone.run(() => { setTimeout(() => { log.push('timeout in zoneSpec1'); }, 50); });
|
||||
proxyZone.run(() => {
|
||||
setTimeout(() => {
|
||||
log.push('timeout in zoneSpec1');
|
||||
}, 50);
|
||||
});
|
||||
proxyZoneSpec.setDelegate(zoneSpec2);
|
||||
proxyZone.run(() => { Promise.resolve(1).then(() => { log.push('then in zoneSpec2'); }); });
|
||||
proxyZone.run(() => {
|
||||
Promise.resolve(1).then(() => {
|
||||
log.push('then in zoneSpec2');
|
||||
});
|
||||
});
|
||||
proxyZoneSpec.setDelegate(null);
|
||||
proxyZone.run(() => { setTimeout(() => { log.push('timeout in null spec'); }, 50); });
|
||||
proxyZone.run(() => {
|
||||
setTimeout(() => {
|
||||
log.push('timeout in null spec');
|
||||
}, 50);
|
||||
});
|
||||
proxyZoneSpec.setDelegate(zoneSpec2);
|
||||
proxyZone.run(() => { Promise.resolve(1).then(() => { log.push('then in zoneSpec2'); }); });
|
||||
proxyZone.run(() => {
|
||||
Promise.resolve(1).then(() => {
|
||||
log.push('then in zoneSpec2');
|
||||
});
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
expect(log).toEqual([
|
||||
|
@ -41,7 +41,9 @@ describe('SyncTestZoneSpec', () => {
|
||||
document.body.appendChild(button);
|
||||
let x = 1;
|
||||
try {
|
||||
button.addEventListener('click', () => { x++; });
|
||||
button.addEventListener('click', () => {
|
||||
x++;
|
||||
});
|
||||
|
||||
button.click();
|
||||
expect(x).toEqual(2);
|
||||
|
@ -23,19 +23,19 @@ describe('TaskTrackingZone', function() {
|
||||
it('should track tasks', (done: Function) => {
|
||||
taskTrackingZone.run(() => {
|
||||
taskTrackingZone.scheduleMicroTask('test1', () => {});
|
||||
expect(taskTrackingZoneSpec !.microTasks.length).toBe(1);
|
||||
expect(taskTrackingZoneSpec !.microTasks[0].source).toBe('test1');
|
||||
expect(taskTrackingZoneSpec!.microTasks.length).toBe(1);
|
||||
expect(taskTrackingZoneSpec!.microTasks[0].source).toBe('test1');
|
||||
|
||||
setTimeout(() => {});
|
||||
expect(taskTrackingZoneSpec !.macroTasks.length).toBe(1);
|
||||
expect(taskTrackingZoneSpec !.macroTasks[0].source).toBe('setTimeout');
|
||||
taskTrackingZone.cancelTask(taskTrackingZoneSpec !.macroTasks[0]);
|
||||
expect(taskTrackingZoneSpec !.macroTasks.length).toBe(0);
|
||||
expect(taskTrackingZoneSpec!.macroTasks.length).toBe(1);
|
||||
expect(taskTrackingZoneSpec!.macroTasks[0].source).toBe('setTimeout');
|
||||
taskTrackingZone.cancelTask(taskTrackingZoneSpec!.macroTasks[0]);
|
||||
expect(taskTrackingZoneSpec!.macroTasks.length).toBe(0);
|
||||
|
||||
setTimeout(() => {
|
||||
// assert on execution it is null
|
||||
expect(taskTrackingZoneSpec !.macroTasks.length).toBe(0);
|
||||
expect(taskTrackingZoneSpec !.microTasks.length).toBe(0);
|
||||
expect(taskTrackingZoneSpec!.macroTasks.length).toBe(0);
|
||||
expect(taskTrackingZoneSpec!.microTasks.length).toBe(0);
|
||||
|
||||
// If a browser does not have XMLHttpRequest, then end test here.
|
||||
if (typeof global['XMLHttpRequest'] == 'undefined') return done();
|
||||
@ -45,22 +45,22 @@ describe('TaskTrackingZone', function() {
|
||||
if (xhr.readyState == 4) {
|
||||
// clear current event tasks using setTimeout
|
||||
setTimeout(() => {
|
||||
expect(taskTrackingZoneSpec !.macroTasks.length).toBe(0);
|
||||
expect(taskTrackingZoneSpec !.microTasks.length).toBe(0);
|
||||
expect(taskTrackingZoneSpec!.macroTasks.length).toBe(0);
|
||||
expect(taskTrackingZoneSpec!.microTasks.length).toBe(0);
|
||||
if (supportPatchXHROnProperty()) {
|
||||
expect(taskTrackingZoneSpec !.eventTasks.length).not.toBe(0);
|
||||
expect(taskTrackingZoneSpec!.eventTasks.length).not.toBe(0);
|
||||
}
|
||||
taskTrackingZoneSpec !.clearEvents();
|
||||
expect(taskTrackingZoneSpec !.eventTasks.length).toBe(0);
|
||||
taskTrackingZoneSpec!.clearEvents();
|
||||
expect(taskTrackingZoneSpec!.eventTasks.length).toBe(0);
|
||||
done();
|
||||
});
|
||||
}
|
||||
};
|
||||
xhr.send();
|
||||
expect(taskTrackingZoneSpec !.macroTasks.length).toBe(1);
|
||||
expect(taskTrackingZoneSpec !.macroTasks[0].source).toBe('XMLHttpRequest.send');
|
||||
expect(taskTrackingZoneSpec!.macroTasks.length).toBe(1);
|
||||
expect(taskTrackingZoneSpec!.macroTasks[0].source).toBe('XMLHttpRequest.send');
|
||||
if (supportPatchXHROnProperty()) {
|
||||
expect(taskTrackingZoneSpec !.eventTasks[0].source)
|
||||
expect(taskTrackingZoneSpec!.eventTasks[0].source)
|
||||
.toMatch(/\.addEventListener:readystatechange/);
|
||||
}
|
||||
});
|
||||
@ -69,8 +69,10 @@ describe('TaskTrackingZone', function() {
|
||||
|
||||
it('should capture task creation stacktrace', (done) => {
|
||||
taskTrackingZone.run(() => {
|
||||
setTimeout(() => { done(); });
|
||||
expect((taskTrackingZoneSpec !.macroTasks[0] as any)['creationLocation']).toBeTruthy();
|
||||
setTimeout(() => {
|
||||
done();
|
||||
});
|
||||
expect((taskTrackingZoneSpec!.macroTasks[0] as any)['creationLocation']).toBeTruthy();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user