GlobalShortcuts.test.tsx 1.5 KB

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