score:1

Accepted answer

if you utilize a mock (jest.fn()), you can simply use tohavebeencalledwith.

so in your case:

it("sends back error callback", () => {
  const iserror = jest.fn();
  render(<component callback={iserror} />);
  const input = screen.getallbyrole("textbox")[0];
  fireevent.change(input, { target: { value: "100.98" } });
  expect(iserror).tohavebeencalledwith(false);
});

or use this alternative, especially if you have issue with the asynchronous nature of the code:

it("sends back error callback", (done) => {
  const iserror = (val) => {
    expect(val).tobe(false);
    done();
  };

  render(<component callback={iserror} />);
  const input = screen.getallbyrole("textbox")[0];
  fireevent.change(input, { target: { value: "100.98" } });
});

read more about async code: https://jestjs.io/docs/asynchronous.

if you have any questions to my answer/or the answer is not working, please get back to me in the comments 😊.


Related Query

More Query from same tag