Skip to content

Commit c93cffe

Browse files
deconstruct id from props and add unit tests
1 parent 96c4eb0 commit c93cffe

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/drafts/Tooltip/Tooltip.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ const isInteractive = (element: HTMLElement) => {
185185
export const TooltipContext = React.createContext<{tooltipId?: string}>({})
186186

187187
export const Tooltip = React.forwardRef(
188-
({direction = 's', text, type = 'description', children, ...rest}: TooltipProps, forwardedRef) => {
189-
const tooltipId = useId(rest.id)
188+
({direction = 's', text, type = 'description', children, id, ...rest}: TooltipProps, forwardedRef) => {
189+
const tooltipId = useId(id)
190190
const child = Children.only(children)
191191
const triggerRef = useProvidedRefOrCreate(forwardedRef as React.RefObject<HTMLElement>)
192192
const tooltipElRef = useRef<HTMLDivElement>(null)

src/drafts/Tooltip/__tests__/Tooltip.test.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {Tooltip, TooltipProps} from '../Tooltip'
33
import {checkStoriesForAxeViolations} from '../../../utils/testing'
44
import {render as HTMLRender} from '@testing-library/react'
55
import theme from '../../../theme'
6-
import {Button, ActionMenu, ActionList, ThemeProvider, SSRProvider, BaseStyles} from '../../../'
6+
import {Button, IconButton, ActionMenu, ActionList, ThemeProvider, SSRProvider, BaseStyles} from '../../../'
7+
import {XIcon} from '@primer/octicons-react'
78

89
const TooltipComponent = (props: Omit<TooltipProps, 'text'> & {text?: string}) => (
910
<Tooltip text="Tooltip text" {...props}>
@@ -91,4 +92,22 @@ describe('Tooltip', () => {
9192
expect(menuButton).toHaveAttribute('aria-describedby', tooltip.id)
9293
expect(menuButton).toHaveAttribute('aria-haspopup', 'true')
9394
})
95+
it('should use the custom tooltip id (if present) to label the trigger element', () => {
96+
const {getByRole} = HTMLRender(
97+
<Tooltip id="custom-tooltip-id" text="Close feedback form" direction="nw" type="label">
98+
<IconButton aria-labelledby="custom-tooltip-id" icon={XIcon} variant="invisible" onClick={() => {}} />
99+
</Tooltip>,
100+
)
101+
const triggerEL = getByRole('button')
102+
expect(triggerEL).toHaveAttribute('aria-labelledby', 'custom-tooltip-id')
103+
})
104+
it('should use the custom tooltip id (if present) to described the trigger element', () => {
105+
const {getByRole} = HTMLRender(
106+
<Tooltip text="This operation cannot be reverted" id="custom-tooltip-id">
107+
<Button>Delete</Button>
108+
</Tooltip>,
109+
)
110+
const triggerEL = getByRole('button')
111+
expect(triggerEL).toHaveAttribute('aria-describedby', 'custom-tooltip-id')
112+
})
94113
})

0 commit comments

Comments
 (0)