Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1.8 KB
Raw Blame
import { initKeaTests } from '~/test/init'

import { commandLogic } from './commandLogic'

describe('commandLogic', () => {
    let logic: ReturnType<typeof commandLogic.build>

    beforeEach(() => {
        initKeaTests()
        logic = commandLogic()
        logic.mount()
    })

    afterEach(() => {
        logic.unmount()
    })

    it('keeps the palette opening when the shortcut is pressed again before the input is ready', () => {
        logic.actions.toggleCommand('keyboard-shortcut')
        logic.actions.toggleCommand('keyboard-shortcut')
        expect(logic.values.isCommandOpen).toBe(true)

        logic.actions.commandInputReady()
        logic.actions.toggleCommand('keyboard-shortcut')
        expect(logic.values.isCommandOpen).toBe(false)
    })

    it('holds keys typed before the input is ready until the palette closes', () => {
        const pageInput = document.createElement('input')
        document.body.append(pageInput)
        pageInput.focus()
        logic.actions.toggleCommand('keyboard-shortcut')

        for (const key of ['d', 'a', 's', 'x', 'Backspace', 'h']) {
            pageInput.dispatchEvent(new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true }))
        }
        const paste = new Event('paste', { bubbles: true, cancelable: true }) as ClipboardEvent
        Object.defineProperty(paste, 'clipboardData', { value: { getData: () => 'board' } })
        pageInput.dispatchEvent(paste)
        expect(paste.defaultPrevented).toBe(true)

        logic.actions.commandInputReady()
        pageInput.dispatchEvent(new KeyboardEvent('keydown', { key: 'z', bubbles: true, cancelable: true }))
        expect(logic.values.pendingText).toBe('dashboard')

        logic.actions.closeCommand()
        expect(logic.values.pendingText).toBe('')
        pageInput.remove()
    })
})