Files
supabase/apps/studio/components/ui/Shortcut.test.tsx
Ali Waseem d67050c44e test(studio): add unit tests for shortcut primitives (#45162)
## Summary

Closes
[FE-3056](https://linear.app/supabase/issue/FE-3056/add-unit-tests-for-new-shortcut-primitives).

Backfills unit coverage for the shortcut primitives added in the recent
keyboard-shortcut overhaul — these shipped with no tests.

- **`state/shortcuts/formatShortcut.test.ts`** (16 tests) —
`hotkeyToKeys`: `Mod → Meta` mapping (case-sensitive, substring-safe),
single/multi-modifier combos, named keys, punctuation, edge cases.
- **`state/shortcuts/useShortcut.test.tsx`** (26 tests) — mocks
`useHotkeySequence`, `useRegisterCommands`, and `useIsShortcutEnabled`
to unit-test: sequence/callback wiring (single-step + multi-step
G-chords), full option-resolution priority (caller → registry →
fallback) for `enabled` / `timeout` / `ignoreInputs`, global-preference
AND gating, command-menu registration across all four enable
permutations, stable action identity, stale-closure protection via
`useLatest`, and badge rendering with/without the "then" separator.
- **`components/ui/Shortcut.test.tsx`** (12 tests) — mocks `useShortcut`
and `ShortcutTooltip` to verify prop forwarding (including rerender
behavior), tooltip positioning props, unset-prop hygiene, and the
`asChild` pass-through contract (clicks on the child do not fire
`onTrigger`).

54 tests total, all passing.

## Test plan

- [x] `pnpm --filter studio test state/shortcuts/formatShortcut.test.ts
state/shortcuts/useShortcut.test.tsx components/ui/Shortcut.test.tsx`
- [x] `tsc --noEmit` clean across the three test files

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Tests**
* Added comprehensive test coverage for the Shortcut component,
verifying rendering, hook integration, and tooltip behavior.
* Added test suite for hotkey key formatting, covering plain keys, named
keys, modifiers, and edge cases.
* Added test suite for the useShortcut hook, validating hotkey
sequences, enablement logic, command menu integration, and callback
handling.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-04-23 09:22:16 -06:00

198 lines
6.1 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>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>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>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>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>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>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>Go</button>
</Shortcut>
)
expect(mockUseShortcut).toHaveBeenLastCalledWith(SHORTCUT_IDS.NAV_HOME, handler, undefined)
rerender(
<Shortcut id={SHORTCUT_IDS.NAV_TABLE_EDITOR} onTrigger={handler}>
<button>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>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>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>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>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 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>Open</button>
</Shortcut>
)
expect(onTrigger).not.toHaveBeenCalled()
})
})
})