Skip to content

Commit 4bbb326

Browse files
committed
enzyme-adapter-react-18
1 parent f6d5533 commit 4bbb326

File tree

7 files changed

+1448
-1147
lines changed

7 files changed

+1448
-1147
lines changed

package-lock.json

Lines changed: 1421 additions & 1113 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@
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",

src/Components/Events/__snapshots__/Events.test.js.snap

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff 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
`;

src/Components/Header/Header.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff 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);

src/Components/Header/Header.test.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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
})

src/Components/RestAPI/RestAPI.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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);

src/setupTests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import '@testing-library/jest-dom';
66
import Enzyme from 'enzyme'
77
import 'jest-enzyme'
88
import '@testing-library/jest-dom/extend-expect'
9-
import Adapter from 'enzyme-adapter-react-17-updated'
9+
import Adapter from '@mnajdova/enzyme-adapter-react-18'
1010
import { createSerializer } from 'enzyme-to-json'
1111

1212

0 commit comments

Comments
 (0)