fix(ivy): removing unnecessary assert in getOrCreateNodeInjector function (#26305)

PR Close #26305
This commit is contained in:
Andrew Kushnir 2018-10-05 17:46:58 -07:00 committed by Jason Aden
parent 69b9758ab8
commit e5644204dc
2 changed files with 82 additions and 2 deletions

View File

@ -82,7 +82,6 @@ export function bloomAdd(injectorIndex: number, tView: TView, type: Type<any>):
}
export function getOrCreateNodeInjector(): number {
ngDevMode && assertPreviousIsParent();
return getOrCreateNodeInjectorForNode(
getPreviousOrParentTNode() as TElementNode | TElementContainerNode | TContainerNode,
_getViewData());

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Directive, OnChanges, OnDestroy, Pipe, PipeTransform} from '@angular/core';
import {Directive, InjectionToken, OnChanges, OnDestroy, Pipe, PipeTransform, createInjector, defineInjectable, defineInjector, ɵNgModuleDef as NgModuleDef, ɵPublicFeature as PublicFeature, ɵdefineComponent as defineComponent, ɵdirectiveInject as directiveInject} from '@angular/core';
import {expect} from '@angular/platform-browser/testing/src/matchers';
import {defineDirective, definePipe} from '../../src/render3/definition';
@ -328,6 +328,87 @@ describe('pipe', () => {
});
});
describe('injection mechanism', () => {
class ServiceA {
title = 'ServiceA Title';
}
class ServiceB {
title = 'ServiceB Title';
static ngInjectableDef =
defineInjectable({providedIn: 'root', factory: () => new ServiceB()});
}
class ModuleA {
static ngInjectorDef = defineInjector({factory: () => new ModuleA(), providers: [ServiceA]});
static ngModuleDef: NgModuleDef<any> = { bootstrap: [] } as any;
}
const generatePipe = (InjectionType: any) => {
return class MyConcatPipe implements PipeTransform {
constructor(public obj: any) {}
transform(value: string): string { return `${value} - ${this.obj.title}`; }
static ngPipeDef = definePipe({
name: 'myConcatPipe',
type: MyConcatPipe,
factory: () => new MyConcatPipe(directiveInject(InjectionType)),
pure: false
});
};
};
const generateComponent = (overrides: any) => {
return class MyComponent {
title = 'MyComponent Title';
static ngComponentDef = defineComponent({
type: MyComponent,
selectors: [['my-app']],
features: [PublicFeature],
factory: function MyComponent_Factory() { return new MyComponent(); },
consts: 2,
vars: 3,
// '{{ title | myConcatPipe }}'
template: (rf: RenderFlags, ctx: MyComponent) => {
if (rf & 1) {
text(0);
pipe(1, 'myConcatPipe');
}
if (rf & 2) {
textBinding(0, interpolation1('', pipeBind1(1, 1, ctx.title), ''));
}
},
...overrides
});
};
};
it('should be able to handle Service injection', () => {
const Comp = generateComponent({providers: [ServiceB], pipes: [generatePipe(ServiceB)]});
const fixture = new ComponentFixture(Comp);
expect(fixture.html).toEqual('MyComponent Title - ServiceB Title');
});
it('should be able to handle Token injections', () => {
const provider = new InjectionToken<ServiceA>(
'token', {providedIn: 'root', factory: () => new ServiceB()});
const Comp = generateComponent({providers: [provider], pipes: [generatePipe(provider)]});
const fixture = new ComponentFixture(Comp);
expect(fixture.html).toEqual('MyComponent Title - ServiceB Title');
});
it('should be able to handle Module injection', () => {
const injector = createInjector(ModuleA);
const Comp = generateComponent({providers: [], pipes: [generatePipe(ServiceA)]});
const fixture = new ComponentFixture(Comp, {injector});
expect(fixture.html).toEqual('MyComponent Title - ServiceA Title');
});
});
});
@Pipe({name: 'countingPipe'})