Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

319
Views
Test onOk / onCancel | Ant Design [antd]

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%...

coverage result

*.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();
    });
  });
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!