dimanche 18 octobre 2020

Test: simulate a click on html element and call a function with jest and enzyme

Im trying to check if the function it's called once. But the test fail always.

HTML DOM:

<button
  className="sort-button"
  onClick={() => handleSortByRating(!sortByRating)}
>
  Rating <FaCaretDown className={iconSort} />
</button>

My Test with jest and enzyme:

test("Check if order tracks functions it's called", () => {
  const handleSortByRating = jest.fn();

  trackList.find(".sort-button").simulate("click");
  expect(handleSortByRating).toHaveBeenCalledTimes(1);
});

Output of jest:

expect(jest.fn()).toHaveBeenCalledTimes(expected)

  Expected number of calls: 1
  Received number of calls: 0

 expect(handleSortByRating).toHaveBeenCalledTimes(1);
                            ^

Have I to pass the arguement of the function? And if yes, how?

Aucun commentaire:

Enregistrer un commentaire