Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/dom-testing-library/api-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,29 @@ fireEvent(
})
)
```

## Using Jest Function Mocks

[Jest's Mock functions](https://jestjs.io/docs/en/mock-functions) can be used to test
that a callback passed to the function was called, or what it was called when the event
that **should** trigger the callback function does trigger the bound callback.


<!--DOCUSAURUS_CODE_TABS-->

<!--React-->

```jsx
import { render, screen } from '@testing-library/react'

const Button = ({onClick, children}) => <button onClick={onClick}>{children}</button>

test('calls onClick prop when clicked', () => {
const handleClick = jest.fn()
render(<Button onClick={handleClick}>Click Me</Button>)
fireEvent.click(screen.getByText(/click me/i))
expect(handleClick).toHaveBeenCalledTimes(1)
})
```

<!--END_DOCUSAURUS_CODE_TABS-->