File tree Expand file tree Collapse file tree 7 files changed +1448
-1147
lines changed Expand file tree Collapse file tree 7 files changed +1448
-1147
lines changed Original file line number Diff line number Diff line change 6666 "url" : " https://github.com/learning-zone/react-unit-test.git"
6767 },
6868 "devDependencies" : {
69+ "@mnajdova/enzyme-adapter-react-18" : " ^0.2.0" ,
6970 "enzyme" : " ^3.11.0" ,
70- "enzyme-adapter-react-17-updated" : " ^1.0.2" ,
7171 "enzyme-to-json" : " ^3.6.2" ,
7272 "jest-canvas-mock" : " ^2.4.0" ,
7373 "jest-enzyme" : " ^7.1.2" ,
Original file line number Diff line number Diff line change @@ -9,17 +9,18 @@ exports[`Events Component Matches the snapshot 1`] = `
99 >
1010 Event & Snapshot Test
1111 </h1 >
12- <div
12+ <h2
1313 data-test = " counter-value"
1414 >
1515 0
16- </div >
16+ </h2 >
1717 <button
18- className = " MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary"
18+ className = " MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium css-sghohy-MuiButtonBase-root-MuiButton-root "
1919 data-test = " increment-btn"
2020 disabled = { false }
2121 onBlur = { [Function ]}
2222 onClick = { [Function ]}
23+ onContextMenu = { [Function ]}
2324 onDragLeave = { [Function ]}
2425 onFocus = { [Function ]}
2526 onKeyDown = { [Function ]}
@@ -33,21 +34,16 @@ exports[`Events Component Matches the snapshot 1`] = `
3334 tabIndex = { 0 }
3435 type = " button"
3536 >
36- <span
37- className = " MuiButton-label"
38- >
39- Increment
40- </span >
37+ Increment
4138 </button >
42- <br />
43-
44- <br />
39+
4540 <button
46- className = " MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary"
41+ className = " MuiButtonBase-root MuiButton-root MuiButton-contained MuiButton-containedPrimary MuiButton-sizeMedium MuiButton-containedSizeMedium css-sghohy-MuiButtonBase-root-MuiButton-root "
4742 data-test = " decrement-btn"
4843 disabled = { false }
4944 onBlur = { [Function ]}
5045 onClick = { [Function ]}
46+ onContextMenu = { [Function ]}
5147 onDragLeave = { [Function ]}
5248 onFocus = { [Function ]}
5349 onKeyDown = { [Function ]}
@@ -61,11 +57,7 @@ exports[`Events Component Matches the snapshot 1`] = `
6157 tabIndex = { 0 }
6258 type = " button"
6359 >
64- <span
65- className = " MuiButton-label"
66- >
67- Decrement
68- </span >
60+ Decrement
6961 </button >
7062</div >
7163` ;
Original file line number Diff line number Diff line change @@ -20,22 +20,22 @@ const styles = () => ({
2020 } ,
2121} ) ;
2222
23- const ButtonAppBar = ( props ) => {
23+ const Header = ( props ) => {
2424
2525 const { classes } = props ;
2626
2727 return (
2828 < div className = { classes . root } >
2929 < AppBar position = 'static' >
3030 < Toolbar >
31- < IconButton edge = 'start' className = { classes . menuButton } color = 'inherit' aria-label = 'menu' data-test = 'menu-icon' >
31+ < IconButton edge = 'start' className = { classes . menuButton } color = 'inherit' aria-label = 'menu' data-testid = 'menu-icon' >
3232 < MenuIcon />
3333 </ IconButton >
34- < Typography variant = 'h6' className = { classes . title } data-test = 'menu-title' > React Unit Testing with Jest and Enzyme</ Typography >
34+ < Typography variant = 'h6' className = { classes . title } data-testid = 'menu-title' > React Unit Testing with Jest and Enzyme</ Typography >
3535 < Button color = 'inherit' > Login</ Button >
3636 </ Toolbar >
3737 </ AppBar >
3838 </ div >
3939 )
4040}
41- export default withStyles ( styles ) ( ButtonAppBar ) ;
41+ export default withStyles ( styles ) ( Header ) ;
Original file line number Diff line number Diff line change 1- import React from 'react'
2- import { shallow } from 'enzyme'
3- import Header from './Header'
1+ import React from 'react' ;
2+ import { mount } from 'enzyme' ;
3+ import { render , fireEvent , screen } from "@testing-library/react" ;
4+ import Header from './Header' ;
45
56/**
67 * shallow():- method is used to render the single component that we are testing.
@@ -21,17 +22,17 @@ describe('Header Component', () => {
2122
2223 let wrapper
2324 beforeEach ( ( ) => {
24- wrapper = shallow ( < Header /> )
25+ wrapper = render ( < Header /> ) ;
2526 } )
2627
27- test ( 'render without errors ' , ( ) => {
28- let component = wrapper . find ( `[data-test=' menu-icon']` )
29- expect ( component . length ) . toBe ( 1 )
28+ test ( 'should check the menu icon ' , ( ) => {
29+ const component = screen . getByTestId ( " menu-icon" ) ;
30+ expect ( component ) ;
3031 } )
3132
32- test ( 'title' , ( ) => {
33- let title = wrapper . find ( `[data-test=' menu-title']` ) . text ( )
34- expect ( title ) . toMatch ( 'Testing React App ' )
33+ test ( 'should check the header component title' , ( ) => {
34+ const title = screen . getByTestId ( " menu-title" ) . text ;
35+ expect ( title ) . toMatch ( 'React Unit Testing with Jest and Enzyme ' )
3536 } )
3637
3738} )
Original file line number Diff line number Diff line change @@ -20,7 +20,7 @@ export default class RestAPI extends Component {
2020 async componentDidMount ( ) {
2121 try {
2222 const response = await axios . get ( `https://jsonplaceholder.typicode.com/users` )
23- const results = response . data
23+ const results = response ? response . data : "" ;
2424 this . setState ( { results } )
2525 } catch ( error ) {
2626 console . log ( error ) ;
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ import '@testing-library/jest-dom';
66import Enzyme from 'enzyme'
77import 'jest-enzyme'
88import '@testing-library/jest-dom/extend-expect'
9- import Adapter from 'enzyme-adapter-react-17-updated '
9+ import Adapter from '@mnajdova/ enzyme-adapter-react-18 '
1010import { createSerializer } from 'enzyme-to-json'
1111
1212
You can’t perform that action at this time.
0 commit comments