From e5644204dc8007434e3d18582d10cc0bbb91935a Mon Sep 17 00:00:00 2001 From: Andrew Kushnir Date: Fri, 5 Oct 2018 17:46:58 -0700 Subject: [PATCH] fix(ivy): removing unnecessary assert in getOrCreateNodeInjector function (#26305) PR Close #26305 --- packages/core/src/render3/di.ts | 1 - packages/core/test/render3/pipe_spec.ts | 83 ++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/packages/core/src/render3/di.ts b/packages/core/src/render3/di.ts index ad029a1195..293f0eacac 100644 --- a/packages/core/src/render3/di.ts +++ b/packages/core/src/render3/di.ts @@ -82,7 +82,6 @@ export function bloomAdd(injectorIndex: number, tView: TView, type: Type): } export function getOrCreateNodeInjector(): number { - ngDevMode && assertPreviousIsParent(); return getOrCreateNodeInjectorForNode( getPreviousOrParentTNode() as TElementNode | TElementContainerNode | TContainerNode, _getViewData()); diff --git a/packages/core/test/render3/pipe_spec.ts b/packages/core/test/render3/pipe_spec.ts index 19b95489dd..4ea9a820dc 100644 --- a/packages/core/test/render3/pipe_spec.ts +++ b/packages/core/test/render3/pipe_spec.ts @@ -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 = { 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( + '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'})