Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to trigger transitionend with Jest unit testing

I am using angular 8 and Jest for unit testing. I have added a listener for 'transitionend' on the element, but I haven't been able to figure out how to trigger/mock the transitionend event with Jest

this.animatedElement.nativeElement.addEventListener('transitionend', () => {
      this.transitionEnded = true;
});

I am trying to create a unit test that tests this.transitionEnded is true

like image 293
mrdeleon Avatar asked Jan 20 '26 20:01

mrdeleon


1 Answers

Use this helper function to trigger any event:

function triggerTransitionEnd(element) {
   let event = document.createEvent("Event");
   event.initEvent("transitionend", true, true);
   element.dispatchEvent(event);
}

And then in your test use it like so:

test("Some description here", () => {
    const popup = document.getElementById("popup");
    triggerTransitionEnd(popup);
    expect(popup.classList.contains("show")).toBeFalsy();
});
like image 80
Weam Adel Avatar answered Jan 23 '26 11:01

Weam Adel