1
1
import React from 'react' ;
2
2
3
3
import { Button } from '@lumx/react' ;
4
- import { act , render , waitFor } from '@testing-library/react' ;
5
- import {
6
- findByClassName ,
7
- getByClassName ,
8
- queryAllByTagName ,
9
- queryByClassName ,
10
- } from '@lumx/react/testing/utils/queries' ;
4
+ import { screen , render , waitFor } from '@testing-library/react' ;
5
+ import { queryAllByTagName , queryByClassName } from '@lumx/react/testing/utils/queries' ;
11
6
import { commonTestsSuiteRTL } from '@lumx/react/testing/utils' ;
12
7
import userEvent from '@testing-library/user-event' ;
13
8
@@ -16,23 +11,19 @@ import { Tooltip, TooltipProps } from './Tooltip';
16
11
const CLASSNAME = Tooltip . className as string ;
17
12
18
13
jest . mock ( 'uid' , ( ) => ( { uid : ( ) => 'uid' } ) ) ;
14
+ // Skip delays
15
+ jest . mock ( '@lumx/react/constants' , ( ) => ( {
16
+ ...jest . requireActual ( '@lumx/react/constants' ) ,
17
+ TOOLTIP_HOVER_DELAY : { open : 0 , close : 0 } ,
18
+ } ) ) ;
19
19
20
20
/**
21
21
* Mounts the component and returns common DOM elements / data needed in multiple tests further down.
22
22
*/
23
23
const setup = async ( propsOverride : Partial < TooltipProps > = { } ) => {
24
- const props : any = { forceOpen : true , ...propsOverride } ;
25
- // Hack to remove act() warning in console
26
- await ( act as any ) ( ( ) =>
27
- Promise . resolve (
28
- render (
29
- < Tooltip label = "Tooltip" { ...props } >
30
- { props . children || 'Anchor' }
31
- </ Tooltip > ,
32
- ) ,
33
- ) ,
34
- ) ;
35
- const tooltip = queryByClassName ( document . body , CLASSNAME ) ;
24
+ const props : any = { forceOpen : true , label : 'Tooltip label' , children : 'Anchor' , ...propsOverride } ;
25
+ render ( < Tooltip { ...props } /> ) ;
26
+ const tooltip = screen . queryByRole ( 'tooltip' , { name : props . label } ) ;
36
27
const anchorWrapper = queryByClassName ( document . body , 'lumx-tooltip-anchor-wrapper' ) ;
37
28
return { props, tooltip, anchorWrapper } ;
38
29
} ;
@@ -67,7 +58,7 @@ describe(`<${Tooltip.displayName}>`, () => {
67
58
} ) ;
68
59
expect ( tooltip ) . toBeInTheDocument ( ) ;
69
60
expect ( anchorWrapper ) . not . toBeInTheDocument ( ) ;
70
- const button = queryByClassName ( document . body , Button . className as string ) ;
61
+ const button = screen . queryByRole ( 'button' , { name : 'Anchor' } ) ;
71
62
expect ( button ) . toHaveAttribute ( 'aria-describedby' , tooltip ?. id ) ;
72
63
} ) ;
73
64
@@ -80,7 +71,7 @@ describe(`<${Tooltip.displayName}>`, () => {
80
71
expect ( tooltip ) . toBeInTheDocument ( ) ;
81
72
expect ( anchorWrapper ) . toBeInTheDocument ( ) ;
82
73
expect ( anchorWrapper ) . toHaveAttribute ( 'aria-describedby' , tooltip ?. id ) ;
83
- const button = queryByClassName ( document . body , Button . className as string ) ;
74
+ const button = screen . queryByRole ( 'button' , { name : 'Anchor' } ) ;
84
75
expect ( button ?. parentElement ) . toBe ( anchorWrapper ) ;
85
76
} ) ;
86
77
@@ -108,11 +99,11 @@ describe(`<${Tooltip.displayName}>`, () => {
108
99
expect ( tooltip ) . not . toBeInTheDocument ( ) ;
109
100
110
101
// Hover anchor button
111
- const button = getByClassName ( document . body , Button . className as string ) ;
102
+ const button = screen . getByRole ( 'button' , { name : 'Anchor' } ) ;
112
103
await userEvent . hover ( button ) ;
113
104
114
105
// Tooltip opened
115
- tooltip = await findByClassName ( document . body , CLASSNAME ) ;
106
+ tooltip = await screen . findByRole ( 'tooltip' , { name : 'Tooltip label' } ) ;
116
107
expect ( tooltip ) . toBeInTheDocument ( ) ;
117
108
expect ( button ) . toHaveAttribute ( 'aria-describedby' , tooltip ?. id ) ;
118
109
@@ -135,11 +126,11 @@ describe(`<${Tooltip.displayName}>`, () => {
135
126
expect ( tooltip ) . not . toBeInTheDocument ( ) ;
136
127
137
128
// Hover anchor button
138
- const button = getByClassName ( document . body , Button . className as string ) ;
129
+ const button = screen . getByRole ( 'button' , { name : 'Anchor' } ) ;
139
130
await userEvent . hover ( button ) ;
140
131
141
132
// Tooltip opened
142
- tooltip = await findByClassName ( document . body , CLASSNAME ) ;
133
+ tooltip = await screen . findByRole ( 'tooltip' , { name : 'Tooltip label' } ) ;
143
134
expect ( tooltip ) . toBeInTheDocument ( ) ;
144
135
expect ( button ) . toHaveAttribute ( 'aria-describedby' , tooltip ?. id ) ;
145
136
@@ -157,7 +148,7 @@ describe(`<${Tooltip.displayName}>`, () => {
157
148
} ) ;
158
149
} ) ;
159
150
160
- it ( 'should activate on anchor focus' , async ( ) => {
151
+ it ( 'should activate on anchor focus and close on escape ' , async ( ) => {
161
152
let { tooltip } = await setup ( {
162
153
label : 'Tooltip label' ,
163
154
children : < Button > Anchor</ Button > ,
@@ -168,21 +159,27 @@ describe(`<${Tooltip.displayName}>`, () => {
168
159
169
160
// Focus anchor button
170
161
await userEvent . tab ( ) ;
171
- const button = getByClassName ( document . body , Button . className as string ) ;
162
+ const button = screen . getByRole ( 'button' , { name : 'Anchor' } ) ;
172
163
expect ( button ) . toHaveFocus ( ) ;
173
164
174
165
// Tooltip opened
175
- tooltip = await findByClassName ( document . body , CLASSNAME ) ;
166
+ tooltip = await screen . findByRole ( 'tooltip' , { name : 'Tooltip label' } ) ;
176
167
expect ( tooltip ) . toBeInTheDocument ( ) ;
177
168
expect ( button ) . toHaveAttribute ( 'aria-describedby' , tooltip ?. id ) ;
178
169
179
- // Focus next element
180
- userEvent . tab ( ) ;
181
- await waitFor ( ( ) => {
182
- expect ( button ) . not . toHaveFocus ( ) ;
183
- // Tooltip closed
184
- expect ( tooltip ) . not . toBeInTheDocument ( ) ;
185
- } ) ;
170
+ // Focus next element => close tooltip
171
+ await userEvent . tab ( ) ;
172
+ expect ( button ) . not . toHaveFocus ( ) ;
173
+ expect ( tooltip ) . not . toBeInTheDocument ( ) ;
174
+
175
+ // Focus again
176
+ await userEvent . tab ( { shift : true } ) ;
177
+ tooltip = await screen . findByRole ( 'tooltip' , { name : 'Tooltip label' } ) ;
178
+ expect ( tooltip ) . toBeInTheDocument ( ) ;
179
+
180
+ // Escape pressed => close tooltip
181
+ await userEvent . keyboard ( '{Escape}' ) ;
182
+ expect ( tooltip ) . not . toBeInTheDocument ( ) ;
186
183
} ) ;
187
184
} ) ;
188
185
0 commit comments