feat(elements): implement `NgElementApplicationContext`

This commit is contained in:
George Kalpakas 2017-10-12 18:02:34 +03:00 committed by Victor Berchet
parent 6b30fbf94e
commit 75cf70ae04
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,18 @@
/**
* @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
*/
import {ApplicationRef, Injector, NgZone} from '@angular/core';
export class NgElementApplicationContext {
applicationRef = this.injector.get<ApplicationRef>(ApplicationRef);
ngZone = this.injector.get<NgZone>(NgZone);
constructor(public injector: Injector) {}
runInNgZone<R>(cb: () => R): R { return this.ngZone.run(cb); }
}

View File

@ -0,0 +1,52 @@
/**
* @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
*/
import {ApplicationRef, Injector, NgZone} from '@angular/core';
import {NgElementApplicationContext} from '../src/ng-element-application-context';
export function main() {
describe('NgElementApplicationContext', () => {
let mockInjector: Injector;
let mockZone: NgZone;
let ctx: NgElementApplicationContext;
beforeEach(() => {
mockZone = new NgZone({});
mockInjector = Injector.create([
{provide: ApplicationRef, useValue: 'mockApplicationRef'},
{provide: NgZone, useValue: mockZone},
]);
ctx = new NgElementApplicationContext(mockInjector);
});
it('should expose the `ApplicationRef`',
() => { expect(ctx.applicationRef as any).toBe('mockApplicationRef'); });
it('should expose the `Injector`', () => { expect(ctx.injector).toBe(mockInjector); });
it('should expose the `NgZone`', () => { expect(ctx.ngZone).toBe(mockZone); });
describe('runInNgZone()', () => {
it('should always run the callback inside the Angular zone', () => {
(spyOn(NgZone, 'isInAngularZone').and as any).returnValues(false, true);
spyOn(mockZone, 'run').and.callThrough();
const callbackSpy = (jasmine.createSpy('callback').and as any).returnValues('foo', 'bar');
const retValues = [
ctx.runInNgZone(callbackSpy),
ctx.runInNgZone(callbackSpy),
];
expect(mockZone.run).toHaveBeenCalledTimes(2);
expect(callbackSpy).toHaveBeenCalledTimes(2);
expect(retValues).toEqual(['foo', 'bar']);
});
});
});
}