Skip to content

Commit 7352979

Browse files
authored
day-25 code snippet updates
1 parent 52085dd commit 7352979

File tree

1 file changed

+20
-46
lines changed

1 file changed

+20
-46
lines changed

day-25/post.md

+20-46
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ imagesDir: /assets/images/series/30-days-of-react/day-25
1616
includeFile: ./../_params.yaml
1717
---
1818

19-
Yesterday we used the `react-addons-test-utils` library to write our first test against the `Timeline` component. However, this library is fairly low-level and can be a bit cumbersome to use. [Enzyme](http://airbnb.io/enzyme/) is a testing utility library released and maintained by the [AirBnb](http://airbnb.io) team and it offers a nicer, higher-level API for dealing with React components under test.
19+
Yesterday we used the `react-dom/test-utils` library to write our first test against the `Timeline` component. However, this library is fairly low-level and can be a bit cumbersome to use. [Enzyme](http://airbnb.io/enzyme/) is a testing utility library released and maintained by the [AirBnb](http://airbnb.io) team and it offers a nicer, higher-level API for dealing with React components under test.
2020

2121
We're testing against our `<Timeline />` component:
2222

@@ -30,7 +30,7 @@ Yesterday, we wrote our first test as the following:
3030

3131
```javascript
3232
import React from 'react';
33-
import TestUtils from 'react-addons-test-utils';
33+
import TestUtils from "react-dom/test-utils";
3434

3535
import Timeline from '../Timeline';
3636

@@ -51,6 +51,16 @@ Rather than testing the complete component tree with Enzyme, we can test just th
5151

5252
Enzyme makes shallow rendering super easy. We'll use the `shallow` function exported by Enzyme to mount our component.
5353

54+
Let's first configure `enzyme` use the adapter that makes it compatible with React version 16. Create `src/setupTests.js` and add the following:
55+
56+
```javascript
57+
import { configure } from "enzyme";
58+
import Adapter from "enzyme-adapter-react-16";
59+
60+
configure({ adapter: new Adapter() });
61+
62+
```
63+
5464
Let's update the `src/components/Timeline/__tests__/Timeline-test.js` file to include the `shallow` function from `enzyme`:
5565

5666
```javascript
@@ -64,7 +74,7 @@ describe('Timeline', () => {
6474
})
6575
```
6676

67-
> Shallow rendering is supported by `react-addons-test-utils` as well. In fact, Enzyme just wraps this functionality. While we didn't use shallow rendering yesterday, if we were to use it would look like this:
77+
> Shallow rendering is supported by `react-dom/test-utils` as well. In fact, Enzyme just wraps this functionality. While we didn't use shallow rendering yesterday, if we were to use it would look like this:
6878
>
6979
> ```javascript
7080
> const renderer = ReactTestUtils.createRenderer();
@@ -84,41 +94,11 @@ import Timeline from '../Timeline';
8494
8595
describe('Timeline', () => {
8696
let wrapper;
87-
88-
it('wraps content in a div with .notificationsFrame class', () => {
97+
98+
it("wraps content in a div with .notificationsFrame class", () => {
8999
wrapper = shallow(<Timeline />);
90-
expect(wrapper.find('.notificationsFrame').length).toEqual(1);
100+
expect(wrapper.find(".notificationsFrame").length).toEqual(1);
91101
});
92-
93-
it('has a title of Timeline', () => {
94-
wrapper = mount(<Timeline />)
95-
expect(wrapper.find('.title').text()).toBe("Timeline")
96-
})
97-
98-
describe('search button', () => {
99-
let search;
100-
beforeEach(() => wrapper = mount(<Timeline />))
101-
beforeEach(() => search = wrapper.find('input.searchInput'))
102-
103-
it('starts out hidden', () => {
104-
expect(search.hasClass('active')).toBeFalsy()
105-
})
106-
it('becomes visible after being clicked on', () => {
107-
const icon = wrapper.find('.searchIcon')
108-
icon.simulate('click')
109-
expect(search.hasClass('active')).toBeTruthy()
110-
})
111-
})
112-
113-
describe('status updates', () => {
114-
it('has 4 status updates at minimum', () => {
115-
wrapper = shallow(<Timeline />)
116-
expect(
117-
wrapper.find('ActivityItem').length
118-
).toBeGreaterThan(3)
119-
})
120-
})
121-
122102
})
123103
```
124104
@@ -216,9 +196,7 @@ describe('Timeline', () => {
216196
let wrapper;
217197
// ...
218198
describe('search button', () => {
219-
let search;
220199
beforeEach(() => wrapper = mount(<Timeline />))
221-
beforeEach(() => search = wrapper.find('input.searchInput'))
222200
// ...
223201
})
224202
})
@@ -231,12 +209,10 @@ describe('Timeline', () => {
231209
let wrapper;
232210
// ...
233211
describe('search button', () => {
234-
let search;
235212
beforeEach(() => wrapper = mount(<Timeline />))
236-
beforeEach(() => search = wrapper.find('input.searchInput'))
237213

238214
it('starts out hidden', () => {
239-
expect(search.hasClass('active')).toBeFalsy()
215+
expect(wrapper.find("input.searchInput").hasClass("active")).toBeFalsy();
240216
})
241217
it('becomes visible after being clicked on')
242218
// ...
@@ -269,7 +245,7 @@ Now we can set an expectation that the `search` component has the `active` class
269245
it('becomes visible after being clicked on', () => {
270246
const icon = wrapper.find('.searchIcon')
271247
icon.simulate('click')
272-
expect(search.hasClass('active')).toBeTruthy()
248+
expect(wrapper.find("input.searchInput").hasClass("active")).toBeTruthy();
273249
})
274250
```
275251

@@ -319,17 +295,15 @@ describe('Timeline', () => {
319295
})
320296

321297
describe('search button', () => {
322-
let search;
323298
beforeEach(() => wrapper = mount(<Timeline />))
324-
beforeEach(() => search = wrapper.find('input.searchInput'))
325299

326300
it('starts out hidden', () => {
327-
expect(search.hasClass('active')).toBeFalsy()
301+
expect(wrapper.find("input.searchInput").hasClass("active")).toBeFalsy();
328302
})
329303
it('becomes visible after being clicked on', () => {
330304
const icon = wrapper.find('.searchIcon')
331305
icon.simulate('click')
332-
expect(search.hasClass('active')).toBeTruthy()
306+
expect(wrapper.find("input.searchInput").hasClass("active")).toBeTruthy();
333307
})
334308
})
335309

0 commit comments

Comments
 (0)