Testing · Junior · Question 57 of 78

What does toHaveBeenCalledTimes(1) check on a prop callback after a click?

const onClick = vi.fn();
render(<Button onClick={onClick}>Ok</Button>);
await userEvent.setup().click(screen.getByRole("button"));
expect(onClick).toHaveBeenCalledTimes(1);

Explanation

Passing a mock as a prop is the simplest way to test what a component reports to its parent.

Further reading

Problem with a question

Testing · Junior · question 57

Test yourself

Take the Testing · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz