Answer the question
In order to leave comments, you need to log in
How to test modal window closing, react?
Good afternoon! Can you please tell me how to test the handleCloseModal method in the component?
The component itself
const TextModal = ({
isOpen,
isLoading,
onClose,
handleSubmit,
onSubmit,
update,
}) => {
const handleCloseModal = () => {
onClose();
update()
};
return (
<Modal
isOpen={isOpen}
handleClose={handleCloseModal}
>
<Form onSubmit={handleSubmit(onSubmit)}>
<ModalBody>
<Field
name="text"
component={FormGroupField}
label="Text"
/>
</ModalBody>
<ModalFooter>
<FormSubmit
className="button is-primary"
isLoading={isLoading}
>
Save
</FormSubmit>
</ModalFooter>
</Form>
</Modal>
);
};
it('test closeModal', () => {
const props = {
isOpen: true,
isLoading: false,
change: jest.fn(),
onSubmit: jest.fn(),
onClose: jest.fn(),
handleSubmit: jest.fn(),
};
const enzymeWrapper = shallow(<TextModal {...props} />);
enzymeWrapper.instance().handleCloseModal();
expect(props.onClose).toHaveBeenCalledTimes(1);
expect(props.update).toHaveBeenCalledTimes(1);
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question