test(ivy): NodeInjector should know how to get itself (INJECTOR) (#28009)

PR Close #28009
This commit is contained in:
Marc Laval 2019-01-14 10:20:27 +01:00 committed by Andrew Kushnir
parent b5c2ef2877
commit fa53150692
2 changed files with 31 additions and 2 deletions

View File

@ -9,7 +9,6 @@
import {Component, INJECTOR, Injectable, NgModule} from '@angular/core'; import {Component, INJECTOR, Injectable, NgModule} from '@angular/core';
import {TestBed} from '@angular/core/testing'; import {TestBed} from '@angular/core/testing';
import {renderModuleFactory} from '@angular/platform-server'; import {renderModuleFactory} from '@angular/platform-server';
import {fixmeIvy} from '@angular/private/testing';
import {BasicAppModuleNgFactory} from 'app_built/src/basic.ngfactory'; import {BasicAppModuleNgFactory} from 'app_built/src/basic.ngfactory';
import {DepAppModuleNgFactory} from 'app_built/src/dep.ngfactory'; import {DepAppModuleNgFactory} from 'app_built/src/dep.ngfactory';
import {HierarchyAppModuleNgFactory} from 'app_built/src/hierarchy.ngfactory'; import {HierarchyAppModuleNgFactory} from 'app_built/src/hierarchy.ngfactory';

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {Attribute, ChangeDetectorRef, ElementRef, Host, Inject, InjectFlags, Injector, Optional, Renderer2, Self, SkipSelf, TemplateRef, ViewContainerRef, createInjector, defineInjectable, defineInjector} from '@angular/core'; import {Attribute, ChangeDetectorRef, ElementRef, Host, INJECTOR, Inject, InjectFlags, Injector, Optional, Renderer2, Self, SkipSelf, TemplateRef, ViewContainerRef, createInjector, defineInjectable, defineInjector} from '@angular/core';
import {ComponentType, RenderFlags} from '@angular/core/src/render3/interfaces/definition'; import {ComponentType, RenderFlags} from '@angular/core/src/render3/interfaces/definition';
import {defineComponent} from '../../src/render3/definition'; import {defineComponent} from '../../src/render3/definition';
@ -1460,6 +1460,36 @@ describe('di', () => {
expect(injectorDir.injector).not.toBe(otherInjectorDir.injector); expect(injectorDir.injector).not.toBe(otherInjectorDir.injector);
}); });
it('should inject INJECTOR', () => {
let injectorDir !: INJECTORDir;
let divElement !: HTMLElement;
class INJECTORDir {
constructor(public injector: Injector) {}
static ngDirectiveDef = defineDirective({
type: INJECTORDir,
selectors: [['', 'injectorDir', '']],
factory: () => injectorDir = new INJECTORDir(directiveInject(INJECTOR as any))
});
}
/** <div injectorDir otherInjectorDir></div> */
const App = createComponent('app', (rf: RenderFlags, ctx: any) => {
if (rf & RenderFlags.Create) {
element(0, 'div', ['injectorDir', '']);
}
// testing only
divElement = load(0);
}, 1, 0, [INJECTORDir]);
const fixture = new ComponentFixture(App);
expect(injectorDir.injector.get(ElementRef).nativeElement).toBe(divElement);
expect(injectorDir.injector.get(Injector).get(ElementRef).nativeElement).toBe(divElement);
expect(injectorDir.injector.get(INJECTOR).get(ElementRef).nativeElement).toBe(divElement);
});
}); });
describe('ElementRef', () => { describe('ElementRef', () => {