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);- ✓Correct answer: That the component called the callback exactly once in response to the click
- 2That the button was rendered once
- 3That
onClickis a function
Explanation
Passing a mock as a prop is the simplest way to test what a component reports to its parent.