1
1
import React from 'react' ;
2
2
3
- import { Button , IconButton } from '@lumx/react' ;
3
+ import { Button } from '@lumx/react' ;
4
4
import { screen , render } from '@testing-library/react' ;
5
5
import { queryAllByTagName , queryByClassName } from '@lumx/react/testing/utils/queries' ;
6
6
import { commonTestsSuiteRTL } from '@lumx/react/testing/utils' ;
@@ -51,7 +51,6 @@ describe(`<${Tooltip.displayName}>`, () => {
51
51
// Default placement
52
52
expect ( tooltip ) . toHaveAttribute ( 'data-popper-placement' , 'bottom' ) ;
53
53
expect ( anchorWrapper ) . toBeInTheDocument ( ) ;
54
- expect ( anchorWrapper ) . toHaveAttribute ( 'aria-describedby' , tooltip ?. id ) ;
55
54
} ) ;
56
55
57
56
it ( 'should render with custom placement' , async ( ) => {
@@ -65,25 +64,6 @@ describe(`<${Tooltip.displayName}>`, () => {
65
64
expect ( tooltip ) . toHaveAttribute ( 'data-popper-placement' , 'top' ) ;
66
65
} ) ;
67
66
68
- it ( 'should wrap unknown children and not add aria-describedby when closed' , async ( ) => {
69
- const { anchorWrapper } = await setup ( {
70
- label : 'Tooltip label' ,
71
- children : 'Anchor' ,
72
- forceOpen : false ,
73
- } ) ;
74
- expect ( anchorWrapper ) . not . toHaveAttribute ( 'aria-describedby' ) ;
75
- } ) ;
76
-
77
- it ( 'should not wrap Button and not add aria-describedby when closed' , async ( ) => {
78
- await setup ( {
79
- label : 'Tooltip label' ,
80
- children : < Button > Anchor</ Button > ,
81
- forceOpen : false ,
82
- } ) ;
83
- const button = screen . queryByRole ( 'button' , { name : 'Anchor' } ) ;
84
- expect ( button ) . not . toHaveAttribute ( 'aria-describedby' ) ;
85
- } ) ;
86
-
87
67
it ( 'should not wrap Button' , async ( ) => {
88
68
const { tooltip, anchorWrapper } = await setup ( {
89
69
label : 'Tooltip label' ,
@@ -96,35 +76,6 @@ describe(`<${Tooltip.displayName}>`, () => {
96
76
expect ( button ) . toHaveAttribute ( 'aria-describedby' , tooltip ?. id ) ;
97
77
} ) ;
98
78
99
- it ( 'should not add aria-describedby if button label is the same as tooltip label' , async ( ) => {
100
- const label = 'Tooltip label' ;
101
- render ( < IconButton label = { label } tooltipProps = { { forceOpen : true } } /> ) ;
102
- const tooltip = screen . queryByRole ( 'tooltip' , { name : label } ) ;
103
- expect ( tooltip ) . toBeInTheDocument ( ) ;
104
- const button = screen . queryByRole ( 'button' , { name : label } ) ;
105
- expect ( button ) . not . toHaveAttribute ( 'aria-describedby' ) ;
106
- } ) ;
107
-
108
- it ( 'should keep anchor aria-describedby if button label is the same as tooltip label' , async ( ) => {
109
- const label = 'Tooltip label' ;
110
- render ( < IconButton label = { label } aria-describedby = ":header-1:" tooltipProps = { { forceOpen : true } } /> ) ;
111
- const tooltip = screen . queryByRole ( 'tooltip' , { name : label } ) ;
112
- expect ( tooltip ) . toBeInTheDocument ( ) ;
113
- const button = screen . queryByRole ( 'button' , { name : label } ) ;
114
- expect ( button ) . toHaveAttribute ( 'aria-describedby' , ':header-1:' ) ;
115
- } ) ;
116
-
117
- it ( 'should concat aria-describedby if already exists' , async ( ) => {
118
- const { tooltip } = await setup ( {
119
- label : 'Tooltip label' ,
120
- children : < Button aria-describedby = ":header-1:" > Anchor</ Button > ,
121
- forceOpen : true ,
122
- } ) ;
123
- expect ( tooltip ) . toBeInTheDocument ( ) ;
124
- const button = screen . queryByRole ( 'button' , { name : 'Anchor' } ) ;
125
- expect ( button ) . toHaveAttribute ( 'aria-describedby' , `:header-1: ${ tooltip ?. id } ` ) ;
126
- } ) ;
127
-
128
79
it ( 'should wrap disabled Button' , async ( ) => {
129
80
const { tooltip, anchorWrapper } = await setup ( {
130
81
label : 'Tooltip label' ,
@@ -172,17 +123,166 @@ describe(`<${Tooltip.displayName}>`, () => {
172
123
expect ( ref . current === element ) . toBe ( true ) ;
173
124
} ) ;
174
125
175
- it ( 'should render in closeMode=hide' , async ( ) => {
176
- const { tooltip } = await setup ( {
177
- label : 'Tooltip label' ,
178
- children : < Button > Anchor</ Button > ,
179
- closeMode : 'hide' ,
180
- forceOpen : false ,
126
+ describe ( 'closeMode="hide"' , ( ) => {
127
+ it ( 'should not render with empty label' , async ( ) => {
128
+ const { tooltip, anchorWrapper } = await setup ( {
129
+ label : undefined ,
130
+ forceOpen : true ,
131
+ closeMode : 'hide' ,
132
+ } ) ;
133
+ expect ( tooltip ) . not . toBeInTheDocument ( ) ;
134
+ expect ( anchorWrapper ) . not . toBeInTheDocument ( ) ;
135
+ } ) ;
136
+
137
+ it ( 'should render hidden' , async ( ) => {
138
+ const { tooltip } = await setup ( {
139
+ label : 'Tooltip label' ,
140
+ children : < Button > Anchor</ Button > ,
141
+ closeMode : 'hide' ,
142
+ forceOpen : false ,
143
+ } ) ;
144
+ expect ( tooltip ) . toBeInTheDocument ( ) ;
145
+ expect ( tooltip ) . toHaveClass ( 'lumx-tooltip--is-hidden' ) ;
146
+
147
+ const anchor = screen . getByRole ( 'button' , { name : 'Anchor' } ) ;
148
+ await userEvent . hover ( anchor ) ;
149
+ expect ( tooltip ) . not . toHaveClass ( 'lumx-tooltip--is-hidden' ) ;
150
+ } ) ;
151
+ } ) ;
152
+
153
+ describe ( 'ariaLinkMode="aria-describedby"' , ( ) => {
154
+ it ( 'should add aria-describedby on anchor on open' , async ( ) => {
155
+ await setup ( {
156
+ label : 'Tooltip label' ,
157
+ forceOpen : false ,
158
+ children : < Button aria-describedby = ":description1:" > Anchor</ Button > ,
159
+ } ) ;
160
+ const anchor = screen . getByRole ( 'button' , { name : 'Anchor' } ) ;
161
+ expect ( anchor ) . toHaveAttribute ( 'aria-describedby' , ':description1:' ) ;
162
+
163
+ await userEvent . hover ( anchor ) ;
164
+ const tooltip = screen . queryByRole ( 'tooltip' ) ;
165
+ expect ( anchor ) . toHaveAttribute ( 'aria-describedby' , `:description1: ${ tooltip ?. id } ` ) ;
166
+ } ) ;
167
+
168
+ it ( 'should always add aria-describedby on anchor with closeMode="hide"' , async ( ) => {
169
+ const { tooltip } = await setup ( {
170
+ label : 'Tooltip label' ,
171
+ forceOpen : false ,
172
+ children : < Button aria-describedby = ":description1:" > Anchor</ Button > ,
173
+ closeMode : 'hide' ,
174
+ } ) ;
175
+ const anchor = screen . getByRole ( 'button' , { name : 'Anchor' } ) ;
176
+ expect ( anchor ) . toHaveAttribute ( 'aria-describedby' , `:description1: ${ tooltip ?. id } ` ) ;
177
+ } ) ;
178
+
179
+ it ( 'should skip aria-describedby if anchor has label' , async ( ) => {
180
+ const { tooltip } = await setup ( {
181
+ label : 'Tooltip label' ,
182
+ forceOpen : true ,
183
+ children : (
184
+ < Button aria-describedby = ":description1:" aria-label = "Tooltip label" >
185
+ Anchor
186
+ </ Button >
187
+ ) ,
188
+ } ) ;
189
+ expect ( tooltip ) . toBeInTheDocument ( ) ;
190
+ expect ( screen . getByRole ( 'button' ) ) . toHaveAttribute ( 'aria-describedby' , `:description1:` ) ;
191
+ } ) ;
192
+
193
+ it ( 'should add aria-describedby on anchor wrapper on open' , async ( ) => {
194
+ const { anchorWrapper } = await setup ( {
195
+ label : 'Tooltip label' ,
196
+ forceOpen : false ,
197
+ children : 'Anchor' ,
198
+ } ) ;
199
+ expect ( anchorWrapper ) . not . toHaveAttribute ( 'aria-describedby' ) ;
200
+
201
+ await userEvent . hover ( anchorWrapper as any ) ;
202
+ const tooltip = screen . queryByRole ( 'tooltip' ) ;
203
+ expect ( anchorWrapper ) . toHaveAttribute ( 'aria-describedby' , tooltip ?. id ) ;
204
+ } ) ;
205
+
206
+ it ( 'should always add aria-describedby on anchor wrapper with closeMode="hide"' , async ( ) => {
207
+ const { tooltip, anchorWrapper } = await setup ( {
208
+ label : 'Tooltip label' ,
209
+ forceOpen : false ,
210
+ children : 'Anchor' ,
211
+ closeMode : 'hide' ,
212
+ } ) ;
213
+ expect ( anchorWrapper ) . toHaveAttribute ( 'aria-describedby' , `${ tooltip ?. id } ` ) ;
214
+ } ) ;
215
+ } ) ;
216
+
217
+ describe ( 'ariaLinkMode="aria-labelledby"' , ( ) => {
218
+ it ( 'should add aria-labelledby on anchor on open' , async ( ) => {
219
+ await setup ( {
220
+ label : 'Tooltip label' ,
221
+ forceOpen : false ,
222
+ children : < Button aria-labelledby = ":label1:" > Anchor</ Button > ,
223
+ ariaLinkMode : 'aria-labelledby' ,
224
+ } ) ;
225
+ const anchor = screen . getByRole ( 'button' , { name : 'Anchor' } ) ;
226
+ expect ( anchor ) . toHaveAttribute ( 'aria-labelledby' , ':label1:' ) ;
227
+
228
+ await userEvent . hover ( anchor ) ;
229
+ const tooltip = screen . queryByRole ( 'tooltip' ) ;
230
+ expect ( anchor ) . toHaveAttribute ( 'aria-labelledby' , `:label1: ${ tooltip ?. id } ` ) ;
231
+ } ) ;
232
+
233
+ it ( 'should always add aria-labelledby on anchor with closeMode="hide"' , async ( ) => {
234
+ const label = 'Tooltip label' ;
235
+ const { tooltip } = await setup ( {
236
+ label,
237
+ forceOpen : false ,
238
+ children : < Button aria-labelledby = ":label1:" > Anchor</ Button > ,
239
+ ariaLinkMode : 'aria-labelledby' ,
240
+ closeMode : 'hide' ,
241
+ } ) ;
242
+ const anchor = screen . queryByRole ( 'button' , { name : label } ) ;
243
+ expect ( anchor ) . toBeInTheDocument ( ) ;
244
+ expect ( anchor ) . toHaveAttribute ( 'aria-labelledby' , `:label1: ${ tooltip ?. id } ` ) ;
245
+ } ) ;
246
+
247
+ it ( 'should skip aria-labelledby if anchor has label' , async ( ) => {
248
+ const { tooltip } = await setup ( {
249
+ label : 'Tooltip label' ,
250
+ forceOpen : true ,
251
+ children : (
252
+ < Button aria-labelledby = ":label1:" aria-label = "Tooltip label" >
253
+ Anchor
254
+ </ Button >
255
+ ) ,
256
+ ariaLinkMode : 'aria-labelledby' ,
257
+ } ) ;
258
+ expect ( tooltip ) . toBeInTheDocument ( ) ;
259
+ expect ( screen . getByRole ( 'button' ) ) . toHaveAttribute ( 'aria-labelledby' , `:label1:` ) ;
260
+ } ) ;
261
+
262
+ it ( 'should add aria-labelledby on anchor wrapper on open' , async ( ) => {
263
+ const { anchorWrapper } = await setup ( {
264
+ label : 'Tooltip label' ,
265
+ forceOpen : false ,
266
+ children : 'Anchor' ,
267
+ ariaLinkMode : 'aria-labelledby' ,
268
+ } ) ;
269
+ expect ( anchorWrapper ) . not . toHaveAttribute ( 'aria-labelledby' ) ;
270
+
271
+ await userEvent . hover ( anchorWrapper as any ) ;
272
+ const tooltip = screen . queryByRole ( 'tooltip' ) ;
273
+ expect ( anchorWrapper ) . toHaveAttribute ( 'aria-labelledby' , tooltip ?. id ) ;
274
+ } ) ;
275
+
276
+ it ( 'should always add aria-labelledby on anchor wrapper with closeMode="hide"' , async ( ) => {
277
+ const { tooltip, anchorWrapper } = await setup ( {
278
+ label : 'Tooltip label' ,
279
+ forceOpen : false ,
280
+ children : 'Anchor' ,
281
+ ariaLinkMode : 'aria-labelledby' ,
282
+ closeMode : 'hide' ,
283
+ } ) ;
284
+ expect ( anchorWrapper ) . toHaveAttribute ( 'aria-labelledby' , `${ tooltip ?. id } ` ) ;
181
285
} ) ;
182
- expect ( tooltip ) . toBeInTheDocument ( ) ;
183
- expect ( tooltip ) . toHaveClass ( 'lumx-tooltip--is-hidden' ) ;
184
- const button = screen . queryByRole ( 'button' , { name : 'Anchor' } ) ;
185
- expect ( button ) . toHaveAttribute ( 'aria-describedby' , tooltip ?. id ) ;
186
286
} ) ;
187
287
} ) ;
188
288
@@ -203,7 +303,6 @@ describe(`<${Tooltip.displayName}>`, () => {
203
303
// Tooltip opened
204
304
tooltip = await screen . findByRole ( 'tooltip' , { name : 'Tooltip label' } ) ;
205
305
expect ( tooltip ) . toBeInTheDocument ( ) ;
206
- expect ( button ) . toHaveAttribute ( 'aria-describedby' , tooltip ?. id ) ;
207
306
208
307
// Un-hover anchor button
209
308
await userEvent . unhover ( button ) ;
0 commit comments