react-tooltip
react-tooltip copied to clipboard
UnitTesting: Getting TypeError: _reactTooltip.default.hide is not a function
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.
Was this solved? I have the same issue with the static methods
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 />; }
}),
}));