react-truncate icon indicating copy to clipboard operation
react-truncate copied to clipboard

Jest: Can not test onTruncate method

Open Kazimirkas opened this issue 4 years ago • 3 comments

The following test fails with 0 value. Can you please help and mention, what am I doing wrong?

import React from "react";
import { mount } from "enzyme";
import Truncate from "react-truncate";

it("on Truncate", () => {
  const onTr = jest.fn();
  const layout = mount(<Truncate onTruncate={onTr}>Hello world</Truncate>);
  expect(onTr).toBeCalledTimes(1);
});

Kazimirkas avatar Feb 04 '21 09:02 Kazimirkas

I have the same problem too.

WORLDI avatar Aug 04 '21 10:08 WORLDI

how to resolve the problem?

WORLDI avatar Aug 04 '21 10:08 WORLDI

I found the way to test onTruncate, just add act(() => { jest.advanceTimersByTime(100); }); to your use case.

WORLDI avatar Aug 06 '21 03:08 WORLDI