-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathtooltip.test.tsx
100 lines (74 loc) · 3.33 KB
/
tooltip.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { act, render, screen, waitFor } from '@testing-library/react'
import user from '@testing-library/user-event'
import { axe } from 'vitest-axe'
import { ComponentUnderTest } from './basic'
describe('Tooltip', () => {
it('should have no a11y violations', async () => {
const { container } = await act(() => render(<ComponentUnderTest />))
const results = await axe(container)
expect(results).toHaveNoViolations()
})
it('should show the tooltip on pointerover and close on pointer leave', async () => {
render(<ComponentUnderTest />)
const tooltipTrigger = screen.getByText('hover me')
await user.hover(tooltipTrigger)
await waitFor(() => expect(screen.getByRole('tooltip')).toBeInTheDocument())
await user.unhover(tooltipTrigger)
waitFor(() => {
expect(screen.queryByText('content')).not.toBeVisible()
})
})
it('should show on pointerover if isDisabled has a falsy value', async () => {
render(<ComponentUnderTest />)
const tooltipTrigger = await screen.findByText('hover me')
await user.hover(tooltipTrigger)
await screen.findByRole('tooltip')
expect(screen.getByText('hover me')).toBeVisible()
})
it('should hide the tooltip when escape is pressed', async () => {
render(<ComponentUnderTest closeOnEscape />)
const tooltipTrigger = screen.getByText('hover me')
await user.hover(tooltipTrigger)
await screen.findByRole('tooltip')
expect(screen.getByText('content')).toBeInTheDocument()
await user.keyboard('[Escape]')
await waitFor(() => {
expect(screen.queryByText('content')).not.toBeVisible()
})
})
it('should not hide the tooltip when escape is pressed if closeOnEsc is set to false', async () => {
render(<ComponentUnderTest closeOnEscape={false} />)
const tooltipTrigger = await screen.findByText('hover me')
await user.hover(tooltipTrigger)
await screen.findByRole('tooltip')
expect(screen.getByText('content')).toBeInTheDocument()
await user.keyboard('[Escape]')
await waitFor(() => expect(screen.getByRole('tooltip')).toBeInTheDocument())
})
it('should have pointer-events none style if interactive is set to false', async () => {
render(<ComponentUnderTest interactive={false} />)
const tooltipTrigger = await screen.findByText('hover me')
await user.hover(tooltipTrigger)
const tooltipContent = screen.getByText('content')
expect(tooltipContent).toHaveStyle({ 'pointer-events': 'none' })
})
it('should lazy render the tooltip', async () => {
render(<ComponentUnderTest lazyMount />)
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
const tooltipTrigger = screen.getByText('hover me')
await user.hover(tooltipTrigger)
await waitFor(() => expect(screen.getByRole('tooltip')).toBeInTheDocument())
await user.keyboard('[Escape]')
await waitFor(() => {
expect(screen.queryByRole('tooltip', { hidden: true })).not.toBeVisible()
})
})
it('should lazy mount and unmount on exit', async () => {
render(<ComponentUnderTest lazyMount unmountOnExit />)
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
const tooltipTrigger = screen.getByText('hover me')
await user.hover(tooltipTrigger)
await user.keyboard('[Escape]')
await waitFor(() => expect(screen.queryByRole('tooltip')).not.toBeInTheDocument())
})
})