簡體   English   中英

JSON.parse 位置 0 處的 JSON 中的意外標記 u

[英]Unexpected token u in JSON at position 0 at JSON.parse

我正在嘗試為包含 JSON.parse 的提交函數編寫 jest 測試用例。 下面是它的代碼和測試用例。

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);
    });

參考這張圖片 如果handleFormSubmit涉及JSON.parse()需要在 event 的值中發送字符串化的 JSON

嘗試將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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM