Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/core/src/render3/di.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
83 changes: 82 additions & 1 deletion packages/core/test/render3/pipe_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'})
Expand Down