Files
supabase/apps/studio/components/ui/Shortcut.test.tsx
Danny White 6f6badae51 fix(eslint): promote require-explicit-tabindex to error (#48170)
## What kind of change does this PR introduce?

Accessibility / lint hardening (Safari keyboard focus).

## What is the current behavior?

`supabase/require-explicit-tabindex` is `'warn'`. Studio’s ratchet was
at 0 but the rule was still ratcheted; www / docs / design-system still
had raw `<button>` / `role="button"` call sites without an explicit
`tabIndex`.

[DEPR-627](https://linear.app/supabase/issue/DEPR-627) · follow-up to
#47984 / #48040

## What is the new behavior?

- Shared config: `'supabase/require-explicit-tabindex': 'error'`
- Swept www / docs / design-system (+ Studio test fixtures the ratchet
skipped)
- Removed the rule from the Studio ratchet + baselines

## To test

Prefer **Safari**. This PR only adds explicit `tabIndex` to raw
`<button>` / `role="button"` call sites — not links, and not controls
that already go through `Button` from `ui`.

### Marketing (`www`) ([staging
link](https://zone-www-dot-com-git-danny-depr-627-promote-req-7ae43c-supabase.vercel.app/))

- [x] Homepage frameworks / dashboard feature tabs — Tab through each
tab button
- [x] Product pages (e.g. `/auth`, `/database`) — section tab switchers
- [x] Narrow viewport — open the hamburger; Tab through menu buttons
- [x] `/partners/catalog` — filter / view controls
- [x] Blog view toggle (list ↔ grid)

### Docs ([staging
link](https://docs-git-danny-depr-627-promote-require-explici-25e46d-supabase.vercel.app/))

- [x] **Desktop (≥ lg):** top-right **⋯ menu** (hamburger icon) — opens
a dropdown that includes Theme. Not a separate theme button.
- [x] **Mobile (< lg):** top-right **hamburger** opens the sheet; close
(X) is the raw button we tagged. Theme inside the sheet uses
`ThemeToggle` / `DropdownMenuTrigger` from `ui` (already supposed to set
`tabIndex`).
- [x] **Code blocks** — copy / language controls
- [x] **Is this helpful?** — X / check are `Button` from `ui` (should
already Tab). After voting **while signed in**, the follow-up “What went
well?” / “How can we improve?” text button is the raw one we tagged.
- [x] **AI Tools → Copy as Markdown** (right rail on a guide) — this is
the only GuidesSidebar control this PR changed. “On this page” TOC items
are **links**, not covered by this lint.
- [x] **Reference docs** (e.g. JS client reference) — section headers
that expand/collapse in the left nav (`Collapsible.Trigger`)
- [x] **Troubleshooting index** — type in the search field, then Tab to
the **clear (X)** control

### Dashboard (`studio`)

No production UI changes in this PR (tests + lint config only). Quick
Safari smoke that prior tabindex work still holds:

- [x] Project sidebar — Tab through primary nav links
- [x] Settings → General — Tab through inputs / buttons
- [x] Storage → Files — Tab a bucket row / file actions
2026-07-23 05:21:15 +10:00

200 lines
6.3 KiB
TypeScript

import { fireEvent, render, screen } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { Shortcut } from './Shortcut'
import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
const { mockUseShortcut, mockShortcutTooltip } = vi.hoisted(() => ({
mockUseShortcut: vi.fn(),
mockShortcutTooltip: vi.fn((props: any) => (
<div data-testid="shortcut-tooltip">{props.children}</div>
)),
}))
vi.mock('@/state/shortcuts/useShortcut', () => ({
useShortcut: mockUseShortcut,
}))
vi.mock('./ShortcutTooltip', () => ({
ShortcutTooltip: mockShortcutTooltip,
}))
describe('Shortcut', () => {
beforeEach(() => {
vi.clearAllMocks()
})
describe('rendering', () => {
it('renders the wrapped child', () => {
render(
<Shortcut id={SHORTCUT_IDS.COMMAND_MENU_OPEN} onTrigger={() => {}}>
<button tabIndex={0}>Open</button>
</Shortcut>
)
expect(screen.getByRole('button', { name: 'Open' })).toBeInTheDocument()
})
it('wraps the child in ShortcutTooltip', () => {
render(
<Shortcut id={SHORTCUT_IDS.COMMAND_MENU_OPEN} onTrigger={() => {}}>
<button tabIndex={0}>Open</button>
</Shortcut>
)
const tooltip = screen.getByTestId('shortcut-tooltip')
expect(tooltip).toContainElement(screen.getByRole('button', { name: 'Open' }))
})
})
describe('useShortcut wiring', () => {
it('forwards id and onTrigger to useShortcut', () => {
const handler = vi.fn()
render(
<Shortcut id={SHORTCUT_IDS.ACTION_BAR_SAVE} onTrigger={handler}>
<button tabIndex={0}>Save</button>
</Shortcut>
)
expect(mockUseShortcut).toHaveBeenCalledWith(SHORTCUT_IDS.ACTION_BAR_SAVE, handler, undefined)
})
it('forwards options to useShortcut', () => {
const handler = vi.fn()
const options = { enabled: true, registerInCommandMenu: true }
render(
<Shortcut id={SHORTCUT_IDS.ACTION_BAR_SAVE} onTrigger={handler} options={options}>
<button tabIndex={0}>Save</button>
</Shortcut>
)
expect(mockUseShortcut).toHaveBeenCalledWith(SHORTCUT_IDS.ACTION_BAR_SAVE, handler, options)
})
it('passes updated options to useShortcut on rerender', () => {
const handler = vi.fn()
const { rerender } = render(
<Shortcut
id={SHORTCUT_IDS.ACTION_BAR_SAVE}
onTrigger={handler}
options={{ enabled: false }}
>
<button tabIndex={0}>Save</button>
</Shortcut>
)
expect(mockUseShortcut).toHaveBeenLastCalledWith(SHORTCUT_IDS.ACTION_BAR_SAVE, handler, {
enabled: false,
})
rerender(
<Shortcut id={SHORTCUT_IDS.ACTION_BAR_SAVE} onTrigger={handler} options={{ enabled: true }}>
<button tabIndex={0}>Save</button>
</Shortcut>
)
expect(mockUseShortcut).toHaveBeenLastCalledWith(SHORTCUT_IDS.ACTION_BAR_SAVE, handler, {
enabled: true,
})
})
it('passes updated id to useShortcut on rerender', () => {
const handler = vi.fn()
const { rerender } = render(
<Shortcut id={SHORTCUT_IDS.NAV_HOME} onTrigger={handler}>
<button tabIndex={0}>Go</button>
</Shortcut>
)
expect(mockUseShortcut).toHaveBeenLastCalledWith(SHORTCUT_IDS.NAV_HOME, handler, undefined)
rerender(
<Shortcut id={SHORTCUT_IDS.NAV_TABLE_EDITOR} onTrigger={handler}>
<button tabIndex={0}>Go</button>
</Shortcut>
)
expect(mockUseShortcut).toHaveBeenLastCalledWith(
SHORTCUT_IDS.NAV_TABLE_EDITOR,
handler,
undefined
)
})
})
describe('ShortcutTooltip wiring', () => {
it('forwards shortcutId to ShortcutTooltip', () => {
render(
<Shortcut id={SHORTCUT_IDS.NAV_HOME} onTrigger={() => {}}>
<button tabIndex={0}>Home</button>
</Shortcut>
)
expect(mockShortcutTooltip).toHaveBeenCalled()
expect(mockShortcutTooltip.mock.calls.at(-1)![0].shortcutId).toBe(SHORTCUT_IDS.NAV_HOME)
})
it('forwards tooltip positioning props', () => {
render(
<Shortcut
id={SHORTCUT_IDS.NAV_HOME}
onTrigger={() => {}}
side="right"
align="start"
sideOffset={8}
delayDuration={100}
>
<button tabIndex={0}>Home</button>
</Shortcut>
)
const props = mockShortcutTooltip.mock.calls.at(-1)![0]
expect(props.side).toBe('right')
expect(props.align).toBe('start')
expect(props.sideOffset).toBe(8)
expect(props.delayDuration).toBe(100)
})
it('forwards label override to ShortcutTooltip', () => {
render(
<Shortcut id={SHORTCUT_IDS.NAV_HOME} onTrigger={() => {}} label="Go home">
<button tabIndex={0}>Home</button>
</Shortcut>
)
expect(mockShortcutTooltip.mock.calls.at(-1)![0].label).toBe('Go home')
})
it('omits undefined positioning props rather than fabricating them', () => {
render(
<Shortcut id={SHORTCUT_IDS.NAV_HOME} onTrigger={() => {}}>
<button tabIndex={0}>Home</button>
</Shortcut>
)
const props = mockShortcutTooltip.mock.calls.at(-1)![0]
expect(props.side).toBeUndefined()
expect(props.align).toBeUndefined()
expect(props.sideOffset).toBeUndefined()
expect(props.delayDuration).toBeUndefined()
expect(props.label).toBeUndefined()
})
})
describe('child interactivity', () => {
it('does not intercept clicks on the wrapped child', () => {
const onClick = vi.fn()
const onTrigger = vi.fn()
render(
<Shortcut id={SHORTCUT_IDS.COMMAND_MENU_OPEN} onTrigger={onTrigger}>
<button tabIndex={0} onClick={onClick}>
Open
</button>
</Shortcut>
)
fireEvent.click(screen.getByRole('button', { name: 'Open' }))
expect(onClick).toHaveBeenCalledTimes(1)
expect(onTrigger).not.toHaveBeenCalled()
})
it('does not invoke onTrigger during render — only the hotkey should trigger it', () => {
const onTrigger = vi.fn()
render(
<Shortcut id={SHORTCUT_IDS.COMMAND_MENU_OPEN} onTrigger={onTrigger}>
<button tabIndex={0}>Open</button>
</Shortcut>
)
expect(onTrigger).not.toHaveBeenCalled()
})
})
})