աӄա 2e49e08315
fix: lazyMotion forwardRef issue (#2622)
* fix(ripple): lazyMotion forwardRef issue in Ripple

* feat(dropdown): add test case for LazyMotion React.forwardRef issue

* refactor(dropdown): revise the test title

* feat(modal): include console error check in modal test

* feat(popover): add "should not throw error when clicking trigger button" test

* feat(accordion): add test for lazy motion issue

* feat(navbar): add test for lazy motion issue

* feat(tabs): add test for lazy motion issue

* feat(tooltip): add test for lazy motion issue

* refactor(dropdown): remove unnecessary async

* refactor(test): move spy outside and trigger clearAllMocks after each test
2024-04-02 11:01:17 -03:00

85 lines
2.2 KiB
TypeScript

import * as React from "react";
import {render, fireEvent, act} from "@testing-library/react";
import {Button} from "@nextui-org/button";
import {Tooltip} from "../src";
// e.g. console.error Warning: Function components cannot be given refs.
// Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
const spy = jest.spyOn(console, "error").mockImplementation(() => {});
describe("Tooltip", () => {
afterEach(() => {
jest.clearAllMocks();
});
it("should throw error if no children is passed", () => {
const spy = jest.spyOn(console, "warn").mockImplementation(() => {});
render(<Tooltip content="tooltip" />);
expect(spy).toHaveBeenCalled();
});
it("should render correctly", () => {
const wrapper = render(
<Tooltip content="tooltip">
<Button>Trigger</Button>
</Tooltip>,
);
expect(() => wrapper.unmount()).not.toThrow();
expect(spy).toBeCalledTimes(0);
});
it("ref should be forwarded", () => {
const ref = React.createRef<HTMLDivElement>();
render(
<Tooltip ref={ref} defaultOpen content="tooltip">
<Button>Trigger</Button>
</Tooltip>,
);
expect(ref.current).not.toBeNull();
});
it("should hide the tooltip when pressing the escape key", async () => {
const onClose = jest.fn();
const wrapper = render(
<Tooltip defaultOpen content={<p data-testid="content-test">tooltip</p>} onClose={onClose}>
<Button>Trigger</Button>
</Tooltip>,
);
const content = wrapper.getByTestId("content-test");
await act(async () => {
await fireEvent.keyDown(content, {key: "Escape"});
expect(onClose).toHaveBeenCalledTimes(1);
});
});
it("should still hide the tooltip when pressing the escape key if isDismissable is false", () => {
const onClose = jest.fn();
const wrapper = render(
<Tooltip
defaultOpen
content={<p data-testid="content-test">tooltip</p>}
isDismissable={false}
onClose={onClose}
>
<Button>Trigger</Button>
</Tooltip>,
);
const content = wrapper.getByTestId("content-test");
fireEvent.keyDown(content, {key: "Escape"});
expect(onClose).toHaveBeenCalledTimes(1);
});
});