@ -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();
|
||||
|
Reference in New Issue
Block a user