2026-02-22 18:46:43 +01:00
|
|
|
import { describe, it, expect, vi, afterEach } from 'vitest'
|
|
|
|
|
import { renderHook } from '@testing-library/react'
|
|
|
|
|
import { useAppKeyboard } from './useAppKeyboard'
|
|
|
|
|
|
|
|
|
|
function fireKey(key: string, mods: { altKey?: boolean; metaKey?: boolean; ctrlKey?: boolean } = {}) {
|
|
|
|
|
const event = new KeyboardEvent('keydown', {
|
|
|
|
|
key,
|
|
|
|
|
altKey: mods.altKey ?? false,
|
|
|
|
|
metaKey: mods.metaKey ?? false,
|
|
|
|
|
ctrlKey: mods.ctrlKey ?? false,
|
|
|
|
|
bubbles: true,
|
|
|
|
|
cancelable: true,
|
|
|
|
|
})
|
|
|
|
|
window.dispatchEvent(event)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function makeActions() {
|
|
|
|
|
return {
|
|
|
|
|
onQuickOpen: vi.fn(),
|
2026-02-24 23:43:58 +01:00
|
|
|
onCommandPalette: vi.fn(),
|
2026-02-22 18:46:43 +01:00
|
|
|
onCreateNote: vi.fn(),
|
|
|
|
|
onSave: vi.fn(),
|
|
|
|
|
onOpenSettings: vi.fn(),
|
|
|
|
|
onTrashNote: vi.fn(),
|
|
|
|
|
onArchiveNote: vi.fn(),
|
|
|
|
|
onSetViewMode: vi.fn(),
|
|
|
|
|
activeTabPathRef: { current: '/vault/test.md' } as React.MutableRefObject<string | null>,
|
|
|
|
|
handleCloseTabRef: { current: vi.fn() } as React.MutableRefObject<(path: string) => void>,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('useAppKeyboard', () => {
|
|
|
|
|
afterEach(() => vi.restoreAllMocks())
|
|
|
|
|
|
2026-02-23 08:13:32 +01:00
|
|
|
it('Cmd+1 sets view mode to editor-only', () => {
|
2026-02-22 18:46:43 +01:00
|
|
|
const actions = makeActions()
|
|
|
|
|
renderHook(() => useAppKeyboard(actions))
|
2026-02-23 08:13:32 +01:00
|
|
|
fireKey('1', { metaKey: true })
|
2026-02-22 18:46:43 +01:00
|
|
|
expect(actions.onSetViewMode).toHaveBeenCalledWith('editor-only')
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-23 08:13:32 +01:00
|
|
|
it('Cmd+2 sets view mode to editor-list', () => {
|
2026-02-22 18:46:43 +01:00
|
|
|
const actions = makeActions()
|
|
|
|
|
renderHook(() => useAppKeyboard(actions))
|
2026-02-23 08:13:32 +01:00
|
|
|
fireKey('2', { metaKey: true })
|
2026-02-22 18:46:43 +01:00
|
|
|
expect(actions.onSetViewMode).toHaveBeenCalledWith('editor-list')
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-23 08:13:32 +01:00
|
|
|
it('Cmd+3 sets view mode to all', () => {
|
2026-02-22 18:46:43 +01:00
|
|
|
const actions = makeActions()
|
|
|
|
|
renderHook(() => useAppKeyboard(actions))
|
2026-02-23 08:13:32 +01:00
|
|
|
fireKey('3', { metaKey: true })
|
2026-02-22 18:46:43 +01:00
|
|
|
expect(actions.onSetViewMode).toHaveBeenCalledWith('all')
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-23 08:13:32 +01:00
|
|
|
it('does not fire view mode when Cmd+Alt pressed', () => {
|
2026-02-22 18:46:43 +01:00
|
|
|
const actions = makeActions()
|
|
|
|
|
renderHook(() => useAppKeyboard(actions))
|
2026-02-23 08:13:32 +01:00
|
|
|
fireKey('1', { metaKey: true, altKey: true })
|
2026-02-22 18:46:43 +01:00
|
|
|
expect(actions.onSetViewMode).not.toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('Cmd+P triggers quick open', () => {
|
|
|
|
|
const actions = makeActions()
|
|
|
|
|
renderHook(() => useAppKeyboard(actions))
|
|
|
|
|
fireKey('p', { metaKey: true })
|
|
|
|
|
expect(actions.onQuickOpen).toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('Cmd+N triggers create note', () => {
|
|
|
|
|
const actions = makeActions()
|
|
|
|
|
renderHook(() => useAppKeyboard(actions))
|
|
|
|
|
fireKey('n', { metaKey: true })
|
|
|
|
|
expect(actions.onCreateNote).toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('Cmd+W closes the active tab', () => {
|
|
|
|
|
const actions = makeActions()
|
|
|
|
|
renderHook(() => useAppKeyboard(actions))
|
|
|
|
|
fireKey('w', { metaKey: true })
|
|
|
|
|
expect(actions.handleCloseTabRef.current).toHaveBeenCalledWith('/vault/test.md')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('Alt+4 does not trigger any view mode', () => {
|
|
|
|
|
const actions = makeActions()
|
|
|
|
|
renderHook(() => useAppKeyboard(actions))
|
|
|
|
|
fireKey('4', { altKey: true })
|
|
|
|
|
expect(actions.onSetViewMode).not.toHaveBeenCalled()
|
|
|
|
|
})
|
2026-02-24 23:43:58 +01:00
|
|
|
|
|
|
|
|
it('Cmd+K triggers command palette', () => {
|
|
|
|
|
const actions = makeActions()
|
|
|
|
|
renderHook(() => useAppKeyboard(actions))
|
|
|
|
|
fireKey('k', { metaKey: true })
|
|
|
|
|
expect(actions.onCommandPalette).toHaveBeenCalled()
|
|
|
|
|
})
|
2026-02-22 18:46:43 +01:00
|
|
|
})
|