简体   繁体   中英

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.

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 be null or undefined "

You are creating a shallow render of <CreateTemplateResults /> , but never passing 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 .

shallow().setState

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM