1
votes

I have following scenario:

ParentComponent and ChildComponent both has a name property. ParentComponent is injected to the ChildComponent and the child shows the name of the parent and his own.

I know to solve this problem with a service would be the better way...

Now the problem I have in the tests. The name of the parent is set on ngOnInit, and that is to late. My test does not work. I think I should create a ComponentFixture to use the method whenStable(), but the ParentComponent is already created in beforeEach, when I create the fixture of the ChildComponent:

beforeEach(async(() => {
    TestBed.configureTestingModule({
        declarations: [ ParentComponent, ChildComponent ],
        providers: [ParentComponent]
    }).compileComponents();
}));

beforeEach(() => {
    fixture = TestBed.createComponent(ChildComponent);
    component = fixture.componentInstance;
    // how to create fixture of:
    // component.parentComponent
    // using the existing instance
    fixture.detectChanges();
});

Or is there an other way to wait until the host component is ready? using whenStable of the ComponentFixture of ChildComponent does not work.

here complete example on stackblitz.com

1

1 Answers

0
votes

Assuming you are writing unit test cases, as a workaround you can invoke ParentComponent inside beforeEach() by adding

component.parentComponent.ngOnInit();