Content-Length: 295206 | pFad | http://github.com/stdavis/testing-library-docs/commit/14f57553e4d975c4a9821d99d43efbad23241ca2

57 Document using with Jest mock functions (#138) · stdavis/testing-library-docs@14f5755 · GitHub
Skip to content

Commit 14f5755

Browse files
Josh Pollockalexkrolick
Josh Pollock
andauthored
Document using with Jest mock functions (testing-library#138)
* Document using with Jest mock functions * Update jest mocking docs based on feedback from Kent * add react codetab, use latest methods Co-authored-by: Alex Krolick <alexkrolick@users.noreply.github.com>
1 parent f7e9a25 commit 14f5755

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

docs/dom-testing-library/api-events.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,29 @@ fireEvent(
130130
})
131131
)
132132
```
133+
134+
## Using Jest Function Mocks
135+
136+
[Jest's Mock functions](https://jestjs.io/docs/en/mock-functions) can be used to test
137+
that a callback passed to the function was called, or what it was called when the event
138+
that **should** trigger the callback function does trigger the bound callback.
139+
140+
141+
<!--DOCUSAURUS_CODE_TABS-->
142+
143+
<!--React-->
144+
145+
```jsx
146+
import { render, screen } from '@testing-library/react'
147+
148+
const Button = ({onClick, children}) => <button onClick={onClick}>{children}</button>
149+
150+
test('calls onClick prop when clicked', () => {
151+
const handleClick = jest.fn()
152+
render(<Button onClick={handleClick}>Click Me</Button>)
153+
fireEvent.click(screen.getByText(/click me/i))
154+
expect(handleClick).toHaveBeenCalledTimes(1)
155+
})
156+
```
157+
158+
<!--END_DOCUSAURUS_CODE_TABS-->

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/stdavis/testing-library-docs/commit/14f57553e4d975c4a9821d99d43efbad23241ca2

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy