2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
import {discardPeriodicTasks, fakeAsync, flushMicrotasks, tick} from '@angular/core/testing';
|
2016-09-27 20:12:25 -04:00
|
|
|
import {Log, beforeEach, describe, inject, it} from '@angular/core/testing/testing_internal';
|
2016-07-22 12:20:51 -04:00
|
|
|
import {expect} from '@angular/platform-browser/testing/matchers';
|
2016-08-02 18:53:34 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {Parser} from '../../compiler/src/expression_parser/parser';
|
2016-08-02 18:53:34 -04:00
|
|
|
|
|
|
|
const resolvedPromise = Promise.resolve(null);
|
2016-08-19 15:10:53 -04:00
|
|
|
const ProxyZoneSpec: {assertPresent: () => void} = (Zone as any)['ProxyZoneSpec'];
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('fake async', () => {
|
|
|
|
it('should run synchronous code', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let ran = false;
|
2015-05-27 12:53:37 -04:00
|
|
|
fakeAsync(() => { ran = true; })();
|
|
|
|
|
|
|
|
expect(ran).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should pass arguments to the wrapped function', () => {
|
2016-06-08 18:45:15 -04:00
|
|
|
fakeAsync((foo: any /** TODO #9100 */, bar: any /** TODO #9100 */) => {
|
2015-05-27 12:53:37 -04:00
|
|
|
expect(foo).toEqual('foo');
|
|
|
|
expect(bar).toEqual('bar');
|
|
|
|
})('foo', 'bar');
|
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should work with inject()', fakeAsync(inject([Parser], (parser: any /** TODO #9100 */) => {
|
|
|
|
expect(parser).toBeAnInstanceOf(Parser);
|
|
|
|
})));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2015-05-19 03:13:17 -04:00
|
|
|
it('should throw on nested calls', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
|
|
|
fakeAsync(() => { fakeAsync((): any /** TODO #9100 */ => null)(); })();
|
|
|
|
}).toThrowError('fakeAsync() calls can not be nested');
|
2015-05-19 03:13:17 -04:00
|
|
|
});
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2015-06-02 13:55:03 -04:00
|
|
|
it('should flush microtasks before returning', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let thenRan = false;
|
2015-06-02 13:55:03 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
fakeAsync(() => { resolvedPromise.then(_ => { thenRan = true; }); })();
|
2015-06-02 13:55:03 -04:00
|
|
|
|
|
|
|
expect(thenRan).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should propagate the return value',
|
|
|
|
() => { expect(fakeAsync(() => 'foo')()).toEqual('foo'); });
|
|
|
|
|
2015-05-27 12:53:37 -04:00
|
|
|
describe('Promise', () => {
|
|
|
|
it('should run asynchronous code', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let thenRan = false;
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then((_) => { thenRan = true; });
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
expect(thenRan).toEqual(false);
|
|
|
|
|
|
|
|
flushMicrotasks();
|
|
|
|
expect(thenRan).toEqual(true);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should run chained thens', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const log = new Log();
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then((_) => log.add(1)).then((_) => log.add(2));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
expect(log.result()).toEqual('');
|
|
|
|
|
|
|
|
flushMicrotasks();
|
|
|
|
expect(log.result()).toEqual('1; 2');
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should run Promise created in Promise', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const log = new Log();
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then((_) => {
|
2015-05-27 12:53:37 -04:00
|
|
|
log.add(1);
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then((_) => log.add(2));
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(log.result()).toEqual('');
|
|
|
|
|
|
|
|
flushMicrotasks();
|
|
|
|
expect(log.result()).toEqual('1; 2');
|
|
|
|
}));
|
|
|
|
|
2016-07-18 06:50:31 -04:00
|
|
|
it('should complain if the test throws an exception during async calls', () => {
|
2015-05-27 12:53:37 -04:00
|
|
|
expect(() => {
|
|
|
|
fakeAsync(() => {
|
2016-08-25 03:50:16 -04:00
|
|
|
resolvedPromise.then((_) => { throw new Error('async'); });
|
2015-05-27 12:53:37 -04:00
|
|
|
flushMicrotasks();
|
|
|
|
})();
|
2016-11-22 16:51:37 -05:00
|
|
|
}).toThrowError(/Uncaught \(in promise\): Error: async/);
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should complain if a test throws an exception', () => {
|
2016-08-25 03:50:16 -04:00
|
|
|
expect(() => { fakeAsync(() => { throw new Error('sync'); })(); }).toThrowError('sync');
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('timers', () => {
|
|
|
|
it('should run queued zero duration timer on zero tick', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let ran = false;
|
2016-08-02 18:53:34 -04:00
|
|
|
setTimeout(() => { ran = true; }, 0);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
expect(ran).toEqual(false);
|
|
|
|
|
|
|
|
tick();
|
|
|
|
expect(ran).toEqual(true);
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
it('should run queued timer after sufficient clock ticks', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let ran = false;
|
2016-08-02 18:53:34 -04:00
|
|
|
setTimeout(() => { ran = true; }, 10);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
tick(6);
|
|
|
|
expect(ran).toEqual(false);
|
|
|
|
|
|
|
|
tick(6);
|
|
|
|
expect(ran).toEqual(true);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should run queued timer only once', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let cycles = 0;
|
2016-08-02 18:53:34 -04:00
|
|
|
setTimeout(() => { cycles++; }, 10);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should not run cancelled timer', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let ran = false;
|
|
|
|
const id = setTimeout(() => { ran = true; }, 10);
|
2016-08-02 18:53:34 -04:00
|
|
|
clearTimeout(id);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(ran).toEqual(false);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should throw an error on dangling timers', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
2016-08-02 18:53:34 -04:00
|
|
|
fakeAsync(() => { setTimeout(() => {}, 10); })();
|
2016-06-08 19:38:52 -04:00
|
|
|
}).toThrowError('1 timer(s) still in the queue.');
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error on dangling periodic timers', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
2016-08-02 18:53:34 -04:00
|
|
|
fakeAsync(() => { setInterval(() => {}, 10); })();
|
2016-06-08 19:38:52 -04:00
|
|
|
}).toThrowError('1 periodic timer(s) still in the queue.');
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should run periodic timers', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let cycles = 0;
|
|
|
|
const id = setInterval(() => { cycles++; }, 10);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(2);
|
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(3);
|
2016-08-02 18:53:34 -04:00
|
|
|
clearInterval(id);
|
2015-05-27 12:53:37 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('should not run cancelled periodic timer', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let ran = false;
|
|
|
|
const id = setInterval(() => { ran = true; }, 10);
|
2016-08-02 18:53:34 -04:00
|
|
|
clearInterval(id);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(ran).toEqual(false);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should be able to cancel periodic timers from a callback', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let cycles = 0;
|
|
|
|
let id: any /** TODO #9100 */;
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
id = setInterval(() => {
|
2015-05-27 12:53:37 -04:00
|
|
|
cycles++;
|
2016-08-02 18:53:34 -04:00
|
|
|
clearInterval(id);
|
2015-05-27 12:53:37 -04:00
|
|
|
}, 10);
|
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
|
|
|
}));
|
|
|
|
|
2016-05-26 13:19:30 -04:00
|
|
|
it('should clear periodic timers', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let cycles = 0;
|
|
|
|
const id = setInterval(() => { cycles++; }, 10);
|
2016-05-26 13:19:30 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
2016-05-26 13:19:30 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
discardPeriodicTasks();
|
2016-05-26 13:19:30 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
// Tick once to clear out the timer which already started.
|
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(2);
|
2016-05-26 13:19:30 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
tick(10);
|
|
|
|
// Nothing should change
|
|
|
|
expect(cycles).toEqual(2);
|
|
|
|
}));
|
2016-05-26 13:19:30 -04:00
|
|
|
|
2015-05-27 12:53:37 -04:00
|
|
|
it('should process microtasks before timers', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const log = new Log();
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then((_) => log.add('microtask'));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
setTimeout(() => log.add('timer'), 9);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const id = setInterval(() => log.add('periodic timer'), 10);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
expect(log.result()).toEqual('');
|
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(log.result()).toEqual('microtask; timer; periodic timer');
|
2016-08-02 18:53:34 -04:00
|
|
|
clearInterval(id);
|
2015-05-27 12:53:37 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('should process micro-tasks created in timers before next timers', fakeAsync(() => {
|
2016-11-12 08:08:58 -05:00
|
|
|
const log = new Log();
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then((_) => log.add('microtask'));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2016-08-02 18:53:34 -04:00
|
|
|
setTimeout(() => {
|
2015-05-27 12:53:37 -04:00
|
|
|
log.add('timer');
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then((_) => log.add('t microtask'));
|
2015-05-27 12:53:37 -04:00
|
|
|
}, 9);
|
|
|
|
|
2016-11-12 08:08:58 -05:00
|
|
|
const id = setInterval(() => {
|
2015-05-27 12:53:37 -04:00
|
|
|
log.add('periodic timer');
|
2016-08-02 18:53:34 -04:00
|
|
|
resolvedPromise.then((_) => log.add('pt microtask'));
|
2015-05-27 12:53:37 -04:00
|
|
|
}, 10);
|
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(log.result())
|
|
|
|
.toEqual('microtask; timer; t microtask; periodic timer; pt microtask');
|
|
|
|
|
|
|
|
tick(10);
|
|
|
|
expect(log.result())
|
|
|
|
.toEqual(
|
|
|
|
'microtask; timer; t microtask; periodic timer; pt microtask; periodic timer; pt microtask');
|
2016-08-02 18:53:34 -04:00
|
|
|
clearInterval(id);
|
2015-05-27 12:53:37 -04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('outside of the fakeAsync zone', () => {
|
|
|
|
it('calling flushMicrotasks should throw', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
|
|
|
flushMicrotasks();
|
|
|
|
}).toThrowError('The code should be running in the fakeAsync zone to call this function');
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('calling tick should throw', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
|
|
|
tick();
|
|
|
|
}).toThrowError('The code should be running in the fakeAsync zone to call this function');
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
2016-05-26 13:19:30 -04:00
|
|
|
|
|
|
|
it('calling discardPeriodicTasks should throw', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
|
|
|
discardPeriodicTasks();
|
|
|
|
}).toThrowError('The code should be running in the fakeAsync zone to call this function');
|
2016-05-26 13:19:30 -04:00
|
|
|
});
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
2016-08-02 07:45:15 -04:00
|
|
|
|
|
|
|
describe('only one `fakeAsync` zone per test', () => {
|
|
|
|
let zoneInBeforeEach: Zone;
|
|
|
|
let zoneInTest1: Zone;
|
|
|
|
beforeEach(fakeAsync(() => { zoneInBeforeEach = Zone.current; }));
|
|
|
|
|
|
|
|
it('should use the same zone as in beforeEach', fakeAsync(() => {
|
|
|
|
zoneInTest1 = Zone.current;
|
|
|
|
expect(zoneInTest1).toBe(zoneInBeforeEach);
|
|
|
|
}));
|
2016-08-19 15:10:53 -04:00
|
|
|
});
|
|
|
|
});
|
2016-08-02 07:45:15 -04:00
|
|
|
|
2016-08-19 15:10:53 -04:00
|
|
|
describe('ProxyZone', () => {
|
|
|
|
beforeEach(() => { ProxyZoneSpec.assertPresent(); });
|
|
|
|
|
|
|
|
afterEach(() => { ProxyZoneSpec.assertPresent(); });
|
|
|
|
|
|
|
|
it('should allow fakeAsync zone to retroactively set a zoneSpec outside of fakeAsync', () => {
|
|
|
|
ProxyZoneSpec.assertPresent();
|
2016-11-12 08:08:58 -05:00
|
|
|
let state: string = 'not run';
|
2016-08-19 15:10:53 -04:00
|
|
|
const testZone = Zone.current.fork({name: 'test-zone'});
|
|
|
|
(fakeAsync(() => {
|
|
|
|
testZone.run(() => {
|
|
|
|
Promise.resolve('works').then((v) => state = v);
|
|
|
|
expect(state).toEqual('not run');
|
|
|
|
flushMicrotasks();
|
|
|
|
expect(state).toEqual('works');
|
|
|
|
});
|
|
|
|
}))();
|
|
|
|
expect(state).toEqual('works');
|
2016-08-02 07:45:15 -04:00
|
|
|
});
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
|
|
|
}
|