我想测试该组件是否接收道具,点击按钮会增加该值。
我知道如何在不使用HOC的情况下进行测试,但我不知道如何使用它进行测试。
示例:
// component
const Test = ({ value, increment }) => {
return (
<div>
{value}
<button onClick={increment}>Click</button>
</div>
);
}
Test.propTypes = {
name: PropTypes.string.isRequired,
increment: PropTypes.func.isRequired
}
// higher order component
const test = WrappedComponent => {
return class extends Component {
state = { value: 0 }
increment = () => {
this.setState({ value: this.state.value + 1 });
}
render() {
return (
<WrappedComponent
increment={this.increment}
value={this.state.value} />
);
}
}
}
// test
// Error: failed the prop type name
it("renders without crashing", () => {
const Container = test(Test);
shallow(<Container />);
});
答案 0 :(得分:0)
你可以&#34;转发&#34;道具:
const test = WrappedComponent => {
return class extends Component {
state = { value: 0 }
increment = () => {
this.setState({ value: this.state.value + 1 });
}
render() {
return (
<WrappedComponent
increment={this.increment}
value={this.state.value}
{...this.props}
/>
);
}
}
}
it("renders without crashing", () => {
const Container = test(Test);
shallow(<Container name="foo" />);
});