Skip to content

Commit 85d5d59

Browse files
author
chenzhe
committed
测试组件的 Props
1 parent 2ee6b58 commit 85d5d59

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/Task.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
3+
const Task = (props) => {
4+
return <li>{props.name}</li>;
5+
};
6+
7+
export default Task;

src/TodoList.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React from 'react';
2+
import Task from './Task';
23

34
const ToDoList = (props) => {
45
return (
56
<ul>
6-
{props.tasks.map((taskName, index) => (
7-
<li key={index}>{taskName}</li>
7+
{props.tasks.map((task) => (
8+
<Task key={task.id} id={task.id} name={task.name} />
89
))}
910
</ul>
1011
);

src/TodoList.test.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,25 @@ describe('ToDoList component', () => {
1717
});
1818

1919
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);
20+
const tasks = [
21+
{
22+
id: 0,
23+
name: 'Wash the dishes',
24+
},
25+
{
26+
id: 1,
27+
name: 'Make the bed',
28+
},
29+
];
30+
31+
it('passes them to the Task components', () => {
32+
const toDoListInstance = shallow(<ToDoList tasks={tasks} />);
33+
34+
toDoListInstance.find('Task').forEach((taskInstance) => {
35+
const taskProps = taskInstance.props();
36+
const matchingTask = tasks.find((task) => task.id === taskProps.id);
37+
expect(taskProps.name).toBe(matchingTask.name);
38+
});
2439
});
2540
});
2641
});

0 commit comments

Comments
 (0)