|
| 1 | +import React from 'react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { render, screen } from '../test-utils'; |
| 4 | +import { Tooltip } from './Tooltip'; |
| 5 | + |
| 6 | +describe('Tooltip', () => { |
| 7 | + it('renders the child element', () => { |
| 8 | + render( |
| 9 | + <Tooltip content="This is a tooltip"> |
| 10 | + <button>Hover me</button> |
| 11 | + </Tooltip> |
| 12 | + ); |
| 13 | + expect(screen.getByRole('button')).toBeInTheDocument(); |
| 14 | + expect(screen.getByText('Hover me')).toBeInTheDocument(); |
| 15 | + }); |
| 16 | + |
| 17 | + it('does not show the tooltip when the user is not hovering over the element', () => { |
| 18 | + render( |
| 19 | + <Tooltip content="Tooltip text"> |
| 20 | + <button>Button</button> |
| 21 | + </Tooltip> |
| 22 | + ); |
| 23 | + |
| 24 | + const button = screen.getByRole('button'); |
| 25 | + expect(button).toBeInTheDocument(); |
| 26 | + expect(button).not.toHaveClass('tooltipped-visible'); |
| 27 | + }); |
| 28 | + |
| 29 | + it('shows the tooltip if the user hovers over the element', async () => { |
| 30 | + const user = userEvent.setup(); |
| 31 | + render( |
| 32 | + <Tooltip content="Tooltip text"> |
| 33 | + <button>Button</button> |
| 34 | + </Tooltip> |
| 35 | + ); |
| 36 | + |
| 37 | + const button = screen.getByRole('button'); |
| 38 | + await user.hover(button); |
| 39 | + |
| 40 | + expect(button).toHaveClass('tooltipped'); |
| 41 | + expect(button).toHaveAttribute('aria-label', 'Tooltip text'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('adds the aria-label with tooltip content to the child element', () => { |
| 45 | + render( |
| 46 | + <Tooltip content="Save your changes"> |
| 47 | + <button>Save</button> |
| 48 | + </Tooltip> |
| 49 | + ); |
| 50 | + |
| 51 | + const button = screen.getByRole('button'); |
| 52 | + expect(button).toHaveAttribute('aria-label', 'Save your changes'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('applies tooltipped-no-delay class when noDelay is true', () => { |
| 56 | + render( |
| 57 | + <Tooltip content="No delay tooltip" noDelay> |
| 58 | + <button>Button</button> |
| 59 | + </Tooltip> |
| 60 | + ); |
| 61 | + |
| 62 | + const button = screen.getByRole('button'); |
| 63 | + expect(button).toHaveClass('tooltipped-no-delay'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('does not apply tooltipped-no-delay class when noDelay is false', () => { |
| 67 | + render( |
| 68 | + <Tooltip content="Normal tooltip" noDelay={false}> |
| 69 | + <button>Button</button> |
| 70 | + </Tooltip> |
| 71 | + ); |
| 72 | + |
| 73 | + const button = screen.getByRole('button'); |
| 74 | + expect(button).not.toHaveClass('tooltipped-no-delay'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('preserves existing className on the child element', () => { |
| 78 | + render( |
| 79 | + <Tooltip content="Tooltip"> |
| 80 | + <button className="custom-class">Button</button> |
| 81 | + </Tooltip> |
| 82 | + ); |
| 83 | + |
| 84 | + const button = screen.getByRole('button'); |
| 85 | + expect(button).toHaveClass('custom-class'); |
| 86 | + expect(button).toHaveClass('tooltipped'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('wraps the child in a tooltip-wrapper span', () => { |
| 90 | + const { container } = render( |
| 91 | + <Tooltip content="Tooltip"> |
| 92 | + <button>Button</button> |
| 93 | + </Tooltip> |
| 94 | + ); |
| 95 | + |
| 96 | + const wrapper = container.querySelector('.tooltip-wrapper'); |
| 97 | + expect(wrapper).toBeInTheDocument(); |
| 98 | + expect(wrapper?.tagName.toLowerCase()).toBe('span'); |
| 99 | + }); |
| 100 | +}); |
0 commit comments