December 2019
Intermediate to advanced
474 pages
10h 3m
English
Other than react-test-renderer, Enzyme can handle onClick events on the shallow rendered component. To test this, you have to create a mocked version of the function, which should be fired once the component is clicked. After this, Jest can check whether or not the function was executed.
The Button component that you tested previously doesn't just take children as a prop – it also takes the onClick function. Let's try and see if this can be tested using Jest and Enzyme by creating a new test scenario in the file for the Button component:
import React from 'react';import { shallow } from 'enzyme';import Button from './Button';describe('the <Button /> component', () => { ...+ it('should handle the onClick ...