|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google Inc. All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {ApplicationRef, Injector, NgZone} from '@angular/core'; |
| 10 | +import {NgElementApplicationContext} from '../src/ng-element-application-context'; |
| 11 | + |
| 12 | +export function main() { |
| 13 | + describe('NgElementApplicationContext', () => { |
| 14 | + let mockInjector: Injector; |
| 15 | + let mockZone: NgZone; |
| 16 | + let ctx: NgElementApplicationContext; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + mockZone = new NgZone({}); |
| 20 | + mockInjector = Injector.create([ |
| 21 | + {provide: ApplicationRef, useValue: 'mockApplicationRef'}, |
| 22 | + {provide: NgZone, useValue: mockZone}, |
| 23 | + ]); |
| 24 | + |
| 25 | + ctx = new NgElementApplicationContext(mockInjector); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should expose the `ApplicationRef`', |
| 29 | + () => { expect(ctx.applicationRef as any).toBe('mockApplicationRef'); }); |
| 30 | + |
| 31 | + it('should expose the `Injector`', () => { expect(ctx.injector).toBe(mockInjector); }); |
| 32 | + |
| 33 | + it('should expose the `NgZone`', () => { expect(ctx.ngZone).toBe(mockZone); }); |
| 34 | + |
| 35 | + describe('runInNgZone()', () => { |
| 36 | + it('should always run the callback inside the Angular zone', () => { |
| 37 | + (spyOn(NgZone, 'isInAngularZone').and as any).returnValues(false, true); |
| 38 | + spyOn(mockZone, 'run').and.callThrough(); |
| 39 | + const callbackSpy = (jasmine.createSpy('callback').and as any).returnValues('foo', 'bar'); |
| 40 | + |
| 41 | + const retValues = [ |
| 42 | + ctx.runInNgZone(callbackSpy), |
| 43 | + ctx.runInNgZone(callbackSpy), |
| 44 | + ]; |
| 45 | + |
| 46 | + expect(mockZone.run).toHaveBeenCalledTimes(2); |
| 47 | + expect(callbackSpy).toHaveBeenCalledTimes(2); |
| 48 | + expect(retValues).toEqual(['foo', 'bar']); |
| 49 | + }); |
| 50 | + }); |
| 51 | + }); |
| 52 | +} |
0 commit comments