import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary'; import {Hovercard} from 'sentry/components/hovercard'; describe('Hovercard', () => { beforeEach(() => { jest.useFakeTimers(); jest.clearAllMocks(); }); afterEach(() => { jest.useRealTimers(); }); it('Displays card', async () => { render( Hovercard Trigger ); userEvent.hover(screen.getByText('Hovercard Trigger')); expect(await screen.findByText(/Hovercard Body/)).toBeInTheDocument(); expect(await screen.findByText(/Hovercard Header/)).toBeInTheDocument(); }); it('Does not display card', () => { render( Hovercard Trigger ); userEvent.hover(screen.getByText('Hovercard Trigger')); act(() => void jest.runAllTimers()); expect(screen.queryByText(/Hovercard Body/)).not.toBeInTheDocument(); expect(screen.queryByText(/Hovercard Header/)).not.toBeInTheDocument(); }); it('Always displays card', () => { render( Hovercard Trigger ); expect(screen.getByText(/Hovercard Body/)).toBeInTheDocument(); expect(screen.getByText(/Hovercard Header/)).toBeInTheDocument(); }); it('Respects displayTimeout displays card', async () => { const DISPLAY_TIMEOUT = 100; render( Hovercard Trigger ); userEvent.hover(screen.getByText('Hovercard Trigger')); act(() => void jest.advanceTimersByTime(DISPLAY_TIMEOUT - 1)); expect(screen.queryByText(/Hovercard Body/)).not.toBeInTheDocument(); expect(screen.queryByText(/Hovercard Header/)).not.toBeInTheDocument(); expect(await screen.findByText(/Hovercard Body/)).toBeInTheDocument(); expect(await screen.findByText(/Hovercard Header/)).toBeInTheDocument(); }); it('Doesnt leak timeout', () => { const DISPLAY_TIMEOUT = 100; render( Hovercard Trigger ); userEvent.hover(screen.getByText('Hovercard Trigger')); act(() => void jest.advanceTimersByTime(DISPLAY_TIMEOUT - 1)); expect(screen.queryByText(/Hovercard Body/)).not.toBeInTheDocument(); expect(screen.queryByText(/Hovercard Header/)).not.toBeInTheDocument(); userEvent.unhover(screen.getByText('Hovercard Trigger')); act(() => void jest.advanceTimersByTime(1)); expect(screen.queryByText(/Hovercard Body/)).not.toBeInTheDocument(); expect(screen.queryByText(/Hovercard Header/)).not.toBeInTheDocument(); }); });