[英]Unexpected token u in JSON at position 0 at JSON.parse
I am trying to write jest test case for submit function which has JSON.parse involed in it.
我正在尝试为包含 JSON.parse 的提交函数编写 jest 测试用例。 Below is the code and test case for it.
下面是它的代码和测试用例。
handleFormSubmit = (e) => {
e.preventDefault();
let requestData = JSON.parse(JSON.stringify(this.props.tempAddRequest));
if (e.target[1].value.length === 0) {
this.setState({ rrNumberFeedback: true, rrNumberErr: FIELD_ERROR });
}
}
it("should check if rr is null on clicking submit button in modal", () => {
const instance = wrapper.instance();
const e = {
target: [{ name: "branchNumber", value: "" }, { name: "rrNumber", value: "" }, { name: "crdNumber", value: "1234" }],
preventDefault: () => { }
}
instance.handleFormSubmit(e);
expect(wrapper.state('rrNumberErr')).toEqual("E");
expect(wrapper.state('rrNumberFeedback')).toEqual(true);
});
If the
handleFormSubmit
involves JSON.parse()
you need to send the stringified JSON in the value of event如果
handleFormSubmit
涉及JSON.parse()
需要在 event 的值中发送字符串化的 JSON
try changing event
as尝试将
event
更改为
const e = {
target: JSON.stringify([{ name: "branchNumber", value: "" }, { name: "rrNumber", value: "" }, { name: "crdNumber", value: "1234" }]),
preventDefault: () => { }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.