Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions src/@next/Tooltip/Tooltip.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import * as React from 'react';
import '@testing-library/jest-dom/extend-expect';
import { act, fireEvent, render } from '@testing-library/react';

import { Tooltip } from './Tooltip';

beforeAll(() => {
Object.defineProperty(global, 'DOMRect', {
configurable: true,
value: class {
bottom: number;
height: number;
left: number;
right: number;
top: number;
width: number;
x: number;
y: number;

constructor(x = 0, y = 0, width = 0, height = 0) {
this.bottom = y + height;
this.height = height;
this.left = x;
this.right = x + width;
this.top = y;
this.width = width;
this.x = x;
this.y = y;
}
},
});
});

beforeEach(() => {
jest.useFakeTimers();
const portalContainer = document.createElement('div');
portalContainer.id = 'glints-portal-container';
document.body.appendChild(portalContainer);
jest
.spyOn(Element.prototype, 'getClientRects')
.mockReturnValue([new DOMRect(0, 0, 20, 20)] as unknown as DOMRectList);
});

afterEach(() => {
jest.useRealTimers();
jest.restoreAllMocks();
document.getElementById('glints-portal-container')?.remove();
});

describe('<Tooltip />', () => {
test('stays open after a rapid leave and re-entry', () => {
const { getByText, queryByText } = render(
<Tooltip content="mock text">
<span>mock content</span>
</Tooltip>
);
const trigger = getByText('mock content');

fireEvent.mouseEnter(trigger);
fireEvent.mouseLeave(trigger);
fireEvent.mouseEnter(trigger);

act(() => {
jest.advanceTimersByTime(370);
});

expect(queryByText('mock text')).toBeInTheDocument();
});
});
21 changes: 18 additions & 3 deletions src/@next/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,25 @@ export const Tooltip = ({
content
);

const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const [animate, setAnimate] = useState<boolean>(false);

useEffect(
() => () => {
if (timeoutRef.current) clearTimeout(timeoutRef.current);
},
[]
);

const handleMouseEnter = () => {
if (!clickable) setIsActive(true);
if (!clickable) {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
setAnimate(false);
setIsActive(true);
}
};
const handleMouseLeave = () => {
if (!clickable) {
Expand All @@ -270,8 +287,6 @@ export const Tooltip = ({
}
};

const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const [animate, setAnimate] = useState<boolean>(false);
const handleAnimation = () => {
// if you click during the tooltip's lifespan, it should reset the timeout
if (timeoutRef.current) {
Expand Down
Loading