| 
									
										
										
										
											2016-06-23 09:47:54 -07: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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 11:19:21 -07:00
										 |  |  | import {discardPeriodicTasks, fakeAsync, flush, flushMicrotasks, tick} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2017-03-02 12:12:46 -08:00
										 |  |  | import {Log, beforeEach, describe, inject, it} from '@angular/core/testing/src/testing_internal'; | 
					
						
							|  |  |  | import {expect} from '@angular/platform-browser/testing/src/matchers'; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {Parser} from '../../compiler/src/expression_parser/parser'; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-15 16:28:41 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | const resolvedPromise = Promise.resolve(null); | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07:00
										 |  |  | const ProxyZoneSpec: {assertPresent: () => void} = (Zone as any)['ProxyZoneSpec']; | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-15 16:28:41 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |   describe('fake async', () => { | 
					
						
							|  |  |  |     it('should run synchronous code', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       let ran = false; | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |       fakeAsync(() => { ran = true; })(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(ran).toEqual(true); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should pass arguments to the wrapped function', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |       fakeAsync((foo: any /** TODO #9100 */, bar: any /** TODO #9100 */) => { | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |         expect(foo).toEqual('foo'); | 
					
						
							|  |  |  |         expect(bar).toEqual('bar'); | 
					
						
							|  |  |  |       })('foo', 'bar'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should work with inject()', fakeAsync(inject([Parser], (parser: any /** TODO #9100 */) => { | 
					
						
							|  |  |  |          expect(parser).toBeAnInstanceOf(Parser); | 
					
						
							|  |  |  |        }))); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-19 09:13:17 +02:00
										 |  |  |     it('should throw on nested calls', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       expect(() => { | 
					
						
							|  |  |  |         fakeAsync(() => { fakeAsync((): any /** TODO #9100 */ => null)(); })(); | 
					
						
							|  |  |  |       }).toThrowError('fakeAsync() calls can not be nested'); | 
					
						
							| 
									
										
										
										
											2015-05-19 09:13:17 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 19:55:03 +02:00
										 |  |  |     it('should flush microtasks before returning', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |       let thenRan = false; | 
					
						
							| 
									
										
										
										
											2015-06-02 19:55:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |       fakeAsync(() => { resolvedPromise.then(_ => { thenRan = true; }); })(); | 
					
						
							| 
									
										
										
										
											2015-06-02 19:55:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(thenRan).toEqual(true); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should propagate the return value', | 
					
						
							|  |  |  |        () => { expect(fakeAsync(() => 'foo')()).toEqual('foo'); }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |     describe('Promise', () => { | 
					
						
							|  |  |  |       it('should run asynchronous code', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            let thenRan = false; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            resolvedPromise.then((_) => { thenRan = true; }); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            expect(thenRan).toEqual(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            flushMicrotasks(); | 
					
						
							|  |  |  |            expect(thenRan).toEqual(true); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should run chained thens', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const log = new Log(); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            resolvedPromise.then((_) => log.add(1)).then((_) => log.add(2)); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            expect(log.result()).toEqual(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            flushMicrotasks(); | 
					
						
							|  |  |  |            expect(log.result()).toEqual('1; 2'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should run Promise created in Promise', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const log = new Log(); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            resolvedPromise.then((_) => { | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |              log.add(1); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |              resolvedPromise.then((_) => log.add(2)); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |            }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            expect(log.result()).toEqual(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            flushMicrotasks(); | 
					
						
							|  |  |  |            expect(log.result()).toEqual('1; 2'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |       it('should complain if the test throws an exception during async calls', () => { | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |         expect(() => { | 
					
						
							|  |  |  |           fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |             resolvedPromise.then((_) => { throw new Error('async'); }); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |             flushMicrotasks(); | 
					
						
							|  |  |  |           })(); | 
					
						
							| 
									
										
										
										
											2016-11-22 13:51:37 -08:00
										 |  |  |         }).toThrowError(/Uncaught \(in promise\): Error: async/); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should complain if a test throws an exception', () => { | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |         expect(() => { fakeAsync(() => { throw new Error('sync'); })(); }).toThrowError('sync'); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('timers', () => { | 
					
						
							|  |  |  |       it('should run queued zero duration timer on zero tick', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            let ran = false; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            setTimeout(() => { ran = true; }, 0); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            expect(ran).toEqual(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tick(); | 
					
						
							|  |  |  |            expect(ran).toEqual(true); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should run queued timer after sufficient clock ticks', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            let ran = false; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            setTimeout(() => { ran = true; }, 10); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            tick(6); | 
					
						
							|  |  |  |            expect(ran).toEqual(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tick(6); | 
					
						
							|  |  |  |            expect(ran).toEqual(true); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should run queued timer only once', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            let cycles = 0; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            setTimeout(() => { cycles++; }, 10); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07: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 14:08:58 +01:00
										 |  |  |            let ran = false; | 
					
						
							|  |  |  |            const id = setTimeout(() => { ran = true; }, 10); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            clearTimeout(id); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            tick(10); | 
					
						
							|  |  |  |            expect(ran).toEqual(false); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should throw an error on dangling timers', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         expect(() => { | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |           fakeAsync(() => { setTimeout(() => {}, 10); })(); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         }).toThrowError('1 timer(s) still in the queue.'); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should throw an error on dangling periodic timers', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         expect(() => { | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |           fakeAsync(() => { setInterval(() => {}, 10); })(); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         }).toThrowError('1 periodic timer(s) still in the queue.'); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should run periodic timers', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            let cycles = 0; | 
					
						
							|  |  |  |            const id = setInterval(() => { cycles++; }, 10); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            tick(10); | 
					
						
							|  |  |  |            expect(cycles).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tick(10); | 
					
						
							|  |  |  |            expect(cycles).toEqual(2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tick(10); | 
					
						
							|  |  |  |            expect(cycles).toEqual(3); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            clearInterval(id); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not run cancelled periodic timer', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            let ran = false; | 
					
						
							|  |  |  |            const id = setInterval(() => { ran = true; }, 10); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            clearInterval(id); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            tick(10); | 
					
						
							|  |  |  |            expect(ran).toEqual(false); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should be able to cancel periodic timers from a callback', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            let cycles = 0; | 
					
						
							|  |  |  |            let id: any /** TODO #9100 */; | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            id = setInterval(() => { | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |              cycles++; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |              clearInterval(id); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |            }, 10); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tick(10); | 
					
						
							|  |  |  |            expect(cycles).toEqual(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tick(10); | 
					
						
							|  |  |  |            expect(cycles).toEqual(1); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  |       it('should clear periodic timers', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            let cycles = 0; | 
					
						
							|  |  |  |            const id = setInterval(() => { cycles++; }, 10); | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            tick(10); | 
					
						
							|  |  |  |            expect(cycles).toEqual(1); | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            discardPeriodicTasks(); | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            // Tick once to clear out the timer which already started.
 | 
					
						
							|  |  |  |            tick(10); | 
					
						
							|  |  |  |            expect(cycles).toEqual(2); | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            tick(10); | 
					
						
							|  |  |  |            // Nothing should change
 | 
					
						
							|  |  |  |            expect(cycles).toEqual(2); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |       it('should process microtasks before timers', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const log = new Log(); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            resolvedPromise.then((_) => log.add('microtask')); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            setTimeout(() => log.add('timer'), 9); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const id = setInterval(() => log.add('periodic timer'), 10); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            expect(log.result()).toEqual(''); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tick(10); | 
					
						
							|  |  |  |            expect(log.result()).toEqual('microtask; timer; periodic timer'); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            clearInterval(id); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should process micro-tasks created in timers before next timers', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const log = new Log(); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            resolvedPromise.then((_) => log.add('microtask')); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |            setTimeout(() => { | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |              log.add('timer'); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |              resolvedPromise.then((_) => log.add('t microtask')); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |            }, 9); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |            const id = setInterval(() => { | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |              log.add('periodic timer'); | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  |              resolvedPromise.then((_) => log.add('pt microtask')); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07: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 15:53:34 -07:00
										 |  |  |            clearInterval(id); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2017-05-22 11:19:21 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should flush tasks', fakeAsync(() => { | 
					
						
							|  |  |  |            let ran = false; | 
					
						
							|  |  |  |            setTimeout(() => { ran = true; }, 10); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            flush(); | 
					
						
							|  |  |  |            expect(ran).toEqual(true); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should flush multiple tasks', fakeAsync(() => { | 
					
						
							|  |  |  |            let ran = false; | 
					
						
							|  |  |  |            let ran2 = false; | 
					
						
							|  |  |  |            setTimeout(() => { ran = true; }, 10); | 
					
						
							|  |  |  |            setTimeout(() => { ran2 = true; }, 30); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            let elapsed = flush(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            expect(ran).toEqual(true); | 
					
						
							|  |  |  |            expect(ran2).toEqual(true); | 
					
						
							|  |  |  |            expect(elapsed).toEqual(30); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should move periodic tasks', fakeAsync(() => { | 
					
						
							|  |  |  |            let ran = false; | 
					
						
							|  |  |  |            let count = 0; | 
					
						
							|  |  |  |            setInterval(() => { count++; }, 10); | 
					
						
							|  |  |  |            setTimeout(() => { ran = true; }, 35); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            let elapsed = flush(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            expect(count).toEqual(3); | 
					
						
							|  |  |  |            expect(ran).toEqual(true); | 
					
						
							|  |  |  |            expect(elapsed).toEqual(35); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            discardPeriodicTasks(); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('outside of the fakeAsync zone', () => { | 
					
						
							|  |  |  |       it('calling flushMicrotasks should throw', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         expect(() => { | 
					
						
							|  |  |  |           flushMicrotasks(); | 
					
						
							|  |  |  |         }).toThrowError('The code should be running in the fakeAsync zone to call this function'); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('calling tick should throw', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         expect(() => { | 
					
						
							|  |  |  |           tick(); | 
					
						
							|  |  |  |         }).toThrowError('The code should be running in the fakeAsync zone to call this function'); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-22 11:19:21 -07:00
										 |  |  |       it('calling flush should throw', () => { | 
					
						
							|  |  |  |         expect(() => { | 
					
						
							|  |  |  |           flush(); | 
					
						
							|  |  |  |         }).toThrowError('The code should be running in the fakeAsync zone to call this function'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  |       it('calling discardPeriodicTasks should throw', () => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |         expect(() => { | 
					
						
							|  |  |  |           discardPeriodicTasks(); | 
					
						
							|  |  |  |         }).toThrowError('The code should be running in the fakeAsync zone to call this function'); | 
					
						
							| 
									
										
										
										
											2016-05-26 10:19:30 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07: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 12:10:53 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-08-02 04:45:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07: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 14:08:58 +01:00
										 |  |  |       let state: string = 'not run'; | 
					
						
							| 
									
										
										
										
											2016-08-19 12:10:53 -07: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 04:45:15 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-27 09:53:37 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } |