2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-06-23 12:47:54 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2017-05-22 14:19:21 -04:00
|
|
|
import {discardPeriodicTasks, fakeAsync, flush, flushMicrotasks, tick} from '@angular/core/testing';
|
2020-04-13 19:40:21 -04:00
|
|
|
import {beforeEach, describe, inject, it, Log} from '@angular/core/testing/src/testing_internal';
|
2019-01-25 19:00:50 -05:00
|
|
|
import {EventManager} from '@angular/platform-browser';
|
2017-03-02 15:12:46 -05:00
|
|
|
import {expect} from '@angular/platform-browser/testing/src/matchers';
|
2017-12-15 19:28:41 -05:00
|
|
|
|
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
|
|
|
|
2017-12-15 19:28:41 -05:00
|
|
|
{
|
2018-11-29 17:34:28 -05:00
|
|
|
describe('fake async', () => {
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should run synchronous code', () => {
|
2016-11-12 08:08:58 -05:00
|
|
|
let ran = false;
|
2020-04-13 19:40:21 -04:00
|
|
|
fakeAsync(() => {
|
|
|
|
ran = true;
|
|
|
|
})();
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
expect(ran).toEqual(true);
|
|
|
|
});
|
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
|
2019-01-25 19:00:50 -05:00
|
|
|
it('should work with inject()',
|
|
|
|
fakeAsync(inject([EventManager], (eventManager: EventManager) => {
|
|
|
|
expect(eventManager).toBeAnInstanceOf(EventManager);
|
|
|
|
})));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should throw on nested calls', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
2020-04-13 19:40:21 -04:00
|
|
|
fakeAsync(() => {
|
|
|
|
fakeAsync((): any /** TODO #9100 */ => null)();
|
|
|
|
})();
|
2016-06-08 19:38:52 -04:00
|
|
|
}).toThrowError('fakeAsync() calls can not be nested');
|
2015-05-19 03:13:17 -04:00
|
|
|
});
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05: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
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
fakeAsync(() => {
|
|
|
|
resolvedPromise.then(_ => {
|
|
|
|
thenRan = true;
|
|
|
|
});
|
|
|
|
})();
|
2015-06-02 13:55:03 -04:00
|
|
|
|
|
|
|
expect(thenRan).toEqual(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('should propagate the return value', () => {
|
|
|
|
expect(fakeAsync(() => 'foo')()).toEqual('foo');
|
|
|
|
});
|
2015-06-02 13:55:03 -04:00
|
|
|
|
2015-05-27 12:53:37 -04:00
|
|
|
describe('Promise', () => {
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should run asynchronous code', fakeAsync(() => {
|
|
|
|
let thenRan = false;
|
2020-04-13 19:40:21 -04:00
|
|
|
resolvedPromise.then((_) => {
|
|
|
|
thenRan = true;
|
|
|
|
});
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
expect(thenRan).toEqual(false);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
flushMicrotasks();
|
|
|
|
expect(thenRan).toEqual(true);
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should run chained thens', fakeAsync(() => {
|
|
|
|
const log = new Log();
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
resolvedPromise.then((_) => log.add(1)).then((_) => log.add(2));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
expect(log.result()).toEqual('');
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
flushMicrotasks();
|
|
|
|
expect(log.result()).toEqual('1; 2');
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should run Promise created in Promise', fakeAsync(() => {
|
|
|
|
const log = new Log();
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
resolvedPromise.then((_) => {
|
|
|
|
log.add(1);
|
|
|
|
resolvedPromise.then((_) => log.add(2));
|
|
|
|
});
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
expect(log.result()).toEqual('');
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
flushMicrotasks();
|
|
|
|
expect(log.result()).toEqual('1; 2');
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should complain if the test throws an exception during async calls', () => {
|
|
|
|
expect(() => {
|
|
|
|
fakeAsync(() => {
|
2020-04-13 19:40:21 -04:00
|
|
|
resolvedPromise.then((_) => {
|
|
|
|
throw new Error('async');
|
|
|
|
});
|
2018-11-30 11:14:53 -05:00
|
|
|
flushMicrotasks();
|
|
|
|
})();
|
2019-05-17 07:50:02 -04:00
|
|
|
}).toThrow();
|
2018-11-30 11:14:53 -05:00
|
|
|
});
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should complain if a test throws an exception', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(() => {
|
|
|
|
fakeAsync(() => {
|
|
|
|
throw new Error('sync');
|
|
|
|
})();
|
|
|
|
}).toThrowError('sync');
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('timers', () => {
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should run queued zero duration timer on zero tick', fakeAsync(() => {
|
|
|
|
let ran = false;
|
2020-04-13 19:40:21 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
ran = true;
|
|
|
|
}, 0);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
expect(ran).toEqual(false);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick();
|
|
|
|
expect(ran).toEqual(true);
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should run queued timer after sufficient clock ticks', fakeAsync(() => {
|
|
|
|
let ran = false;
|
2020-04-13 19:40:21 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
ran = true;
|
|
|
|
}, 10);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(6);
|
|
|
|
expect(ran).toEqual(false);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(6);
|
|
|
|
expect(ran).toEqual(true);
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2019-12-02 10:56:56 -05:00
|
|
|
it('should run new macro tasks created by timer callback', fakeAsync(() => {
|
|
|
|
function nestedTimer(callback: () => any): void {
|
|
|
|
setTimeout(() => setTimeout(() => callback()));
|
|
|
|
}
|
|
|
|
const callback = jasmine.createSpy('callback');
|
|
|
|
nestedTimer(callback);
|
|
|
|
expect(callback).not.toHaveBeenCalled();
|
|
|
|
tick(0);
|
|
|
|
expect(callback).toHaveBeenCalled();
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should not queue nested timer on tick with processNewMacroTasksSynchronously=false',
|
|
|
|
fakeAsync(() => {
|
|
|
|
function nestedTimer(callback: () => any): void {
|
|
|
|
setTimeout(() => setTimeout(() => callback()));
|
|
|
|
}
|
|
|
|
const callback = jasmine.createSpy('callback');
|
|
|
|
nestedTimer(callback);
|
|
|
|
expect(callback).not.toHaveBeenCalled();
|
|
|
|
tick(0, {processNewMacroTasksSynchronously: false});
|
|
|
|
expect(callback).not.toHaveBeenCalled();
|
|
|
|
flush();
|
|
|
|
expect(callback).toHaveBeenCalled();
|
|
|
|
}));
|
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should run queued timer only once', fakeAsync(() => {
|
|
|
|
let cycles = 0;
|
2020-04-13 19:40:21 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
cycles++;
|
|
|
|
}, 10);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should not run cancelled timer', fakeAsync(() => {
|
|
|
|
let ran = false;
|
2020-04-13 19:40:21 -04:00
|
|
|
const id = setTimeout(() => {
|
|
|
|
ran = true;
|
|
|
|
}, 10);
|
2018-11-30 11:14:53 -05:00
|
|
|
clearTimeout(id);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(ran).toEqual(false);
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should throw an error on dangling timers', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
2020-04-13 19:40:21 -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
|
|
|
});
|
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should throw an error on dangling periodic timers', () => {
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(() => {
|
2020-04-13 19:40:21 -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
|
|
|
});
|
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should run periodic timers', fakeAsync(() => {
|
|
|
|
let cycles = 0;
|
2020-04-13 19:40:21 -04:00
|
|
|
const id = setInterval(() => {
|
|
|
|
cycles++;
|
|
|
|
}, 10);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(2);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(3);
|
|
|
|
clearInterval(id);
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should not run cancelled periodic timer', fakeAsync(() => {
|
|
|
|
let ran = false;
|
2020-04-13 19:40:21 -04:00
|
|
|
const id = setInterval(() => {
|
|
|
|
ran = true;
|
|
|
|
}, 10);
|
2018-11-30 11:14:53 -05:00
|
|
|
clearInterval(id);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(ran).toEqual(false);
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should be able to cancel periodic timers from a callback', fakeAsync(() => {
|
|
|
|
let cycles = 0;
|
|
|
|
let id: any /** TODO #9100 */;
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
id = setInterval(() => {
|
|
|
|
cycles++;
|
|
|
|
clearInterval(id);
|
|
|
|
}, 10);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should clear periodic timers', fakeAsync(() => {
|
|
|
|
let cycles = 0;
|
2020-04-13 19:40:21 -04:00
|
|
|
const id = setInterval(() => {
|
|
|
|
cycles++;
|
|
|
|
}, 10);
|
2016-05-26 13:19:30 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(cycles).toEqual(1);
|
2016-05-26 13:19:30 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
discardPeriodicTasks();
|
2016-05-26 13:19:30 -04:00
|
|
|
|
2018-11-30 11:14:53 -05: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
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
// Nothing should change
|
|
|
|
expect(cycles).toEqual(2);
|
|
|
|
}));
|
2016-05-26 13:19:30 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should process microtasks before timers', fakeAsync(() => {
|
|
|
|
const log = new Log();
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
resolvedPromise.then((_) => log.add('microtask'));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
setTimeout(() => log.add('timer'), 9);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
const id = setInterval(() => log.add('periodic timer'), 10);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
expect(log.result()).toEqual('');
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(log.result()).toEqual('microtask; timer; periodic timer');
|
|
|
|
clearInterval(id);
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should process micro-tasks created in timers before next timers', fakeAsync(() => {
|
|
|
|
const log = new Log();
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
resolvedPromise.then((_) => log.add('microtask'));
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
log.add('timer');
|
|
|
|
resolvedPromise.then((_) => log.add('t microtask'));
|
|
|
|
}, 9);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
const id = setInterval(() => {
|
|
|
|
log.add('periodic timer');
|
|
|
|
resolvedPromise.then((_) => log.add('pt microtask'));
|
|
|
|
}, 10);
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(log.result())
|
|
|
|
.toEqual('microtask; timer; t microtask; periodic timer; pt microtask');
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
tick(10);
|
|
|
|
expect(log.result())
|
|
|
|
.toEqual(
|
|
|
|
'microtask; timer; t microtask; periodic timer; pt microtask; periodic timer; pt microtask');
|
|
|
|
clearInterval(id);
|
|
|
|
}));
|
2017-05-22 14:19:21 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should flush tasks', fakeAsync(() => {
|
|
|
|
let ran = false;
|
2020-04-13 19:40:21 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
ran = true;
|
|
|
|
}, 10);
|
2017-05-22 14:19:21 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
flush();
|
|
|
|
expect(ran).toEqual(true);
|
|
|
|
}));
|
2017-05-22 14:19:21 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should flush multiple tasks', fakeAsync(() => {
|
|
|
|
let ran = false;
|
|
|
|
let ran2 = false;
|
2020-04-13 19:40:21 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
ran = true;
|
|
|
|
}, 10);
|
|
|
|
setTimeout(() => {
|
|
|
|
ran2 = true;
|
|
|
|
}, 30);
|
2017-05-22 14:19:21 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
let elapsed = flush();
|
2017-05-22 14:19:21 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
expect(ran).toEqual(true);
|
|
|
|
expect(ran2).toEqual(true);
|
|
|
|
expect(elapsed).toEqual(30);
|
|
|
|
}));
|
2017-05-22 14:19:21 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should move periodic tasks', fakeAsync(() => {
|
|
|
|
let ran = false;
|
|
|
|
let count = 0;
|
2020-04-13 19:40:21 -04:00
|
|
|
setInterval(() => {
|
|
|
|
count++;
|
|
|
|
}, 10);
|
|
|
|
setTimeout(() => {
|
|
|
|
ran = true;
|
|
|
|
}, 35);
|
2017-05-22 14:19:21 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
let elapsed = flush();
|
2017-05-22 14:19:21 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
expect(count).toEqual(3);
|
|
|
|
expect(ran).toEqual(true);
|
|
|
|
expect(elapsed).toEqual(35);
|
2017-05-22 14:19:21 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
discardPeriodicTasks();
|
|
|
|
}));
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('outside of the fakeAsync zone', () => {
|
2018-11-30 11:14:53 -05:00
|
|
|
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
|
|
|
});
|
|
|
|
|
2018-11-30 11:14:53 -05: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
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('calling flush should throw', () => {
|
2017-05-22 14:19:21 -04:00
|
|
|
expect(() => {
|
|
|
|
flush();
|
|
|
|
}).toThrowError('The code should be running in the fakeAsync zone to call this function');
|
|
|
|
});
|
|
|
|
|
2018-11-30 11:14:53 -05: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;
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(fakeAsync(() => {
|
|
|
|
zoneInBeforeEach = Zone.current;
|
|
|
|
}));
|
2016-08-02 07:45:15 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
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', () => {
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
ProxyZoneSpec.assertPresent();
|
|
|
|
});
|
2016-08-19 15:10:53 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
afterEach(() => {
|
|
|
|
ProxyZoneSpec.assertPresent();
|
|
|
|
});
|
2016-08-19 15:10:53 -04:00
|
|
|
|
2018-11-30 11:14:53 -05:00
|
|
|
it('should allow fakeAsync zone to retroactively set a zoneSpec outside of fakeAsync', () => {
|
|
|
|
ProxyZoneSpec.assertPresent();
|
|
|
|
let state: string = 'not run';
|
|
|
|
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');
|
|
|
|
});
|
2015-05-27 12:53:37 -04:00
|
|
|
});
|
|
|
|
}
|