I have a conditional rendering block in my React ponent which is defined as:
{(props.email.primary.isPending) ?
<PendingComponent emailAddress={props.email.primary.pendingEmail} />
:
<SecondaryEmailContact
state={props.email.secondary}
retrieveInputData={props.retrieveInputData}
handleSecondaryEmailToggle={props.handleSecondaryEmailToggle}
handleDelete={props.handleDelete}
handleSubmitContact={props.handleSubmitContact}
refs={props.refs}
/>
}
I have written a test case as below:
it('renders the EditEmailContact ponent', () => {
wrapper=mount(<EditEmailContact
email={emailState}
handleSecondaryEmailToggle={handleSecondaryEmailToggleFn}
retrieveInputData={retrieveInputDataFn}
handleDelete={handleDeleteFn}
handleSubmitContact={handleSubmitContactFn} />);
});
});
So, in my test result it shows the line where the statement for conditional rendering is defined is not tested. So, how do I test the conditional rendering?
I have a conditional rendering block in my React ponent which is defined as:
{(props.email.primary.isPending) ?
<PendingComponent emailAddress={props.email.primary.pendingEmail} />
:
<SecondaryEmailContact
state={props.email.secondary}
retrieveInputData={props.retrieveInputData}
handleSecondaryEmailToggle={props.handleSecondaryEmailToggle}
handleDelete={props.handleDelete}
handleSubmitContact={props.handleSubmitContact}
refs={props.refs}
/>
}
I have written a test case as below:
it('renders the EditEmailContact ponent', () => {
wrapper=mount(<EditEmailContact
email={emailState}
handleSecondaryEmailToggle={handleSecondaryEmailToggleFn}
retrieveInputData={retrieveInputDataFn}
handleDelete={handleDeleteFn}
handleSubmitContact={handleSubmitContactFn} />);
});
});
So, in my test result it shows the line where the statement for conditional rendering is defined is not tested. So, how do I test the conditional rendering?
Share Improve this question edited Nov 20, 2018 at 9:25 GibboK 73.9k147 gold badges451 silver badges672 bronze badges asked Nov 18, 2018 at 11:09 pranamipranami 1,4155 gold badges26 silver badges47 bronze badges1 Answer
Reset to default 11You could create two different test cases passing props to your ponent. For instance:
const yourProps = {
email: {
primary: {
isPending: true // create test cases passing a different value
},
},
}
const ponent = mount(<YourComponent {...yourProps} />)