繁体   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