Tooltip
A popup that displays information related to an element when the element receives keyboard focus or the mouse hovers over it.
Demo
Accessibility Features
WAI-ARIA Roles
| Role | Target Element | Description |
|---|---|---|
tooltip | Tooltip popup | A contextual popup that displays a description for an element |
WAI-ARIA Properties
aria-describedby
Only when tooltip is visible. References the tooltip element to provide an accessible description for the trigger element.
- Values
- ID of tooltip
- Required
- Conditional
aria-hidden
Indicates whether the tooltip is hidden from assistive technology. Default is true.
- Values
true|false- Required
- No
Keyboard Support
| Key | Action |
|---|---|
| Escape | Closes the tooltip |
| Tab | Standard focus navigation; tooltip shows when trigger receives focus |
Focus Management
| Event | Behavior |
|---|---|
| Tooltip display | Tooltip never receives focus - Per APG, tooltips must not be focusable. If interactive content is needed, use a Dialog or Popover pattern instead. |
| Trigger focus | Focus triggers display - When the trigger element receives focus, the tooltip appears after the configured delay. |
| Trigger blur | Blur hides tooltip - When focus leaves the trigger element, the tooltip is hidden. |
Mouse/Pointer Behavior
- Hover triggers display - Moving the pointer over the trigger shows the tooltip after the delay.
- Pointer leave hides - Moving the pointer away from the trigger hides the tooltip.
Visual Design
- High contrast - Dark background with light text ensures readability
- Dark mode support - Colors invert appropriately in dark mode
- Positioned near trigger - Tooltip appears adjacent to the triggering element
- Configurable delay - Prevents accidental activation during cursor movement
References
Source Code
<script lang="ts" module>
import type { Snippet } from 'svelte';
export type TooltipPlacement = 'top' | 'bottom' | 'left' | 'right';
export interface TooltipProps {
/** Tooltip content - can be string or Snippet for rich content */
content: string | Snippet;
/** Trigger element - must be a focusable element for keyboard accessibility */
children?: Snippet<[{ describedBy: string | undefined }]>;
/** Controlled open state */
open?: boolean;
/** Default open state (uncontrolled) */
defaultOpen?: boolean;
/** Delay before showing tooltip (ms) */
delay?: number;
/** Tooltip placement */
placement?: TooltipPlacement;
/**
* Tooltip ID - Required for SSR/hydration consistency.
* Must be unique and stable across server and client renders.
*/
id: string;
/** Whether the tooltip is disabled */
disabled?: boolean;
/** Additional class name for the wrapper */
class?: string;
/** Additional class name for the tooltip content */
tooltipClass?: string;
}
</script>
<script lang="ts">
import { cn } from '@/lib/utils';
import { onDestroy, untrack } from 'svelte';
let {
content,
children,
open: controlledOpen = undefined,
defaultOpen = false,
delay = 300,
placement = 'top',
id,
disabled = false,
class: className = '',
tooltipClass = '',
onOpenChange,
}: TooltipProps & { onOpenChange?: (open: boolean) => void } = $props();
// Use provided id directly - required for SSR/hydration consistency
const tooltipId = $derived(id);
let internalOpen = $state(untrack(() => defaultOpen));
let timeout: ReturnType<typeof setTimeout> | null = null;
let isControlled = $derived(controlledOpen !== undefined);
let isOpen = $derived(isControlled ? controlledOpen : internalOpen);
// aria-describedby should always be set when not disabled for screen reader accessibility
// This ensures SR users know the element has a description even before tooltip is visible
let describedBy = $derived(!disabled ? tooltipId : undefined);
function setOpen(value: boolean) {
if (controlledOpen === undefined) {
internalOpen = value;
}
onOpenChange?.(value);
}
function handleKeyDown(event: KeyboardEvent) {
if (event.key === 'Escape') {
hideTooltip();
}
}
function showTooltip() {
if (disabled) return;
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
setOpen(true);
}, delay);
}
function hideTooltip() {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
setOpen(false);
}
// Manage keydown listener based on isOpen state
// This handles both controlled and uncontrolled modes
$effect(() => {
if (isOpen) {
document.addEventListener('keydown', handleKeyDown);
} else {
document.removeEventListener('keydown', handleKeyDown);
}
});
// Cleanup on destroy - fix for memory leak
onDestroy(() => {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
document.removeEventListener('keydown', handleKeyDown);
});
const placementClasses: Record<TooltipPlacement, string> = {
top: 'bottom-full left-1/2 -translate-x-1/2 mb-2',
bottom: 'top-full left-1/2 -translate-x-1/2 mt-2',
left: 'right-full top-1/2 -translate-y-1/2 mr-2',
right: 'left-full top-1/2 -translate-y-1/2 ml-2',
};
</script>
<!-- svelte-ignore a11y_no_static_element_interactions -->
<span
class={cn('apg-tooltip-trigger', 'relative inline-block', className)}
onmouseenter={showTooltip}
onmouseleave={hideTooltip}
onfocusin={showTooltip}
onfocusout={hideTooltip}
>
{#if children}
{@render children({ describedBy })}
{/if}
<span
id={tooltipId}
role="tooltip"
aria-hidden={!isOpen}
class={cn(
'apg-tooltip',
'absolute z-50 px-3 py-1.5 text-sm',
'rounded-md bg-gray-900 text-white shadow-lg',
'dark:bg-gray-100 dark:text-gray-900',
'pointer-events-none whitespace-nowrap',
'transition-opacity duration-150',
placementClasses[placement],
isOpen ? 'visible opacity-100' : 'invisible opacity-0',
tooltipClass
)}
>
{#if typeof content === 'string'}
{content}
{:else}
{@render content()}
{/if}
</span>
</span> Usage
<script>
import Tooltip from './Tooltip.svelte';
</script>
<!-- Basic usage with render props for aria-describedby -->
<Tooltip
id="tooltip-save"
content="Save your changes"
placement="top"
delay={300}
>
{#snippet children({ describedBy })}
<button aria-describedby={describedBy}>Save</button>
{/snippet}
</Tooltip>
<!-- Rich content using Snippet -->
<Tooltip id="tooltip-shortcut">
{#snippet content()}
<span class="flex items-center gap-1">
<kbd>Ctrl</kbd>+<kbd>S</kbd>
</span>
{/snippet}
{#snippet children({ describedBy })}
<button aria-describedby={describedBy}>Keyboard shortcut</button>
{/snippet}
</Tooltip> API
| Prop | Type | Default | Description |
|---|---|---|---|
id | string | - | Unique ID for the tooltip (required for SSR/hydration consistency) |
content | string | Snippet | - | Tooltip content (required) |
children | Snippet<[{ describedBy }]> | - | Render props pattern - receives describedBy for aria-describedby |
open | boolean | - | Controlled open state |
defaultOpen | boolean | false | Default open state (uncontrolled) |
onOpenChange | (open: boolean) => void | - | Callback when open state changes |
delay | number | 300 | Delay before showing (ms) |
placement | 'top' | 'bottom' | 'left' | 'right' | 'top' | Tooltip position |
disabled | boolean | false | Disable the tooltip |
Testing
Verify the component's rendered output using framework-specific testing libraries. These tests ensure correct HTML structure and ARIA attributes.
Testing Strategy
Unit Tests (Testing Library)
Verify the component's rendered output using framework-specific testing libraries. These tests ensure correct HTML structure and ARIA attributes.
- ARIA attributes (role, aria-describedby, aria-hidden)
- Keyboard interaction (Escape key dismissal)
- Show/hide behavior on focus and blur
- Accessibility via jest-axe
E2E Tests (Playwright)
Verify component behavior in a real browser environment across all frameworks. These tests cover interactions and cross-framework consistency.
- Hover interactions with delay timing
- Focus/blur interactions
- Escape key dismissal
- ARIA structure validation in live browser
- axe-core accessibility scanning
- Cross-framework consistency checks
Test Categories
APG ARIA Structure (Unit + E2E)
| Test | APG Requirement |
|---|---|
role="tooltip" | Tooltip container must have tooltip role |
aria-hidden | Hidden tooltips must have aria-hidden="true" |
aria-describedby | Trigger references tooltip when visible |
Show/Hide Behavior (Unit + E2E)
| Test | APG Requirement |
|---|---|
Hover shows | Shows tooltip on mouse hover after delay |
Focus shows | Shows tooltip on keyboard focus |
Blur hides | Hides tooltip on focus loss |
Mouseleave hides | Hides tooltip when mouse leaves trigger |
Keyboard Interaction (Unit + E2E)
| Test | APG Requirement |
|---|---|
Escape | Closes tooltip on Escape key |
Focus retention | Focus remains on trigger after Escape |
Disabled State (Unit + E2E)
| Test | WCAG Requirement |
|---|---|
Disabled no show | Disabled tooltip does not show on hover |
Accessibility (Unit + E2E)
| Test | WCAG Requirement |
|---|---|
axe violations (hidden) | No WCAG 2.1 AA violations when tooltip hidden |
axe violations (visible) | No WCAG 2.1 AA violations when tooltip visible |
Cross-framework Consistency (E2E)
| Test | Description |
|---|---|
All frameworks have tooltips | React, Vue, Svelte, Astro all render tooltip elements |
Show on hover | All frameworks show tooltip on hover |
Consistent ARIA | All frameworks have consistent ARIA structure |
Example Test Code
The following is the actual E2E test file (e2e/tooltip.spec.ts).
import { test, expect } from '@playwright/test';
import AxeBuilder from '@axe-core/playwright';
/**
* E2E Tests for Tooltip Pattern
*
* A tooltip is a popup that displays information related to an element
* when the element receives keyboard focus or the mouse hovers over it.
*
* APG Reference: https://www.w3.org/WAI/ARIA/apg/patterns/tooltip/
*/
const frameworks = ['react', 'vue', 'svelte', 'astro'] as const;
// Helper to get tooltip triggers (wrapper elements that contain tooltips)
const getTooltipTriggers = (page: import('@playwright/test').Page) => {
return page.locator('.apg-tooltip-trigger');
};
// Helper to get tooltip content
const getTooltip = (page: import('@playwright/test').Page) => {
return page.locator('[role="tooltip"]');
};
// Helper to get the element that should have aria-describedby
// In React/Vue/Astro: the wrapper span has aria-describedby
// In Svelte: the button inside has aria-describedby (passed via slot props)
const getDescribedByElement = (
_page: import('@playwright/test').Page,
framework: string,
trigger: import('@playwright/test').Locator
) => {
if (framework === 'svelte') {
return trigger.locator('button').first();
}
return trigger;
};
for (const framework of frameworks) {
test.describe(`Tooltip (${framework})`, () => {
test.beforeEach(async ({ page }) => {
await page.goto(`patterns/tooltip/${framework}/demo/`);
// Wait for tooltip triggers to be available
await getTooltipTriggers(page).first().waitFor();
});
// ------------------------------------------
// ๐ด High Priority: APG ARIA Structure
// ------------------------------------------
test.describe('APG: ARIA Structure', () => {
test('tooltip has role="tooltip"', async ({ page }) => {
const trigger = getTooltipTriggers(page).first();
const tooltip = getTooltip(page).first();
// Hover to show tooltip
await trigger.hover();
// Wait for tooltip to appear (default delay is 300ms)
await expect(tooltip).toBeVisible({ timeout: 1000 });
await expect(tooltip).toHaveRole('tooltip');
});
test('tooltip has aria-hidden when not visible', async ({ page }) => {
const tooltip = getTooltip(page).first();
await expect(tooltip).toHaveAttribute('aria-hidden', 'true');
});
test('trigger has aria-describedby when tooltip is shown', async ({ page }) => {
const trigger = getTooltipTriggers(page).first();
const describedByElement = getDescribedByElement(page, framework, trigger);
const tooltip = getTooltip(page).first();
// Hover to show tooltip
await trigger.hover();
await expect(tooltip).toBeVisible({ timeout: 1000 });
// After hover - has aria-describedby linking to tooltip
const tooltipId = await tooltip.getAttribute('id');
await expect(describedByElement).toHaveAttribute('aria-describedby', tooltipId!);
});
test('trigger removes aria-describedby when tooltip is hidden', async ({ page }) => {
// Svelte always has aria-describedby set (even when hidden) - skip this test for Svelte
if (framework === 'svelte') {
test.skip();
return;
}
const trigger = getTooltipTriggers(page).first();
const describedByElement = getDescribedByElement(page, framework, trigger);
const tooltip = getTooltip(page).first();
// Show tooltip
await trigger.hover();
await expect(tooltip).toBeVisible({ timeout: 1000 });
// Hide tooltip by moving mouse away
await page.locator('body').hover({ position: { x: 10, y: 10 } });
await expect(tooltip).not.toBeVisible();
// aria-describedby should be removed
const describedby = await describedByElement.getAttribute('aria-describedby');
expect(describedby).toBeNull();
});
});
// ------------------------------------------
// ๐ด High Priority: Show/Hide Behavior
// ------------------------------------------
test.describe('APG: Show/Hide Behavior', () => {
test('shows tooltip on hover after delay', async ({ page }) => {
const trigger = getTooltipTriggers(page).first();
const tooltip = getTooltip(page).first();
await expect(tooltip).not.toBeVisible();
await trigger.hover();
// Tooltip should appear after delay (300ms default)
await expect(tooltip).toBeVisible({ timeout: 1000 });
});
test('shows tooltip on focus', async ({ page }) => {
const trigger = getTooltipTriggers(page).first();
const focusable = trigger.locator('button, a, [tabindex="0"]').first();
const tooltip = getTooltip(page).first();
await expect(tooltip).not.toBeVisible();
// Click first to ensure page is focused, then Tab to element
await page.locator('body').click({ position: { x: 10, y: 10 } });
// Focus the element directly - use click to ensure focus event fires
await focusable.click();
await expect(focusable).toBeFocused();
// Tooltip should appear after delay
await expect(tooltip).toBeVisible({ timeout: 1000 });
});
test('hides tooltip on blur', async ({ page }) => {
const trigger = getTooltipTriggers(page).first();
const focusable = trigger.locator('button, a, [tabindex="0"]').first();
const tooltip = getTooltip(page).first();
// Show tooltip via click (which also focuses)
await focusable.click();
await expect(focusable).toBeFocused();
await expect(tooltip).toBeVisible({ timeout: 1000 });
// Blur by clicking outside
await page.locator('body').click({ position: { x: 10, y: 10 } });
await expect(tooltip).not.toBeVisible();
});
test('hides tooltip on mouseleave', async ({ page }) => {
const trigger = getTooltipTriggers(page).first();
const tooltip = getTooltip(page).first();
// Show tooltip via hover
await trigger.hover();
await expect(tooltip).toBeVisible({ timeout: 1000 });
// Move mouse away
await page.locator('body').hover({ position: { x: 10, y: 10 } });
await expect(tooltip).not.toBeVisible();
});
});
// ------------------------------------------
// ๐ด High Priority: Keyboard Interaction
// ------------------------------------------
test.describe('APG: Keyboard Interaction', () => {
test('hides tooltip on Escape key', async ({ page }) => {
const trigger = getTooltipTriggers(page).first();
const tooltip = getTooltip(page).first();
// Show tooltip via hover (more reliable than focus for this test)
await trigger.hover();
await expect(tooltip).toBeVisible({ timeout: 1000 });
// Press Escape
await page.keyboard.press('Escape');
await expect(tooltip).not.toBeVisible();
});
test('focus remains on trigger after Escape', async ({ page }) => {
const trigger = getTooltipTriggers(page).first();
const focusable = trigger.locator('button, a, [tabindex="0"]').first();
const tooltip = getTooltip(page).first();
// Show tooltip via click (which also focuses)
await focusable.click();
await expect(focusable).toBeFocused();
await expect(tooltip).toBeVisible({ timeout: 1000 });
// Press Escape
await page.keyboard.press('Escape');
await expect(tooltip).not.toBeVisible();
// Focus should remain on the focusable element
await expect(focusable).toBeFocused();
});
});
// ------------------------------------------
// ๐ก Medium Priority: Disabled State
// ------------------------------------------
test.describe('Disabled State', () => {
test('disabled tooltip does not show on hover', async ({ page }) => {
// Find the disabled tooltip trigger (4th one in demo)
const disabledTrigger = getTooltipTriggers(page).nth(3);
const tooltips = getTooltip(page);
// Get initial visible tooltip count
const initialVisibleCount = await tooltips
.filter({ has: page.locator(':visible') })
.count();
await disabledTrigger.hover();
// Wait a bit for potential tooltip to appear
await page.waitForTimeout(500);
// No new tooltip should be visible
const finalVisibleCount = await tooltips.filter({ has: page.locator(':visible') }).count();
expect(finalVisibleCount).toBe(initialVisibleCount);
});
});
// ------------------------------------------
// ๐ข Low Priority: Accessibility
// ------------------------------------------
test.describe('Accessibility', () => {
test('has no axe-core violations (tooltip hidden)', async ({ page }) => {
const trigger = getTooltipTriggers(page);
await trigger.first().waitFor();
const results = await new AxeBuilder({ page })
.include('.apg-tooltip-trigger')
// Exclude color-contrast - design choice for tooltip styling
.disableRules(['color-contrast'])
.analyze();
expect(results.violations).toEqual([]);
});
test('has no axe-core violations (tooltip visible)', async ({ page }) => {
const trigger = getTooltipTriggers(page).first();
const tooltip = getTooltip(page).first();
// Show tooltip
await trigger.hover();
await expect(tooltip).toBeVisible({ timeout: 1000 });
const results = await new AxeBuilder({ page })
.include('.apg-tooltip-trigger')
// Exclude color-contrast - design choice for tooltip styling
.disableRules(['color-contrast'])
.analyze();
expect(results.violations).toEqual([]);
});
});
});
}
// ============================================
// Cross-framework Consistency Tests
// ============================================
test.describe('Tooltip - Cross-framework Consistency', () => {
test('all frameworks have tooltips', async ({ page }) => {
for (const framework of frameworks) {
await page.goto(`patterns/tooltip/${framework}/demo/`);
await getTooltipTriggers(page).first().waitFor();
const triggers = getTooltipTriggers(page);
const count = await triggers.count();
expect(count).toBeGreaterThan(0);
}
});
test('all frameworks show tooltip on hover', async ({ page }) => {
// Run sequentially to avoid parallel test interference
test.setTimeout(60000);
for (const framework of frameworks) {
// Navigate fresh for each framework to avoid state leaking
await page.goto(`patterns/tooltip/${framework}/demo/`);
const trigger = getTooltipTriggers(page).first();
await trigger.waitFor();
const tooltip = getTooltip(page).first();
// Ensure tooltip is initially hidden
await expect(tooltip).toHaveAttribute('aria-hidden', 'true');
// Get bounding box for precise hover
const box = await trigger.boundingBox();
if (!box) throw new Error(`Trigger not found for ${framework}`);
// Move mouse away, then to center of trigger
await page.mouse.move(0, 0);
await page.mouse.move(box.x + box.width / 2, box.y + box.height / 2);
// Wait for tooltip to appear (300ms delay + buffer)
await expect(tooltip).toBeVisible({ timeout: 2000 });
// Move away to hide for next iteration
await page.mouse.move(0, 0);
await expect(tooltip).not.toBeVisible({ timeout: 1000 });
}
});
test('all frameworks have consistent ARIA structure', async ({ page }) => {
for (const framework of frameworks) {
await page.goto(`patterns/tooltip/${framework}/demo/`);
await getTooltipTriggers(page).first().waitFor();
const trigger = getTooltipTriggers(page).first();
const describedByElement = getDescribedByElement(page, framework, trigger);
const tooltip = getTooltip(page).first();
// Show tooltip
await trigger.hover();
await expect(tooltip).toBeVisible({ timeout: 1000 });
// Check role
await expect(tooltip).toHaveRole('tooltip');
// Check aria-describedby linkage
// Note: In React/Vue/Astro, aria-describedby is on the wrapper span
// In Svelte, it's on the button inside (passed via slot props)
const tooltipId = await tooltip.getAttribute('id');
await expect(describedByElement).toHaveAttribute('aria-describedby', tooltipId!);
// Move away to hide for next iteration
await page.locator('body').hover({ position: { x: 10, y: 10 } });
}
});
}); Running Tests
# Run unit tests for Tooltip
npm run test -- tooltip
# Run E2E tests for Tooltip (all frameworks)
npm run test:e2e:pattern --pattern=tooltip
# Run E2E tests for specific framework
npm run test:e2e:react:pattern --pattern=tooltip
npm run test:e2e:vue:pattern --pattern=tooltip
npm run test:e2e:svelte:pattern --pattern=tooltip
npm run test:e2e:astro:pattern --pattern=tooltip Testing Tools
- Vitest (opens in new tab) - Test runner for unit tests
- Testing Library (opens in new tab) - Framework-specific testing utilities (React, Vue, Svelte)
- Playwright (opens in new tab) - Browser automation for E2E tests
- axe-core/playwright (opens in new tab) - Automated accessibility testing in E2E
See testing-strategy.md (opens in new tab) for full documentation.
import { render, screen, waitFor } from '@testing-library/svelte';
import userEvent from '@testing-library/user-event';
import { axe } from 'jest-axe';
import { describe, expect, it, vi } from 'vitest';
import TooltipTestWrapper from './test-wrappers/TooltipTestWrapper.svelte';
describe('Tooltip (Svelte)', () => {
describe('APG: ARIA ๅฑๆง', () => {
it('role="tooltip" ใๆใค', () => {
render(TooltipTestWrapper, { props: { content: 'This is a tooltip' } });
expect(screen.getByRole('tooltip', { hidden: true })).toBeInTheDocument();
});
it('้่กจ็คบๆใฏ aria-hidden ใ true', () => {
render(TooltipTestWrapper, { props: { content: 'This is a tooltip' } });
const tooltip = screen.getByRole('tooltip', { hidden: true });
expect(tooltip).toHaveAttribute('aria-hidden', 'true');
});
it('่กจ็คบๆใฏ aria-hidden ใ false', async () => {
const user = userEvent.setup();
render(TooltipTestWrapper, { props: { content: 'This is a tooltip', delay: 0 } });
const trigger = screen.getByRole('button');
await user.hover(trigger);
await waitFor(() => {
const tooltip = screen.getByRole('tooltip');
expect(tooltip).toHaveAttribute('aria-hidden', 'false');
});
});
});
describe('APG: ใญใผใใผใๆไฝ', () => {
it('Escape ใญใผใง้ใใ', async () => {
const user = userEvent.setup();
render(TooltipTestWrapper, { props: { content: 'This is a tooltip', delay: 0 } });
const trigger = screen.getByRole('button');
await user.hover(trigger);
await waitFor(() => {
expect(screen.getByRole('tooltip')).toHaveAttribute('aria-hidden', 'false');
});
await user.keyboard('{Escape}');
await waitFor(() => {
expect(screen.getByRole('tooltip', { hidden: true })).toHaveAttribute(
'aria-hidden',
'true'
);
});
});
it('ใใฉใผใซในใง่กจ็คบใใใ', async () => {
const user = userEvent.setup();
render(TooltipTestWrapper, { props: { content: 'This is a tooltip', delay: 0 } });
await user.tab();
await waitFor(() => {
expect(screen.getByRole('tooltip')).toHaveAttribute('aria-hidden', 'false');
});
});
});
describe('ใใใผๆไฝ', () => {
it('ใใใผใง่กจ็คบใใใ', async () => {
const user = userEvent.setup();
render(TooltipTestWrapper, { props: { content: 'This is a tooltip', delay: 0 } });
await user.hover(screen.getByRole('button'));
await waitFor(() => {
expect(screen.getByRole('tooltip')).toHaveAttribute('aria-hidden', 'false');
});
});
it('ใใใผ่งฃ้คใง้ใใ', async () => {
const user = userEvent.setup();
render(TooltipTestWrapper, { props: { content: 'This is a tooltip', delay: 0 } });
const trigger = screen.getByRole('button');
await user.hover(trigger);
await waitFor(() => {
expect(screen.getByRole('tooltip')).toHaveAttribute('aria-hidden', 'false');
});
await user.unhover(trigger);
await waitFor(() => {
expect(screen.getByRole('tooltip', { hidden: true })).toHaveAttribute(
'aria-hidden',
'true'
);
});
});
});
describe('ใขใฏใปใทใใชใใฃ', () => {
it('axe ใซใใ WCAG 2.1 AA ้ๅใใชใ', async () => {
const { container } = render(TooltipTestWrapper, { props: { content: 'This is a tooltip' } });
const results = await axe(container);
expect(results).toHaveNoViolations();
});
it('tooltip ใใใฉใผใซในใๅใๅใใชใ', () => {
render(TooltipTestWrapper, { props: { content: 'This is a tooltip' } });
const tooltip = screen.getByRole('tooltip', { hidden: true });
expect(tooltip).not.toHaveAttribute('tabindex');
});
});
describe('Props', () => {
it('placement prop ใงไฝ็ฝฎใๅคๆดใงใใ', () => {
render(TooltipTestWrapper, { props: { content: 'Tooltip', placement: 'bottom' } });
const tooltip = screen.getByRole('tooltip', { hidden: true });
expect(tooltip).toHaveClass('top-full');
});
it('disabled ใฎๅ ดๅใtooltip ใ่กจ็คบใใใชใ', async () => {
const user = userEvent.setup();
render(TooltipTestWrapper, { props: { content: 'Tooltip', delay: 0, disabled: true } });
await user.hover(screen.getByRole('button'));
await new Promise((r) => setTimeout(r, 50));
expect(screen.getByRole('tooltip', { hidden: true })).toHaveAttribute('aria-hidden', 'true');
});
it('id prop ใงใซในใฟใ ID ใ่จญๅฎใงใใ', () => {
render(TooltipTestWrapper, { props: { content: 'Tooltip', id: 'custom-tooltip-id' } });
const tooltip = screen.getByRole('tooltip', { hidden: true });
expect(tooltip).toHaveAttribute('id', 'custom-tooltip-id');
});
});
}); Resources
- WAI-ARIA APG: Tooltip Pattern (opens in new tab)
- AI Implementation Guide (llm.md) (opens in new tab) - ARIA specs, keyboard support, test checklist