fix(ivy): update ViewContainerRef to get the correct parentInjector (#35013)

PR Close #35013
This commit is contained in:
Andrew Scott
2020-01-28 10:22:17 -08:00
committed by Andrew Kushnir
parent 4bc0abf003
commit eaa4a5a76f
2 changed files with 69 additions and 6 deletions

View File

@ -470,6 +470,64 @@ describe('di', () => {
fixture.detectChanges();
expect(divElement.id).toBe('bar');
});
it('dynamic components should find dependencies when parent is projected', () => {
@Directive({selector: '[dirA]'})
class DirA {
}
@Directive({selector: '[dirB]'})
class DirB {
}
@Component({selector: 'child', template: ''})
class Child {
constructor(@Optional() readonly dirA: DirA, @Optional() readonly dirB: DirB) {}
}
@Component({
selector: 'projector',
template: '<ng-content></ng-content>',
})
class Projector {
}
@Component({
template: `
<projector>
<div dirA>
<ng-container #childOrigin></ng-container>
<ng-container #childOriginWithDirB dirB></ng-container>
</div>
</projector>`,
entryComponents: [Child]
})
class MyApp {
@ViewChild('childOrigin', {read: ViewContainerRef, static: true})
childOrigin !: ViewContainerRef;
@ViewChild('childOriginWithDirB', {read: ViewContainerRef, static: true})
childOriginWithDirB !: ViewContainerRef;
childFactory = this.resolver.resolveComponentFactory(Child);
constructor(readonly resolver: ComponentFactoryResolver, readonly injector: Injector) {}
addChild() { return this.childOrigin.createComponent(this.childFactory); }
addChildWithDirB() { return this.childOriginWithDirB.createComponent(this.childFactory); }
}
const fixture =
TestBed.configureTestingModule({declarations: [Child, DirA, DirB, Projector, MyApp]})
.createComponent(MyApp);
const child = fixture.componentInstance.addChild();
expect(child).toBeDefined();
expect(child.instance.dirA)
.not.toBeNull('dirA should be found. It is on the parent of the viewContainerRef.');
const child2 = fixture.componentInstance.addChildWithDirB();
expect(child2).toBeDefined();
expect(child2.instance.dirA)
.not.toBeNull('dirA should be found. It is on the parent of the viewContainerRef.');
expect(child2.instance.dirB)
.toBeNull(
'dirB appears on the ng-container and should not be found because the ' +
'viewContainerRef.createComponent node is inserted next to the container.');
});
});
it('should throw if directive is not found anywhere', () => {