Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I wait for async ngOninit to complete before testing

I have an ngOnInit method that does async work.

How can I execute my expect statements once the async work is complete ?

it('test things once all the promises declared in ngOninit are resolved', () => {
  comp.ngOnInit();

  // I want to test that comp.property is like expected

});

My component methods look like this:

OnInit() {
  this.asyncMethod();
};

asyncMethod() {
  this.methodThatReturnsPromise().then(() => {
    this.property = this.otherPropertyNowResolved;
  })
};
like image 319
Lev Avatar asked Mar 09 '23 23:03

Lev


1 Answers

fixture = TestBed.createComponent(MyComponent);


it('test things once all the promises declared in ngOninit are resolved', async(() => {
        fixture.detectChanges();
        fixture.whenStable().then(
                    () => {
                         // This should run once ngOnInit is completed

                    }
                );
}));
like image 178
Amit Chigadani Avatar answered Mar 31 '23 19:03

Amit Chigadani