diff --git a/src/__testing__/InfoTooltip.test.tsx b/src/__testing__/InfoTooltip.test.tsx new file mode 100644 index 000000000..92727c7e8 --- /dev/null +++ b/src/__testing__/InfoTooltip.test.tsx @@ -0,0 +1,35 @@ +import { render, screen } from '@testing-library/react'; +import React from 'react'; +import { InfoTooltip } from '../custom/CustomTooltip'; +import { SistentThemeProvider } from '../theme'; + +// CustomTooltip renders its string titles through the Markdown module, which +// is ESM-only and not transformed by this jest setup. The tooltip's content +// rendering is not what this file exercises. +jest.mock('../custom/Markdown', () => ({ + RenderMarkdownTooltip: ({ content }: { content: string }) => <>{content} +})); + +const renderWithTheme = (ui: React.ReactElement) => + render({ui}); + +describe('InfoTooltip', () => { + it('renders a focusable trigger so the tooltip opens on keyboard focus', async () => { + renderWithTheme(); + + const trigger = screen.getByRole('button', { name: 'More information' }); + + expect(trigger.getAttribute('type')).toBe('button'); + expect(trigger.tabIndex).toBeGreaterThanOrEqual(0); + trigger.focus(); + expect(await screen.findByRole('tooltip')).not.toBeNull(); + }); + + it('exposes an accessible name so screen readers announce the trigger', () => { + renderWithTheme(); + + expect( + screen.getByRole('button', { name: 'More information' }) + ).not.toBeNull(); + }); +}); diff --git a/src/custom/CustomTooltip/infoTooltip.tsx b/src/custom/CustomTooltip/infoTooltip.tsx index e7376d2c9..c4f10cc1c 100644 --- a/src/custom/CustomTooltip/infoTooltip.tsx +++ b/src/custom/CustomTooltip/infoTooltip.tsx @@ -10,17 +10,23 @@ type InfoTooltipProps = { const InfoTooltip = ({ helpText, style = {}, ...props }: InfoTooltipProps) => { return ( -
-
+
); };