GlobalShortcuts.test.tsx50 lines · main
1import { act, render, screen } from '@testing-library/react'
2import { beforeEach, describe, expect, it, vi } from 'vitest'
3
4import { GlobalShortcuts } from './GlobalShortcuts'
5import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
6
7const { mockSetCommandMenuOpen, mockUseShortcut } = vi.hoisted(() => ({
8 mockSetCommandMenuOpen: vi.fn(),
9 mockUseShortcut: vi.fn(),
10}))
11
12vi.mock('ui-patterns/CommandMenu', () => ({
13 useSetCommandMenuOpen: () => mockSetCommandMenuOpen,
14}))
15
16vi.mock('@/state/shortcuts/useShortcut', () => ({
17 useShortcut: mockUseShortcut,
18}))
19
20vi.mock('./ShortcutsReferenceSheet', () => ({
21 ShortcutsReferenceSheet: ({ open }: { open: boolean }) => (
22 <div data-testid="shortcuts-reference-sheet" data-open={open} />
23 ),
24}))
25
26describe('GlobalShortcuts', () => {
27 beforeEach(() => {
28 vi.clearAllMocks()
29 })
30
31 it('registers the shortcuts reference action in the command palette', () => {
32 render(<GlobalShortcuts />)
33
34 expect(mockUseShortcut).toHaveBeenCalledWith(
35 SHORTCUT_IDS.SHORTCUTS_OPEN_REFERENCE,
36 expect.any(Function),
37 { registerInCommandMenu: true }
38 )
39 })
40
41 it('closes the command palette and opens the shortcuts reference sheet', () => {
42 render(<GlobalShortcuts />)
43
44 const openReference = mockUseShortcut.mock.calls[0][1]
45 act(() => openReference())
46
47 expect(mockSetCommandMenuOpen).toHaveBeenCalledWith(false)
48 expect(screen.getByTestId('shortcuts-reference-sheet')).toHaveAttribute('data-open', 'true')
49 })
50})