使用 [event.target.name] 时 React 笑话测试停止工作
React jest test stops working when using [event.target.name]
有以下比赛
constructor(props) {
super(props);
this.state = {
comment: ''
}
}
handleChange = event => {
this.setState({ [event.target.name]: event.target.value })
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<h4>Add a Comment</h4>
<textarea name="comment" onChange={this.handleChange} value={this.state.comment} />
<div>
<button>Submit Comment</button>
</div>
</form>
)
}
写下面的就不让我过关了
it('has a text area where the users can type in', () => {
wrapped.find('textarea').simulate('change', {
target: {value: 'new comment'}
})
wrapped.update()
expect(wrapped.find('textarea').prop('value')).toEqual('new comment')
})
但是当使用下面的 handlechange 时同样的测试通过了
handleChange = event => {
this.setState({ comment: event.target.value })
}
如何在使用 [event.target.name]
时通过此测试
您还需要在 event.target
对象中具有名称 属性
const event = {target: {name: "somename", value: "somevalue"}};
试试下面的方法,
it("should respond to name change", _ => {
const event = {target: {name: "somename", value: "somevalue"}};
const wrapper = mount(
<Component />
);
const handleChangeSpy = jest.spy(wrapper.instance(), "handleChange");
wrapper.update();
wrapper.ref('somename').simulate('change', event);
expect(handleChangeSpy.calledOnce).to.equal(true);
});
或者如果浅渲染,
it("should respond to name change", _ => {
const event = {target: {name: "comment", value: "somevalue"}};
const wrapper = shallow(<Component />);
const myTextArea = wrapper.find(#comment);
myTextArea.simulate('change', event);
expect(...);
});
有以下比赛
constructor(props) {
super(props);
this.state = {
comment: ''
}
}
handleChange = event => {
this.setState({ [event.target.name]: event.target.value })
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<h4>Add a Comment</h4>
<textarea name="comment" onChange={this.handleChange} value={this.state.comment} />
<div>
<button>Submit Comment</button>
</div>
</form>
)
}
写下面的就不让我过关了
it('has a text area where the users can type in', () => {
wrapped.find('textarea').simulate('change', {
target: {value: 'new comment'}
})
wrapped.update()
expect(wrapped.find('textarea').prop('value')).toEqual('new comment')
})
但是当使用下面的 handlechange 时同样的测试通过了
handleChange = event => {
this.setState({ comment: event.target.value })
}
如何在使用 [event.target.name]
时通过此测试您还需要在 event.target
对象中具有名称 属性
const event = {target: {name: "somename", value: "somevalue"}};
试试下面的方法,
it("should respond to name change", _ => {
const event = {target: {name: "somename", value: "somevalue"}};
const wrapper = mount(
<Component />
);
const handleChangeSpy = jest.spy(wrapper.instance(), "handleChange");
wrapper.update();
wrapper.ref('somename').simulate('change', event);
expect(handleChangeSpy.calledOnce).to.equal(true);
});
或者如果浅渲染,
it("should respond to name change", _ => {
const event = {target: {name: "comment", value: "somevalue"}};
const wrapper = shallow(<Component />);
const myTextArea = wrapper.find(#comment);
myTextArea.simulate('change', event);
expect(...);
});