@@ -279,5 +279,62 @@ describe('rc-tooltip', () => {
279279 expect ( tooltipElement . style . backgroundColor ) . toBe ( 'blue' ) ;
280280 expect ( tooltipBodyElement . style . color ) . toBe ( 'red' ) ;
281281 } ) ;
282+
283+ describe ( 'children handling' , ( ) => {
284+ it ( 'should pass aria-describedby to child element when overlay exists' , ( ) => {
285+ const { container } = render (
286+ < Tooltip id = "test-id" overlay = "tooltip content" >
287+ < button > Click me</ button >
288+ </ Tooltip > ,
289+ ) ;
290+
291+ expect ( container . querySelector ( 'button' ) ) . toHaveAttribute ( 'aria-describedby' , 'test-id' ) ;
292+ } ) ;
293+
294+ it ( 'should not pass aria-describedby when overlay is empty' , ( ) => {
295+ const { container } = render (
296+ < Tooltip id = "test-id" overlay = { null } >
297+ < button > Click me</ button >
298+ </ Tooltip > ,
299+ ) ;
300+
301+ expect ( container . querySelector ( 'button' ) ) . not . toHaveAttribute ( 'aria-describedby' ) ;
302+ } ) ;
303+
304+ it ( 'should preserve original props of children' , ( ) => {
305+ const onMouseEnter = jest . fn ( ) ;
306+
307+ const { container } = render (
308+ < Tooltip overlay = "tip" >
309+ < button className = "custom-btn" onMouseEnter = { onMouseEnter } >
310+ Click me
311+ </ button >
312+ </ Tooltip > ,
313+ ) ;
314+
315+ const btn = container . querySelector ( 'button' ) ;
316+ expect ( btn ) . toHaveClass ( 'custom-btn' ) ;
317+
318+ // 触发原始事件处理器
319+ fireEvent . mouseEnter ( btn ) ;
320+ expect ( onMouseEnter ) . toHaveBeenCalled ( ) ;
321+ } ) ;
322+
323+ it ( 'should throw error when multiple children provided' , ( ) => {
324+ const errorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
325+
326+ expect ( ( ) => {
327+ render (
328+ // @ts -expect-error
329+ < Tooltip overlay = "tip" >
330+ < button > First</ button >
331+ < button > Second</ button >
332+ </ Tooltip > ,
333+ ) ;
334+ } ) . toThrow ( ) ;
335+
336+ errorSpy . mockRestore ( ) ;
337+ } ) ;
338+ } ) ;
282339} ) ;
283340
0 commit comments