[英]How to unit test the state of props using mocha chai and enzyme?
I'm trying to unit test onClick the state of the props in my component clear. 我正在尝试对组件清晰显示中的道具状态进行onClick单元测试。
I tried doing it this way: 我尝试这样做:
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)
But I get: 但我得到:
" TypeError: ShallowWrapper::state("statusMessage") requires that
state
not benull
orundefined
"“ TypeError:ShallowWrapper :: state(” statusMessage“)要求
state
不能为null
或undefined
”
You are creating a shallow render of <CreateTemplateResults />
, but never passing actualComponent.setState(nextState)
. 您正在创建
<CreateTemplateResults />
的浅表呈现器,但从未传递actualComponent.setState(nextState)
。 Therefore, when you are trying to access the state on your last line, it is throwing an error because state is null
/ undefined
. 因此,当您尝试访问最后一行的状态时,由于state为
null
/ undefined
,因此将引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.