Last active
July 7, 2020 03:13
-
-
Save damusnet/f15a3c23162012be66d7f0b36c930734 to your computer and use it in GitHub Desktop.
Basic react-test-renderer Click test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const App = () => { | |
const [counter, setCounter] = React.useState(0) | |
return ( | |
<div> | |
You clicked me {counter} times! | |
<ClickMe counter={counter} setCounter={setCounter} /> | |
</div> | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ClickMe = ({ counter, setCounter }) => { | |
return ( | |
<button type="button" onClick={() => setCounter(counter + 1)}> | |
Click me! | |
</button> | |
) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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