簡體   English   中英

如何使用摩卡茶和酶對道具的狀態進行單元測試?

[英]How to unit test the state of props using mocha chai and enzyme?

我正在嘗試對組件清晰顯示中的道具狀態進行onClick單元測試。

我嘗試這樣做:

props = {
    attributeTableData: data,
    clearMessage: onClickMethod,
    reset: () => { },
    resetAttributeTable: () => { },
    statusMessage: {
        messageType: 'message-success',
        userMessage: 'Template has been saved successfully. Please wait …see your results display with the latest'
    },
    submitTemplateCreationStatus: () => { },
    templateAttributeFormData: () => { },
    templateFormSubmission: true,
    templateAttributeFormSubmission: true,
    templateFormData: () => { },
    userRoles: new Set(['admin'])
};
let emptyStatusMessage = {};
actualComponent = shallow(<CreateTemplateResults { ...props } />);
actualComponent.instance().resetForms();
expect(onClickMethod.called).to.be.true;
expect(actualComponent.state('statusMessage')).to.eql(emptyStatusMessage)

但我得到:

“ TypeError:ShallowWrapper :: state(” statusMessage“)要求state不能為nullundefined

您正在創建<CreateTemplateResults />的淺表呈現器,但從未傳遞actualComponent.setState(nextState) 因此,當您嘗試訪問最后一行的狀態時,由於state為null / undefined ,因此將引發錯誤。

淺()。的setState

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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