最近开始使用 recompose ( https://github.com/acdlite/recompose )
我想知道我应该如何处理包含一些 hocs recompose 提供的单元测试组件?我喜欢如何用函数式方法替换整个类组件,但在单元测试方面完全不正确。
例如给定的列表组件
export const ListItem = toClass(pure(({ text }) => <li>{text}</li>));
const renderItems = R.map(t => <ListItem key={t} text={t} />);
export const ListComponent = toClass(({ todos, name, updateName, addTodo }) =>
<div>
<input onChange={updateName} value={name} />
<button onClick={() => addTodo(name)}>add todo</button>
<ul>
{renderItems(todos)}
</ul>
</div>
);
...
const List = compose(
withReducer("state", "dispatch", listReducer, props => ({
...initialState,
...props
})),
withHandlers({
updateName: ({ dispatch }) => e =>
dispatch({ type: "UPDATE_NAME", payload: e.target.value }),
addTodo: ({ dispatch, name }) => name =>
dispatch({ type: "ADD_TODO", payload: name })
}),
flattenProp("state")
)(ListComponent);
export default List;
如何使用给定的道具测试儿童的长度?我已经尝试过这样的事情,但它不起作用。
it("renders todos list", () => {
const component = shallow(<List todos={["todo1", "todo2", "todo3"]} />);
expect(component.instance().children).toHaveLength(3);
});