Skip to content

Commit e4b0a87

Browse files
committed
Replaced Enzyme with Testing Library
1 parent eb407bd commit e4b0a87

File tree

58 files changed

+6000
-4955
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+6000
-4955
lines changed

.eslintrc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
"Request": true,
4040
"fetch": true,
4141
"reducerTester": true,
42-
"mountWithProviders": true,
43-
"shallowWithProviders": true
42+
"renderWithProviders": true
4443
},
4544
"parser": "babel-eslint",
4645
"settings": {

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ React Firebase Admin is our in-house admin dashboard boilerplate, used in many o
9292
### Unit Testing
9393

9494
- [Jest](https://jestjs.io/) (★ 29.9k) as testing framework (see [docs](https://jestjs.io/docs/en/getting-started)).
95-
- [Enzyme](https://airbnb.io/enzyme/) (★ 18.5k) to test react components in Jest.
95+
- [React Testing Library](https://testing-library.com/docs/react-testing-library/intro) (★ 12.7k) to test react components in Jest.
9696
- [Redux-mock-store](https://github.com/dmitry-zaets/redux-mock-store) (★ 2.1k) to test redux actions, reducers and store state in Jest.
9797

9898
### Linting

functions/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"sourceMap": true,
88
"strict": true,
99
"target": "es2017",
10-
"resolveJsonModule": true
10+
"resolveJsonModule": true,
11+
"skipLibCheck": true
1112
},
1213
"compileOnSave": true,
1314
"include": ["src"]

package-lock.json

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

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,13 @@
5656
]
5757
},
5858
"devDependencies": {
59+
"@testing-library/jest-dom": "^5.11.4",
60+
"@testing-library/react": "^11.0.2",
5961
"babel-eslint": "^10.1.0",
6062
"babel-loader": "^8.1.0",
6163
"cross-env": "^7.0.2",
6264
"deep-freeze": "^0.0.1",
6365
"dotenv": "^8.2.0",
64-
"enzyme": "^3.11.0",
65-
"enzyme-adapter-react-16": "^1.15.3",
66-
"enzyme-to-json": "^3.4.4",
6766
"eslint": "^6.8.0",
6867
"eslint-config-airbnb": "^18.2.0",
6968
"eslint-config-prettier": "^6.11.0",
@@ -88,9 +87,6 @@
8887
}
8988
},
9089
"jest": {
91-
"snapshotSerializers": [
92-
"enzyme-to-json/serializer"
93-
],
9490
"collectCoverageFrom": [
9591
"src/**/*.{js,jsx}"
9692
],

src/components/ConfirmationModal/ConfirmationModal.test.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
2-
import { shallow } from 'enzyme';
2+
import { render, fireEvent } from '@testing-library/react';
3+
import '@testing-library/jest-dom';
34

45
import ConfirmationModal from '.';
56

@@ -13,7 +14,7 @@ describe('<ConfirmationModal /> rendering', () => {
1314
});
1415

1516
it('should render without crashing', () => {
16-
const component = shallow(
17+
const component = render(
1718
<ConfirmationModal
1819
isActive
1920
confirmButtonMessage="test message"
@@ -25,11 +26,11 @@ describe('<ConfirmationModal /> rendering', () => {
2526
/>
2627
);
2728

28-
expect(component).toMatchSnapshot();
29+
expect(component.asFragment()).toMatchSnapshot();
2930
});
3031

3132
it('should set the active modifier if the isActive prop is passed down', () => {
32-
const component = shallow(
33+
const component = render(
3334
<ConfirmationModal
3435
isActive
3536
confirmButtonMessage="test message"
@@ -41,11 +42,13 @@ describe('<ConfirmationModal /> rendering', () => {
4142
/>
4243
);
4344

44-
expect(component.exists('div.modal.is-active')).toBeTruthy();
45+
expect(
46+
component.container.querySelector('div.modal.is-active')
47+
).toBeTruthy();
4548
});
4649

4750
it('should not set the active modifier if the isActive prop is not passed down', () => {
48-
const component = shallow(
51+
const component = render(
4952
<ConfirmationModal
5053
confirmButtonMessage="test message"
5154
onConfirmation={onConfirm}
@@ -56,11 +59,11 @@ describe('<ConfirmationModal /> rendering', () => {
5659
/>
5760
);
5861

59-
expect(component.exists('div.modal.is-active')).toBeFalsy();
62+
expect(component.container.querySelector('div.modal.is-active')).toBeNull();
6063
});
6164

6265
it('should call onConfirm when the confirmation button is clicked', () => {
63-
const component = shallow(
66+
const component = render(
6467
<ConfirmationModal
6568
isActive
6669
confirmButtonMessage="confirm test message"
@@ -72,13 +75,12 @@ describe('<ConfirmationModal /> rendering', () => {
7275
/>
7376
);
7477

75-
component.find('button[children="confirm test message"]').simulate('click');
76-
78+
fireEvent.click(component.getByText('confirm test message'));
7779
expect(onConfirm).toHaveBeenCalled();
7880
});
7981

8082
it('should call onCancel when the cancel button is clicked', () => {
81-
const component = shallow(
83+
const component = render(
8284
<ConfirmationModal
8385
isActive
8486
confirmButtonMessage="test message"
@@ -90,13 +92,13 @@ describe('<ConfirmationModal /> rendering', () => {
9092
/>
9193
);
9294

93-
component.find('button[children="cancel test message"]').simulate('click');
95+
fireEvent.click(component.getByText('cancel test message'));
9496

9597
expect(onCancel).toHaveBeenCalled();
9698
});
9799

98100
it('should set the title of the modal', () => {
99-
const component = shallow(
101+
const component = render(
100102
<ConfirmationModal
101103
isActive
102104
confirmButtonMessage="test message"
@@ -108,11 +110,11 @@ describe('<ConfirmationModal /> rendering', () => {
108110
/>
109111
);
110112

111-
expect(component.exists('p[children="test title"]')).toBeTruthy();
113+
expect(component.getByText('test title')).toBeTruthy();
112114
});
113115

114116
it('should set the body of the modal', () => {
115-
const component = shallow(
117+
const component = render(
116118
<ConfirmationModal
117119
isActive
118120
confirmButtonMessage="test message"
@@ -124,11 +126,11 @@ describe('<ConfirmationModal /> rendering', () => {
124126
/>
125127
);
126128

127-
expect(component.exists('section[children="test body"]')).toBeTruthy();
129+
expect(component.getByText('test body')).toBeTruthy();
128130
});
129131

130132
it('should set the confirm button message', () => {
131-
const component = shallow(
133+
const component = render(
132134
<ConfirmationModal
133135
isActive
134136
confirmButtonMessage="confirm test message"
@@ -140,13 +142,11 @@ describe('<ConfirmationModal /> rendering', () => {
140142
/>
141143
);
142144

143-
expect(
144-
component.exists('button[children="confirm test message"]')
145-
).toBeTruthy();
145+
expect(component.getByText('confirm test message')).toBeTruthy();
146146
});
147147

148148
it('should set the cancel button message', () => {
149-
const component = shallow(
149+
const component = render(
150150
<ConfirmationModal
151151
isActive
152152
confirmButtonMessage="test message"
@@ -158,8 +158,6 @@ describe('<ConfirmationModal /> rendering', () => {
158158
/>
159159
);
160160

161-
expect(
162-
component.exists('button[children="cancel test message"]')
163-
).toBeTruthy();
161+
expect(component.getByText('cancel test message')).toBeTruthy();
164162
});
165163
});
Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,47 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`<ConfirmationModal /> rendering should render without crashing 1`] = `
4-
<div
5-
className="modal is-active"
6-
>
4+
<DocumentFragment>
75
<div
8-
className="modal-background"
9-
onClick={[MockFunction]}
10-
/>
11-
<div
12-
className="modal-card"
6+
class="modal is-active"
137
>
14-
<header
15-
className="modal-card-head"
8+
<div
9+
class="modal-background"
10+
/>
11+
<div
12+
class="modal-card"
1613
>
17-
<p
18-
className="modal-card-title"
14+
<header
15+
class="modal-card-head"
1916
>
20-
test title
21-
</p>
22-
</header>
23-
<section
24-
className="modal-card-body"
25-
>
26-
test body
27-
</section>
28-
<footer
29-
className="modal-card-foot"
30-
>
31-
<button
32-
className="button is-danger undefined"
33-
onClick={[MockFunction]}
34-
type="button"
17+
<p
18+
class="modal-card-title"
19+
>
20+
test title
21+
</p>
22+
</header>
23+
<section
24+
class="modal-card-body"
3525
>
36-
test message
37-
</button>
38-
<button
39-
className="button"
40-
onClick={[MockFunction]}
41-
type="button"
26+
test body
27+
</section>
28+
<footer
29+
class="modal-card-foot"
4230
>
43-
test message
44-
</button>
45-
</footer>
31+
<button
32+
class="button is-danger undefined"
33+
type="button"
34+
>
35+
test message
36+
</button>
37+
<button
38+
class="button"
39+
type="button"
40+
>
41+
test message
42+
</button>
43+
</footer>
44+
</div>
4645
</div>
47-
</div>
46+
</DocumentFragment>
4847
`;

src/components/ConfirmationModal/index.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const ConfirmationModal = ({
1111
confirmButtonMessage,
1212
onConfirmation,
1313
cancelButtonMessage,
14-
onCancel
14+
onCancel,
1515
}) => {
1616
const modifiers = isActive && 'is-active';
1717
const loadingModifier = isLoading && 'is-loading';
@@ -57,7 +57,7 @@ ConfirmationModal.propTypes = {
5757
confirmButtonMessage: PropTypes.string.isRequired,
5858
onConfirmation: PropTypes.func.isRequired,
5959
cancelButtonMessage: PropTypes.string.isRequired,
60-
onCancel: PropTypes.func.isRequired
60+
onCancel: PropTypes.func.isRequired,
6161
};
6262

6363
export default ConfirmationModal;
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import React from 'react';
2-
import DatePicker from 'react-datepicker';
32

43
import DatePickerStyled from '.';
54

65
describe('<DatePickerStyled /> rendering', () => {
76
const onChange = jest.fn();
87

98
it('should render without crashing', () => {
10-
const { component } = shallowWithProviders(
9+
const { component } = renderWithProviders(
1110
<DatePickerStyled
1211
name="test"
1312
dateFormat="en-US"
@@ -16,11 +15,11 @@ describe('<DatePickerStyled /> rendering', () => {
1615
/>
1716
)({});
1817

19-
expect(component).toMatchSnapshot();
18+
expect(component.asFragment()).toMatchSnapshot();
2019
});
2120

2221
it('should render <DatePicker /> component correctly', () => {
23-
const { component } = mountWithProviders(
22+
const { component } = renderWithProviders(
2423
<DatePickerStyled
2524
name="test"
2625
dateFormat="en-US"
@@ -29,11 +28,11 @@ describe('<DatePickerStyled /> rendering', () => {
2928
/>
3029
)({});
3130

32-
expect(component.exists(DatePicker)).toBeTruthy();
31+
expect(component.container.querySelector('input')).toBeTruthy();
3332
});
3433

3534
it('should pass the date prop to <DatePicker selected={date} /> correctly', () => {
36-
const { component } = mountWithProviders(
35+
const { component } = renderWithProviders(
3736
<DatePickerStyled
3837
name="test"
3938
dateFormat="en-US"
@@ -42,8 +41,6 @@ describe('<DatePickerStyled /> rendering', () => {
4241
/>
4342
)({});
4443

45-
expect(component.find(DatePicker).prop('selected')).toEqual(
46-
new Date('11/12/2020')
47-
);
44+
expect(component.container.querySelector('input').value).toBe('11-12-20');
4845
});
4946
});

0 commit comments

Comments
 (0)