/
tooltip.test.tsx
166 lines (118 loc) · 3.99 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import {
act,
fireEvent,
render,
screen,
testA11y,
waitForElementToBeRemoved,
press,
} from "@chakra-ui/test-utils"
import * as React from "react"
import { Tooltip, TooltipProps } from "../src"
const buttonLabel = "Hover me"
const tooltipLabel = "tooltip label"
const DummyComponent = (
props: Omit<TooltipProps & { isButtonDisabled?: boolean }, "children">,
) => {
const { isButtonDisabled, ...tooltipProps } = props
return (
<Tooltip label={tooltipLabel} {...tooltipProps}>
<button disabled={isButtonDisabled || false}>{buttonLabel}</button>
</Tooltip>
)
}
test("passes a11y test when hovered", async () => {
render(<DummyComponent />)
fireEvent.mouseOver(screen.getByText(buttonLabel))
const tooltip = await screen.findByRole("tooltip")
await testA11y(tooltip)
})
test("shows on mouseover and closes on mouseleave", async () => {
render(<DummyComponent />)
act(() => {
fireEvent.mouseOver(screen.getByText(buttonLabel))
})
await screen.findByRole("tooltip")
expect(screen.getByText(buttonLabel)).toBeInTheDocument()
expect(screen.getByRole("tooltip")).toBeInTheDocument()
act(() => {
fireEvent.mouseLeave(screen.getByText(buttonLabel))
})
await waitForElementToBeRemoved(() => screen.getByText(tooltipLabel))
})
test("should not show on mouseover if isDisabled is true", async () => {
jest.useFakeTimers()
render(<DummyComponent isDisabled />)
act(() => {
fireEvent.mouseOver(screen.getByText(buttonLabel))
jest.advanceTimersByTime(200)
})
expect(screen.queryByText(tooltipLabel)).not.toBeInTheDocument()
jest.useRealTimers()
})
test.skip("should close on mouseleave if openDelay is set", async () => {
jest.useFakeTimers()
render(<DummyComponent openDelay={500} />)
act(() => {
fireEvent.mouseOver(screen.getByText(buttonLabel))
})
jest.advanceTimersByTime(200)
expect(screen.queryByText(tooltipLabel)).not.toBeInTheDocument()
act(() => {
jest.advanceTimersByTime(500)
})
expect(screen.queryByText(tooltipLabel)).toBeInTheDocument()
act(() => {
fireEvent.mouseLeave(screen.getByText(buttonLabel))
jest.advanceTimersByTime(200)
})
await waitForElementToBeRemoved(() => screen.queryByText(tooltipLabel))
jest.useRealTimers()
})
test("should show on mouseover if isDisabled has a falsy value", async () => {
render(<DummyComponent isDisabled={false} />)
act(() => {
fireEvent.mouseOver(screen.getByText(buttonLabel))
})
await screen.findByRole("tooltip")
expect(screen.getByText(buttonLabel)).toBeInTheDocument()
})
test("should close on mouseleave if shouldWrapChildren is true and child is a disabled element", async () => {
render(<DummyComponent shouldWrapChildren isButtonDisabled />)
act(() => {
fireEvent.mouseEnter(screen.getByText(buttonLabel))
})
await screen.findByRole("tooltip")
const wrapper = screen.getByText(buttonLabel).parentElement
expect(wrapper).not.toBeNull()
act(() => {
fireEvent.mouseLeave(wrapper!)
})
await waitForElementToBeRemoved(() => screen.getByText(tooltipLabel))
})
test("shows on mouseover and closes on pressing 'esc'", async () => {
render(<DummyComponent />)
act(() => {
fireEvent.mouseOver(screen.getByText(buttonLabel))
})
await screen.findByRole("tooltip")
expect(screen.getByText(buttonLabel)).toBeInTheDocument()
expect(screen.getByRole("tooltip")).toBeInTheDocument()
act(() => {
press.Escape(screen.getByRole("tooltip"))
})
await waitForElementToBeRemoved(() => screen.getByText(tooltipLabel))
})
test("shows on mouseover and stays on pressing 'esc' if 'closeOnEsc' is false", async () => {
render(<DummyComponent closeOnEsc={false} />)
act(() => {
fireEvent.mouseOver(screen.getByText(buttonLabel))
})
await screen.findByRole("tooltip")
expect(screen.getByText(buttonLabel)).toBeInTheDocument()
expect(screen.getByRole("tooltip")).toBeInTheDocument()
act(() => {
press.Escape(screen.getByRole("tooltip"))
})
expect(screen.getByRole("tooltip")).toBeInTheDocument()
})