Skip to content

Commit 2afd043

Browse files
test: use waitFor only when relevant
1 parent 890a7a7 commit 2afd043

File tree

3 files changed

+29
-76
lines changed

3 files changed

+29
-76
lines changed

src/test/tooltip-attributes.spec.js

+6-18
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,8 @@ describe('tooltip attributes', () => {
3939

4040
await userEvent.hover(anchorElement)
4141

42-
let tooltip = null
43-
44-
await waitFor(() => {
45-
tooltip = screen.getByRole('tooltip')
46-
expect(tooltip).toHaveAttribute('style')
47-
})
42+
const tooltip = await screen.findByRole('tooltip')
43+
expect(tooltip).toHaveAttribute('style')
4844

4945
expect(anchorElement).toHaveAttribute('data-tooltip-content')
5046
expect(tooltip).toBeInTheDocument()
@@ -63,12 +59,8 @@ describe('tooltip attributes', () => {
6359

6460
await userEvent.hover(anchorElement)
6561

66-
let tooltip = null
67-
68-
await waitFor(() => {
69-
tooltip = screen.getByRole('tooltip')
70-
expect(tooltip).toHaveAttribute('style')
71-
})
62+
const tooltip = await screen.findByRole('tooltip')
63+
expect(tooltip).toHaveAttribute('style')
7264

7365
expect(anchorElement).toHaveAttribute('data-tooltip-place')
7466
expect(anchorElement).toHaveAttribute('data-tooltip-content')
@@ -88,12 +80,8 @@ describe('tooltip attributes', () => {
8880

8981
await userEvent.hover(anchorElement)
9082

91-
let tooltip = null
92-
93-
await waitFor(() => {
94-
tooltip = screen.getByRole('tooltip')
95-
expect(tooltip).toHaveClass('tooltip-class-name')
96-
})
83+
const tooltip = await screen.findByRole('tooltip')
84+
expect(tooltip).toHaveClass('tooltip-class-name')
9785

9886
expect(anchorElement).toHaveAttribute('data-tooltip-class-name')
9987
expect(tooltip).toBeInTheDocument()

src/test/tooltip-props.spec.js

+23-55
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,8 @@ describe('tooltip props', () => {
3535

3636
await userEvent.hover(anchorElement)
3737

38-
let tooltip = null
39-
40-
await waitFor(() => {
41-
tooltip = screen.getByRole('tooltip')
42-
expect(tooltip).toHaveAttribute('style')
43-
})
38+
const tooltip = await screen.findByRole('tooltip')
39+
expect(tooltip).toHaveAttribute('style')
4440

4541
expect(tooltip).toBeInTheDocument()
4642
expect(container).toMatchSnapshot()
@@ -54,12 +50,8 @@ describe('tooltip props', () => {
5450

5551
await userEvent.hover(anchorElement)
5652

57-
let tooltip = null
58-
59-
await waitFor(() => {
60-
tooltip = screen.getByRole('tooltip')
61-
expect(tooltip).toHaveAttribute('style')
62-
})
53+
const tooltip = await screen.findByRole('tooltip')
54+
expect(tooltip).toHaveAttribute('style')
6355

6456
expect(tooltip).toBeInTheDocument()
6557
expect(container).toMatchSnapshot()
@@ -71,12 +63,8 @@ describe('tooltip props', () => {
7163

7264
await userEvent.hover(anchorElement)
7365

74-
let tooltip = null
75-
76-
await waitFor(() => {
77-
tooltip = screen.getByRole('tooltip')
78-
expect(tooltip).toHaveAttribute('style')
79-
})
66+
const tooltip = await screen.findByRole('tooltip')
67+
expect(tooltip).toHaveAttribute('style')
8068

8169
expect(tooltip).toBeInTheDocument()
8270
expect(container).toMatchSnapshot()
@@ -98,14 +86,10 @@ describe('tooltip props', () => {
9886

9987
await userEvent.hover(anchorElement)
10088

101-
let tooltip = null
102-
let button = null
89+
const tooltip = await screen.findByRole('tooltip')
90+
expect(tooltip).toHaveAttribute('style')
10391

104-
await waitFor(() => {
105-
tooltip = screen.getByRole('tooltip')
106-
button = screen.getByRole('button')
107-
expect(tooltip).toHaveAttribute('style')
108-
})
92+
const button = await screen.findByRole('button')
10993

11094
await userEvent.click(button)
11195

@@ -123,22 +107,13 @@ describe('tooltip props', () => {
123107

124108
await userEvent.hover(anchorElement)
125109

126-
let tooltip = null
127-
128-
await waitFor(
129-
() => {
130-
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
131-
},
132-
{
133-
timeout: 250,
134-
},
135-
)
136-
137110
await waitFor(() => {
138-
tooltip = screen.getByRole('tooltip')
139-
expect(tooltip).toHaveAttribute('style')
111+
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
140112
})
141113

114+
const tooltip = await screen.findByRole('tooltip')
115+
expect(tooltip).toHaveAttribute('style')
116+
142117
expect(tooltip).toBeInTheDocument()
143118
expect(container).toMatchSnapshot()
144119
})
@@ -151,15 +126,16 @@ describe('tooltip props', () => {
151126

152127
await userEvent.hover(anchorElement)
153128

154-
await waitFor(() => {
155-
expect(screen.queryByRole('tooltip')).toBeInTheDocument()
156-
})
129+
const tooltip = await screen.findByRole('tooltip')
130+
expect(tooltip).toHaveAttribute('style')
131+
132+
expect(tooltip).toBeInTheDocument()
157133

158134
await userEvent.unhover(anchorElement)
159135

160136
await waitFor(
161137
() => {
162-
expect(screen.queryByRole('tooltip')).toBeInTheDocument()
138+
expect(tooltip).toBeInTheDocument()
163139
},
164140
{
165141
timeout: 200,
@@ -168,7 +144,7 @@ describe('tooltip props', () => {
168144

169145
await waitFor(
170146
() => {
171-
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument()
147+
expect(tooltip).not.toBeInTheDocument()
172148
},
173149
{
174150
timeout: 500,
@@ -186,12 +162,8 @@ describe('tooltip props', () => {
186162

187163
await userEvent.hover(anchorElement)
188164

189-
let tooltip = null
190-
191-
await waitFor(() => {
192-
tooltip = screen.getByRole('tooltip')
193-
expect(tooltip).toHaveAttribute('style')
194-
})
165+
const tooltip = await screen.findByRole('tooltip')
166+
expect(tooltip).toHaveAttribute('style')
195167

196168
expect(tooltip).toBeInTheDocument()
197169
expect(container).toMatchSnapshot()
@@ -203,12 +175,8 @@ describe('tooltip props', () => {
203175

204176
await userEvent.hover(anchorElement)
205177

206-
let tooltip = null
207-
208-
await waitFor(() => {
209-
tooltip = screen.getByRole('tooltip')
210-
expect(tooltip).toHaveAttribute('style')
211-
})
178+
const tooltip = await screen.findByRole('tooltip')
179+
expect(tooltip).toHaveAttribute('style')
212180

213181
expect(tooltip).toBeInTheDocument()
214182
expect(container).toMatchSnapshot()

src/test/utils.spec.js

-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { debounce, computeTooltipPosition, cssTimeToMs } from 'utils'
22

3-
// Tell Jest to mock all timeout functions
4-
jest.useRealTimers()
5-
63
describe('compute positions', () => {
74
test('empty reference elements', async () => {
85
const value = await computeTooltipPosition({

0 commit comments

Comments
 (0)