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.6 KB
Raw Blame
import { act, fireEvent, render, screen } from '@testing-library/react'

import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'

import { initKeaTests } from '~/test/init'

import { Command } from './Command'
import { commandLogic } from './commandLogic'

// On CI, the first open mounts enough connected logics to outlast the findBy and Jest default timeouts.
jest.setTimeout(20000)

const findSearchInput = (): Promise<HTMLInputElement> =>
    screen.findByPlaceholderText('Search or ask PostHog AI… try is:dashboard', {}, { timeout: 10000 })

describe('Command', () => {
    // Keeps the cold load of the lazy chunk out of the test. The lazy component still suspends on the first open.
    beforeAll(() => import('lib/components/CommandKSearch/CommandKSearch'))

    beforeEach(() => {
        initKeaTests()
        featureFlagLogic.mount()
        featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.NEW_COMMAND_K_SEARCH], {
            [FEATURE_FLAGS.NEW_COMMAND_K_SEARCH]: true,
        })
        commandLogic.mount()
    })

    it('opens Command K search with an empty query after it closes with a query', async () => {
        render(<Command />)
        act(() => commandLogic.actions.openCommand('keyboard-shortcut'))
        fireEvent.change(await findSearchInput(), { target: { value: 'work', selectionStart: 4 } })
        expect((await findSearchInput()).value).toBe('work')

        act(() => commandLogic.actions.closeCommand())
        act(() => commandLogic.actions.openCommand('keyboard-shortcut'))

        expect((await findSearchInput()).value).toBe('')
    })
})