Skip to content

Commit 2ee6b58

Browse files
committed
测试更复杂的组件
1 parent 9fd76e2 commit 2ee6b58

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

package-lock.json

Lines changed: 105 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@testing-library/user-event": "^7.2.1",
99
"enzyme": "^3.11.0",
1010
"enzyme-adapter-react-16": "^1.15.3",
11+
"jest-enzyme": "^7.1.2",
1112
"react": "^16.13.1",
1213
"react-dom": "^16.13.1",
1314
"react-scripts": "3.4.3"

src/TodoList.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
3+
const ToDoList = (props) => {
4+
return (
5+
<ul>
6+
{props.tasks.map((taskName, index) => (
7+
<li key={index}>{taskName}</li>
8+
))}
9+
</ul>
10+
);
11+
};
12+
13+
export default ToDoList;

src/TodoList.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import { shallow } from 'enzyme';
3+
4+
import ToDoList from './ToDoList';
5+
6+
describe('ToDoList component', () => {
7+
describe('when provided with an empty array of tasks', () => {
8+
it('contains an empty <ul> element', () => {
9+
const toDoList = shallow(<ToDoList tasks={[]} />);
10+
expect(toDoList).toContainReact(<ul />);
11+
});
12+
13+
it('does not contain any <li> elements', () => {
14+
const toDoList = shallow(<ToDoList tasks={[]} />);
15+
expect(toDoList.find('li').length).toEqual(0);
16+
});
17+
});
18+
19+
describe('when provided with an array of tasks', () => {
20+
it('contains a matching number of <li> elements', () => {
21+
const tasks = ['Wash the dishes', 'Make the bed'];
22+
const toDoList = shallow(<ToDoList tasks={tasks} />);
23+
expect(toDoList.find('li').length).toEqual(tasks.length);
24+
});
25+
});
26+
});

src/setupTests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { configure } from 'enzyme';
22
import Adapter from 'enzyme-adapter-react-16';
3+
import 'jest-enzyme';
34

45
configure({ adapter: new Adapter() });

0 commit comments

Comments
 (0)