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

UnitTesting: Getting TypeError: _reactTooltip.default.hide is not a function

Open ghost opened this issue 4 years ago • 2 comments

I have created a component using ReactTooltip library, below is the source code:

import * as React from 'react';
import ReactTooltip from 'react-tooltip';

export const TooltipText = () => {
  return (
    <div>
      <span
        aria-label="tootipText"
        onKeyDown={ evt => {
          const trgt: any = evt.target;
          ReactTooltip.hide (trgt);
        }}
      >
      </span>
      <ReactTooltip id="test" place="top" html="<div>Hello<div>" />
    </div>
  );
};

I am writing unit test case using jest and enzyme, below is the source code:

import { shallow } from 'enzyme';
import * as Enzyme from 'enzyme';
import ReactSixteenAdapter from 'enzyme-adapter-react-16';
import * as React from 'react';
import ReactTooltip from 'react-tooltip';

 jest.mock('react-tooltip', () => jest.fn(({ children }) => children({}, {})));
  beforeAll(() => {
    Enzyme.configure({ adapter: new ReactSixteenAdapter() });
  });
describe( "toolrip test case", () => {
     it('triggers oKeyDown', () => {
     jest.unmock('react-tooltip');

    component = shallow(
      <TooltipText id="1" tipText="Some tooltip text">
        {jsxElement}
      </TooltipText>
    );
    component
      .find('[data-testid="tooltip-text"]')
      .first()
      .simulate('keyDown', { target: <span /> });
     expect(component).toMatchSnapshot();
  });
}

I am having hard time writing test case for "onKeyDown" event as I am getting this error: " _reactTooltip.default.hide is not a function". Need help to fix this issue.

ghost avatar Nov 10 '21 12:11 ghost

Was this solved? I have the same issue with the static methods

stefann01 avatar Apr 18 '22 13:04 stefann01

I managed to mock the entire component including static method like this:

jest.mock('react-tooltip', () => ({
  __esModule: true,
  default: () => (class {
    static rebuild() {}

    render() { return <div />; }
  }),
}));

stefann01 avatar Apr 19 '22 07:04 stefann01