Skip to Main Content
React Projects
book

React Projects

by Roy Derks
December 2019
Intermediate to advanced content levelIntermediate to advanced
474 pages
10h 3m
English
Packt Publishing
Content preview from React Projects

Testing assertions with shallow rendering

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

React Projects - Second Edition

React Projects - Second Edition

Roy Derks
Test-Driven React

Test-Driven React

Trevor Burnham

Publisher Resources

ISBN: 9781789954937Supplemental Content