|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act, type ReactNode } from 'react' |
| 5 | +import { createRoot, type Root } from 'react-dom/client' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const { mockIsMac } = vi.hoisted(() => ({ mockIsMac: vi.fn(() => false) })) |
| 9 | +vi.mock('@/lib/core/utils/platform', () => ({ isMacPlatform: mockIsMac })) |
| 10 | +vi.mock('next/navigation', () => ({ useRouter: () => ({ push: vi.fn() }) })) |
| 11 | + |
| 12 | +import { |
| 13 | + GlobalCommandsProvider, |
| 14 | + useRegisterGlobalCommands, |
| 15 | +} from '@/app/workspace/[workspaceId]/providers/global-commands-provider' |
| 16 | + |
| 17 | +function RegisterModK({ handler }: { handler: () => void }) { |
| 18 | + useRegisterGlobalCommands([{ id: 'search', shortcut: 'Mod+K', handler }]) |
| 19 | + return null |
| 20 | +} |
| 21 | + |
| 22 | +let container: HTMLDivElement |
| 23 | +let root: Root |
| 24 | + |
| 25 | +function mount(ui: ReactNode) { |
| 26 | + act(() => { |
| 27 | + root.render(ui) |
| 28 | + }) |
| 29 | +} |
| 30 | + |
| 31 | +/** Non-mac (mocked): `Mod` resolves to Ctrl, so Ctrl+K matches a `Mod+K` shortcut. */ |
| 32 | +function pressModK() { |
| 33 | + window.dispatchEvent( |
| 34 | + new KeyboardEvent('keydown', { key: 'k', ctrlKey: true, bubbles: true, cancelable: true }) |
| 35 | + ) |
| 36 | +} |
| 37 | + |
| 38 | +beforeEach(() => { |
| 39 | + container = document.createElement('div') |
| 40 | + document.body.appendChild(container) |
| 41 | + root = createRoot(container) |
| 42 | +}) |
| 43 | + |
| 44 | +afterEach(() => { |
| 45 | + act(() => root.unmount()) |
| 46 | + container.remove() |
| 47 | + vi.clearAllMocks() |
| 48 | +}) |
| 49 | + |
| 50 | +describe('GlobalCommandsProvider owned-shortcut yielding', () => { |
| 51 | + it('fires a global command when nothing owns the shortcut', () => { |
| 52 | + const handler = vi.fn() |
| 53 | + mount( |
| 54 | + <GlobalCommandsProvider> |
| 55 | + <RegisterModK handler={handler} /> |
| 56 | + </GlobalCommandsProvider> |
| 57 | + ) |
| 58 | + pressModK() |
| 59 | + expect(handler).toHaveBeenCalledTimes(1) |
| 60 | + }) |
| 61 | + |
| 62 | + it('yields the shortcut to a focused element that declares it owns it', () => { |
| 63 | + const handler = vi.fn() |
| 64 | + mount( |
| 65 | + <GlobalCommandsProvider> |
| 66 | + <RegisterModK handler={handler} /> |
| 67 | + {/* biome-ignore lint/a11y/noNoninteractiveTabindex: focusable stand-in for the editor */} |
| 68 | + <div data-owned-shortcuts='Mod+K' tabIndex={0} /> |
| 69 | + </GlobalCommandsProvider> |
| 70 | + ) |
| 71 | + ;(container.querySelector('[data-owned-shortcuts]') as HTMLElement).focus() |
| 72 | + pressModK() |
| 73 | + expect(handler).not.toHaveBeenCalled() |
| 74 | + }) |
| 75 | + |
| 76 | + it('still fires when the focused element owns only a different shortcut', () => { |
| 77 | + const handler = vi.fn() |
| 78 | + mount( |
| 79 | + <GlobalCommandsProvider> |
| 80 | + <RegisterModK handler={handler} /> |
| 81 | + {/* biome-ignore lint/a11y/noNoninteractiveTabindex: focusable stand-in for the editor */} |
| 82 | + <div data-owned-shortcuts='Mod+B' tabIndex={0} /> |
| 83 | + </GlobalCommandsProvider> |
| 84 | + ) |
| 85 | + ;(container.querySelector('[data-owned-shortcuts]') as HTMLElement).focus() |
| 86 | + pressModK() |
| 87 | + expect(handler).toHaveBeenCalledTimes(1) |
| 88 | + }) |
| 89 | +}) |
0 commit comments