|
| 1 | +import { act, render, waitFor } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | + |
| 4 | +import React from 'react'; |
| 5 | + |
| 6 | +import { Tooltip } from './Tooltip'; |
| 7 | +import { TooltipPlacement, TooltipTrigger } from './Tooltip.types'; |
| 8 | + |
| 9 | +describe('Tooltip', () => { |
| 10 | + beforeEach(() => { |
| 11 | + jest.useFakeTimers(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + jest.useRealTimers(); |
| 16 | + }); |
| 17 | + |
| 18 | + test('should render a tooltip on hover in and hide on hover out', () => { |
| 19 | + const { getByRole, queryByText } = render( |
| 20 | + <Tooltip children={<button>Text</button>} content={<>Tooltip</>} />, |
| 21 | + ); |
| 22 | + const button = getByRole('button'); |
| 23 | + userEvent.hover(button); |
| 24 | + const tooltip = queryByText('Tooltip'); |
| 25 | + expect(tooltip).toBeInTheDocument(); |
| 26 | + userEvent.unhover(button); |
| 27 | + act(() => { |
| 28 | + jest.runAllTimers(); |
| 29 | + }); |
| 30 | + expect(tooltip).not.toBeInTheDocument(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should render a tooltip on focus in and hide on focus out', async () => { |
| 34 | + const { getByRole, queryByText } = render( |
| 35 | + <Tooltip |
| 36 | + trigger={TooltipTrigger.focus} |
| 37 | + children={<button>Text</button>} |
| 38 | + content={<>Tooltip</>} |
| 39 | + />, |
| 40 | + ); |
| 41 | + const button = getByRole('button'); |
| 42 | + await waitFor(() => button.focus()); |
| 43 | + const tooltip = queryByText('Tooltip'); |
| 44 | + expect(tooltip).toBeInTheDocument(); |
| 45 | + await waitFor(() => button.blur()); |
| 46 | + act(() => { |
| 47 | + jest.runAllTimers(); |
| 48 | + }); |
| 49 | + expect(tooltip).not.toBeInTheDocument(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should render a tooltip on click and hide on a second click', () => { |
| 53 | + const { getByRole, queryByText } = render( |
| 54 | + <Tooltip |
| 55 | + trigger={TooltipTrigger.click} |
| 56 | + children={<button>Text</button>} |
| 57 | + content={<>Tooltip</>} |
| 58 | + />, |
| 59 | + ); |
| 60 | + const button = getByRole('button'); |
| 61 | + userEvent.click(button); |
| 62 | + const tooltip = queryByText('Tooltip'); |
| 63 | + expect(tooltip).toBeInTheDocument(); |
| 64 | + userEvent.click(button); |
| 65 | + act(() => { |
| 66 | + jest.runAllTimers(); |
| 67 | + }); |
| 68 | + expect(tooltip).not.toBeInTheDocument(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should not render a tooltip if disabled is true', () => { |
| 72 | + const { getByRole, queryByText } = render( |
| 73 | + <Tooltip |
| 74 | + disabled |
| 75 | + children={<button>Text</button>} |
| 76 | + content={<>Tooltip</>} |
| 77 | + />, |
| 78 | + ); |
| 79 | + userEvent.click(getByRole('button')); |
| 80 | + expect(queryByText('Tooltip')).not.toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should render a tooltip with a bottom placement', () => { |
| 84 | + const { getByRole, getByText } = render( |
| 85 | + <Tooltip |
| 86 | + children={<button>Text</button>} |
| 87 | + placement={TooltipPlacement.bottom} |
| 88 | + content={<>Tooltip</>} |
| 89 | + />, |
| 90 | + ); |
| 91 | + userEvent.hover(getByRole('button')); |
| 92 | + const tooltip = getByText('Tooltip'); |
| 93 | + const classes = tooltip.getAttribute('class'); |
| 94 | + expect(classes).toContain('bottom'); |
| 95 | + }); |
| 96 | +}); |
0 commit comments