Unit Testing a class that uses Injector

15 views
Skip to first unread message

Brian Wilkinson

unread,
Jan 12, 2020, 4:22:53 PM1/12/20
to Angular and AngularJS discussion
I have a class that uses the Injector as below

export class DetailsComponent implements OnInitIContainer {
    viewsViewDetails[];

    // static true run before change detection
    @ViewChild('detailsRegion', { readViewContainerRefstatictrue })
    detailsRegionViewContainerRef;

    constructor(private injectorInjector) {}

    private insertView(
        viewContainerViewContainerRef,
        componentFactoryComponentFactory<any>
    ) {
        const compRef = componentFactory.create(this.injector);
        viewContainer.clear();
        viewContainer.insert(compRef.hostView);
    }

    ngOnInit() {}

    public insertsView(viewsViewDetails[]): void {
        this.views = views;

        views.forEach(v => {
            switch (v.region) {
                case ContainerView.Details:
                    this.insertView(this.detailsRegionv.componentFactory);
                    break;

                case ContainerView.Master:
                case ContainerView.Map:
                    throw new Error('Out of Range');
            }
        });
    }
}


When writing the unit test how do I capture the Injector so I can set up the component factory spy object to ensure that create is correctly called with it. I have tried setting up a provider in TestBed.configureTestingModule with no success.

Reply all
Reply to author
Forward
0 new messages