vendredi 20 janvier 2017

Angular 2, Jasmine: test a function that contains a setTimeout

I have close function in my component contains a setTimeout() to give time for the animation to complete.

public close() {
    this.animate = "inactive"
    setTimeout(() => {
       this.show = false
    }, 250)
}

this.show is bound to an ngIf.

this.animate is bound to an animation.

I have a test that needs to test this function

it("tests the exit button click", () => {
  comp.close()
  fixture.detectChanges()
  //verifies the element is no longer in the DOM
  const popUpWindow = fixture.debugElement.query(By.css("#popup-window"))
  expect(popUpWindow).toEqual(null)
})

How do you properly test this function when there is a setTimeout()?

I was using jasmine.clock().tick(251) but the window would never disappear. any thoughts on this as well?

Aucun commentaire:

Enregistrer un commentaire