Skip to content

Commit bb9565d

Browse files
committed
feat(elements): implement NgElementApplicationContext
1 parent 4628017 commit bb9565d

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
11+
export class NgElementApplicationContext {
12+
applicationRef = this.injector.get<ApplicationRef>(ApplicationRef);
13+
ngZone = this.injector.get<NgZone>(NgZone);
14+
15+
constructor(public injector: Injector) {}
16+
17+
runInNgZone<R>(cb: () => R): R { return this.ngZone.run(cb); }
18+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)