在测试我的 TodoList 组件时,我不断收到此错误:
debug.html:38
Invariant Violation:
元素类型无效:
期望字符串(用于内置组件)或类/函数(用于复合组件)但得到:对象。
我不确定是什么原因造成的。谁能引导我走向正确的方向?
TodoList.js:
import React from 'react';
import Todo from 'Todo';
class TodoList extends React.Component {
renderTodos(todos) {
return todos.map(todo => {
return <Todo key={todo.id} {...todo} />;
});
}
render() {
const { todos } = this.props;
return (
<div>
{this.renderTodos(todos)}
</div>
);
}
}
export default TodoList;
TodoList.test.js:
const React = require('react');
const ReactDOM = require('react-dom');
const TestUtils = require('react-addons-test-utils');
const expect = require('expect');
const $ = require('jQuery');
const TodoList = require('TodoList');
const Todo = require('Todo');
describe('TodoList', () => {
it('should exist', () => {
expect(TodoList).toExist();
});
it('should render one Todo component for each todo item', () => {
const todos = [{
id: 1,
text: "Do something"
}, {
id: 2,
text: "Check mail"
}];
const todoList = TestUtils.renderIntoDocument(<TodoList todos={todos} />);
const todoComponents = TestUtils.scryRenderedComponentsWithType(todoList, Todo);
expect(todoComponents.length).toBe(todos.length);
});
});
链接到源代码:链接到源代码