Is there any way to test onOk and onCancel ? I've already tried to test onCancel using fireEvent.keyDown but it doesn't work as I expect.
<>
<AntdModal
data-testid="modal-window"
width={583}
visible={visible}
onOk={() => setVisible(false)}
onCancel={() => setVisible(false)}
closable={false}
header={null}
footer={null}
>
<span className="modal-header">
{headerText}
</span>
{children}
</AntdModal>
</>
The main reason why I'd like to test this one It's because I need to cover my tests more than 80%...
*.test.js:
it('should modal disappear after cancel click', async () => {
const { container } = render(
<ModalWindow headerText="header" setVisible={jest.fn} visible>
<div>children</div>
</ModalWindow>,
);
const modal = screen.getByTestId('modal-window');
expect(modal).toBeInTheDocument();
fireEvent.keyDown(container, {
key: 'Escape',
code: 'Escape',
keyCode: 27,
charCode: 27,
});
await waitFor(() => {
expect(modal).not.toBeInTheDocument();
});
});
If it is only about code coverage (and in your case about code smell), I would suggest to declare () => setVisible(false) as constant. This will reduce a (possible bug generating) code duplicate and you will not need to test foreign libraries in your code.