Skip to content

Instantly share code, notes, and snippets.

@damusnet
Last active July 7, 2020 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damusnet/f15a3c23162012be66d7f0b36c930734 to your computer and use it in GitHub Desktop.
Save damusnet/f15a3c23162012be66d7f0b36c930734 to your computer and use it in GitHub Desktop.
Basic react-test-renderer Click test
const App = () => {
const [counter, setCounter] = React.useState(0)
return (
<div>
You clicked me {counter} times!
<ClickMe counter={counter} setCounter={setCounter} />
</div>
)
}
const ClickMe = ({ counter, setCounter }) => {
return (
<button type="button" onClick={() => setCounter(counter + 1)}>
Click me!
</button>
)
}
import TestRenderer, { act } from 'react-test-renderer';
describe('<ClickMe />', () => {
it('should render properly', () => {
const mockFn = jest.fn()
const component = TestRenderer.create(
<ClickMe counter={0} setCounter={mockFn} />,
)
expect(component.toJSON()).toMatchSnapshot()
act(() => {
component.root.findByProps({ children: 'Click Me!' }).props.onClick()
})
expect(mockFn).toHaveBeenCalledWith(1)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment