import { render, screen } from '@testing-library/react'; import { userEvent } from '@testing-library/user-event'; import Tooltip from '.'; describe('Tooltip Component', () => { it('initially is hidden (test with toBeInTheDocument)', () => { render( Hover me , ); const tooltip = screen.getByRole('tooltip', { hidden: true }); expect(tooltip).toBeInTheDocument(); expect(tooltip).toHaveAttribute('aria-hidden', 'true'); }); it('should render the tooltip label when closed', async () => { render( Hover me , ); const triggerElement = screen.getByText('Hover me'); const tooltip = screen.getByRole('tooltip', { hidden: true }); expect(tooltip).toHaveAttribute('aria-hidden', 'true'); await userEvent.hover(triggerElement); expect(tooltip).toHaveAttribute('aria-hidden', 'false'); }); it('should display the tooltip on mouse hover', async () => { render( Hover over me! , ); const triggerElement = screen.getByText('Hover over me!'); await userEvent.hover(triggerElement); const tooltipElement = screen.queryByText('Test Tooltip'); expect(tooltipElement).toBeVisible(); }); it('should hide the tooltip on mouse out', async () => { render( Hover me , ); const triggerElement = screen.getByText('Hover me'); await userEvent.hover(triggerElement); let tooltipElement = screen.getByText('Test Tooltip'); expect(tooltipElement).toBeVisible(); await userEvent.unhover(triggerElement); tooltipElement = screen.getByRole('tooltip', { hidden: true }); expect(tooltipElement).toHaveAttribute('aria-hidden', 'true'); }); it('should display the tooltip on focus', async () => { render( Focus me , ); const triggerElement = screen.getByText('Focus me'); await userEvent.tab(); const tooltipElement = screen.getByText('Test Tooltip'); expect(tooltipElement).toBeVisible(); }); it('should hide the tooltip on blur', async () => { render( Focus me , ); const triggerElement = screen.getByText('Focus me'); await userEvent.tab(); let tooltipElement = screen.getByRole('tooltip', { hidden: true }); expect(tooltipElement).toBeVisible(); await userEvent.tab({ shift: true }); // Blur the element tooltipElement = screen.getByRole('tooltip', { hidden: true }); expect(tooltipElement).toHaveAttribute('aria-hidden', 'true'); }); it('should display the tooltip when the "children" prop is a React element', async () => { render( , ); const triggerElement = screen.getByText('Click me'); await userEvent.hover(triggerElement); const tooltipElement = screen.getByRole('tooltip', { hidden: true }); expect(tooltipElement).toBeVisible(); }); });