@@ -2,6 +2,7 @@ import { act, render, screen } from "@testing-library/react";
22import "@testing-library/jest-dom" ;
33
44import userEvent from "@testing-library/user-event" ;
5+ import { ReactElement , useEffect } from "react" ;
56import { Tooltip , TooltipProps } from "../Tooltip" ;
67
78jest . useFakeTimers ( ) ;
@@ -165,15 +166,15 @@ describe("Tooltip", () => {
165166
166167 it ( "renders sr-only content for screen readers before tooltip is shown" , ( ) => {
167168 render ( < Tooltip { ...defaultTooltipProps } /> ) ;
168- const srOnlyContent = document . querySelector ( ".sr-only" ) ;
169+ const srOnlyContent = document . querySelector ( ".widget-tooltip- sr-only" ) ;
169170
170171 expect ( srOnlyContent ) . toBeInTheDocument ( ) ;
171172 expect ( srOnlyContent ) . toHaveTextContent ( defaultTooltipProps . textMessage as string ) ;
172173 } ) ;
173174
174175 it ( "sr-only content is always in DOM even when tooltip is not visible" , ( ) => {
175176 render ( < Tooltip { ...defaultTooltipProps } openOn = "click" /> ) ;
176- const srOnlyContent = document . querySelector ( ".sr-only" ) ;
177+ const srOnlyContent = document . querySelector ( ".widget-tooltip- sr-only" ) ;
177178
178179 // Content should exist before tooltip is shown
179180 expect ( srOnlyContent ) . toBeInTheDocument ( ) ;
@@ -183,7 +184,7 @@ describe("Tooltip", () => {
183184 it ( "sr-only content matches the text message" , ( ) => {
184185 const customMessage = "Custom accessibility message" ;
185186 render ( < Tooltip { ...defaultTooltipProps } textMessage = { customMessage } /> ) ;
186- const srOnlyContent = document . querySelector ( ".sr-only" ) ;
187+ const srOnlyContent = document . querySelector ( ".widget-tooltip- sr-only" ) ;
187188
188189 expect ( srOnlyContent ) . toHaveTextContent ( customMessage ) ;
189190 } ) ;
@@ -198,17 +199,99 @@ describe("Tooltip", () => {
198199 textMessage = { undefined }
199200 />
200201 ) ;
201- const srOnlyContent = document . querySelector ( ".sr-only" ) ;
202+ const srOnlyContent = document . querySelector ( ".widget-tooltip- sr-only" ) ;
202203
203204 expect ( srOnlyContent ) . toHaveTextContent ( "Custom HTML Content" ) ;
204205 } ) ;
205206
206207 it ( "does not add aria-describedby when no tooltip content is provided" , ( ) => {
207208 render ( < Tooltip { ...defaultTooltipProps } textMessage = { undefined } htmlMessage = { undefined } /> ) ;
208209 const triggerElement = screen . getByTestId ( "tooltip-trigger" ) ;
209- const parentElement = triggerElement . parentElement ;
210210
211- expect ( parentElement ) . not . toHaveAttribute ( "aria-describedby" ) ;
211+ expect ( triggerElement ) . not . toHaveAttribute ( "aria-describedby" ) ;
212+ } ) ;
213+
214+ it ( "renders open custom content once and keeps widgets mounted across open and close" , async ( ) => {
215+ const onMount = jest . fn ( ) ;
216+ const CustomWidget = ( ) : ReactElement => {
217+ useEffect ( onMount , [ ] ) ;
218+ return < div > Custom widget</ div > ;
219+ } ;
220+ render (
221+ < Tooltip
222+ { ...defaultTooltipProps }
223+ renderMethod = "custom"
224+ htmlMessage = { < CustomWidget /> }
225+ textMessage = { undefined }
226+ />
227+ ) ;
228+ const user = userEvent . setup ( { advanceTimers : jest . advanceTimersByTime } ) ;
229+ const triggerElement = screen . getByTestId ( "tooltip-trigger" ) ;
230+
231+ await user . click ( triggerElement ) ;
232+ expect ( screen . getAllByText ( "Custom widget" ) ) . toHaveLength ( 1 ) ;
233+
234+ await user . click ( triggerElement ) ;
235+ expect ( screen . getAllByText ( "Custom widget" ) ) . toHaveLength ( 1 ) ;
236+ expect ( onMount ) . toHaveBeenCalledTimes ( 1 ) ;
237+ } ) ;
238+
239+ it ( "makes content inert and aria-hidden only while closed" , async ( ) => {
240+ render ( < Tooltip { ...defaultTooltipProps } /> ) ;
241+ const user = userEvent . setup ( { advanceTimers : jest . advanceTimersByTime } ) ;
242+ const closedContent = document . querySelector ( ".widget-tooltip-sr-only" ) ;
243+
244+ expect ( closedContent ) . toHaveAttribute ( "inert" ) ;
245+ expect ( closedContent ) . toHaveAttribute ( "aria-hidden" , "true" ) ;
246+
247+ await user . click ( screen . getByTestId ( "tooltip-trigger" ) ) ;
248+ const openContent = screen . getByRole ( "tooltip" ) ;
249+
250+ expect ( openContent ) . toBe ( closedContent ) ;
251+ expect ( openContent ) . not . toHaveAttribute ( "inert" ) ;
252+ expect ( openContent ) . not . toHaveAttribute ( "aria-hidden" ) ;
253+ } ) ;
254+
255+ it ( "places focusable elements in closed custom content inside the inert element" , ( ) => {
256+ render (
257+ < Tooltip
258+ { ...defaultTooltipProps }
259+ renderMethod = "custom"
260+ htmlMessage = { < button > Inside tooltip</ button > }
261+ textMessage = { undefined }
262+ />
263+ ) ;
264+
265+ expect ( screen . getByText ( "Inside tooltip" ) . closest ( "[inert]" ) ) . toBeInTheDocument ( ) ;
266+ } ) ;
267+
268+ it ( "ignores custom content in text mode when the text is empty" , async ( ) => {
269+ render (
270+ < Tooltip { ...defaultTooltipProps } htmlMessage = { < div > Custom content</ div > } textMessage = { undefined } />
271+ ) ;
272+ const user = userEvent . setup ( { advanceTimers : jest . advanceTimersByTime } ) ;
273+ const triggerElement = screen . getByTestId ( "tooltip-trigger" ) ;
274+
275+ expect ( triggerElement ) . not . toHaveAttribute ( "aria-describedby" ) ;
276+
277+ await user . click ( triggerElement ) ;
278+ expect ( screen . queryByText ( "Custom content" ) ) . toBeNull ( ) ;
279+ expect ( screen . queryByRole ( "tooltip" ) ) . toBeNull ( ) ;
280+ } ) ;
281+
282+ it ( "renders preview content once without adding a description" , ( ) => {
283+ render (
284+ < Tooltip
285+ { ...defaultTooltipProps }
286+ preview
287+ renderMethod = "custom"
288+ htmlMessage = { < div > Drop zone</ div > }
289+ textMessage = { undefined }
290+ />
291+ ) ;
292+
293+ expect ( screen . getAllByText ( "Drop zone" ) ) . toHaveLength ( 1 ) ;
294+ expect ( screen . getByTestId ( "tooltip-trigger" ) ) . not . toHaveAttribute ( "aria-describedby" ) ;
212295 } ) ;
213296
214297 it ( "adds aria-describedby to all focusable elements in trigger" , ( ) => {
@@ -244,5 +327,50 @@ describe("Tooltip", () => {
244327 expect ( ariaDescribedBy1 ) . toBe ( ariaDescribedBy2 ) ;
245328 expect ( ariaDescribedBy2 ) . toBe ( ariaDescribedBy3 ) ;
246329 } ) ;
330+
331+ it ( "keeps an existing aria-describedby and restores it when the content is removed" , ( ) => {
332+ const trigger = < input aria-describedby = "help-text" data-testid = "input" /> ;
333+ const { rerender } = render ( < Tooltip { ...defaultTooltipProps } trigger = { trigger } /> ) ;
334+ const input = screen . getByTestId ( "input" ) ;
335+ const contentId = document . querySelector ( ".widget-tooltip-sr-only" ) ?. id ;
336+
337+ expect ( input ) . toHaveAttribute ( "aria-describedby" , `help-text ${ contentId } ` ) ;
338+
339+ rerender ( < Tooltip { ...defaultTooltipProps } trigger = { trigger } textMessage = { undefined } /> ) ;
340+
341+ expect ( input ) . toHaveAttribute ( "aria-describedby" , "help-text" ) ;
342+ } ) ;
343+
344+ it ( "removes aria-describedby when it only contained the tooltip" , ( ) => {
345+ const trigger = < button data-testid = "button" > Button</ button > ;
346+ const { rerender } = render ( < Tooltip { ...defaultTooltipProps } trigger = { trigger } /> ) ;
347+
348+ rerender ( < Tooltip { ...defaultTooltipProps } trigger = { trigger } textMessage = { undefined } /> ) ;
349+
350+ expect ( screen . getByTestId ( "button" ) ) . not . toHaveAttribute ( "aria-describedby" ) ;
351+ } ) ;
352+
353+ it ( "adds aria-describedby to focusable elements that mount later" , async ( ) => {
354+ const { rerender } = render ( < Tooltip { ...defaultTooltipProps } trigger = { < span > Loading</ span > } /> ) ;
355+ const contentId = document . querySelector ( ".widget-tooltip-sr-only" ) ?. id ;
356+
357+ await act ( async ( ) => {
358+ rerender ( < Tooltip { ...defaultTooltipProps } trigger = { < button data-testid = "button" > Button</ button > } /> ) ;
359+ } ) ;
360+
361+ expect ( screen . getByTestId ( "button" ) ) . toHaveAttribute ( "aria-describedby" , contentId ) ;
362+ } ) ;
363+
364+ it ( "adds the tooltip back when the trigger resets aria-describedby" , async ( ) => {
365+ render ( < Tooltip { ...defaultTooltipProps } trigger = { < button data-testid = "button" > Button</ button > } /> ) ;
366+ const button = screen . getByTestId ( "button" ) ;
367+ const contentId = document . querySelector ( ".widget-tooltip-sr-only" ) ?. id ;
368+
369+ await act ( async ( ) => {
370+ button . setAttribute ( "aria-describedby" , "validation-message" ) ;
371+ } ) ;
372+
373+ expect ( button ) . toHaveAttribute ( "aria-describedby" , `validation-message ${ contentId } ` ) ;
374+ } ) ;
247375 } ) ;
248376} ) ;
0 commit comments