diff --git a/src/Tooltip.tsx b/src/Tooltip.tsx index ac1ea87..fc67dcb 100644 --- a/src/Tooltip.tsx +++ b/src/Tooltip.tsx @@ -121,7 +121,7 @@ const Tooltip = React.forwardRef((props, ref) => { const getChildren: TriggerProps['children'] = ({ open }) => { const child = React.Children.only(children); const childAriaDescribedBy = (child.props as React.AriaAttributes)['aria-describedby']; - const ariaDescribedBy = [childAriaDescribedBy, overlay && open ? mergedId : undefined] + const ariaDescribedBy = [childAriaDescribedBy, overlay != null && open ? mergedId : undefined] .filter(Boolean) .join(' '); const ariaProps: React.AriaAttributes = { diff --git a/tests/index.test.tsx b/tests/index.test.tsx index 019854d..e4af8bf 100644 --- a/tests/index.test.tsx +++ b/tests/index.test.tsx @@ -597,6 +597,35 @@ describe('rc-tooltip', () => { expect(trigger).toHaveAttribute('aria-describedby', 'existing-description'); }); + it('should set aria-describedby when overlay is a falsy but valid node like 0', () => { + const { container } = render( + + + , + ); + + const trigger = container.querySelector('button'); + const describedBy = trigger.getAttribute('aria-describedby'); + expect(describedBy).toBeTruthy(); + expect(document.getElementById(describedBy)).toHaveTextContent('0'); + }); + + it('should not set aria-describedby when overlay is null or undefined', () => { + const { container: nullContainer } = render( + + + , + ); + expect(nullContainer.querySelector('button')).not.toHaveAttribute('aria-describedby'); + + const { container: undefinedContainer } = render( + + + , + ); + expect(undefinedContainer.querySelector('button')).not.toHaveAttribute('aria-describedby'); + }); + it('should preserve original props of children', () => { const onMouseEnter = jest.fn();