import { fireEvent, render, screen, waitFor } from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import { useState } from 'react'; import { describe, expect, it, vi, beforeEach } from 'vitest'; import { Command, CommandDialog, CommandInput, CommandItem, CommandList } from './command'; import { fuzzyFilter } from '@/lib/fuzzyMatch'; import { CommandDialogContent, CommandDialogRoot, } from '@/modules/chatroom/components/shared/CommandDialogContent'; vi.mock('@/hooks/useIsDesktop', () => ({ useIsDesktop: vi.fn(() => true), })); beforeEach(() => { Element.prototype.scrollIntoView = vi.fn(); }); describe('CommandItem click after search', () => { it('uses sharp industrial command surfaces and items', () => { render( Item ); expect(screen.getByRole('combobox')).toHaveClass('rounded-none'); expect(screen.getByRole('combobox').parentElement).toHaveClass('border-chatroom-border'); expect(screen.getByText('Item')).toHaveClass('rounded-none'); expect(screen.getByRole('listbox').parentElement).toHaveClass('rounded-none'); }); it('composes CommandDialog over the shared industrial Dialog', () => { render( ); expect(screen.getByRole('dialog')).toHaveClass('rounded-none', 'bg-chatroom-bg-primary'); }); it('fires onSelect when clicking a filtered item without spurious dismiss', async () => { const user = userEvent.setup(); const onSelectAlpha = vi.fn(); const onSelectBeta = vi.fn(); const onOpenChange = vi.fn(); render( Alpha Beta ); await user.click(screen.getByPlaceholderText('Search...')); await user.type(screen.getByPlaceholderText('Search...'), 'alp'); const alphaItem = await screen.findByText('Alpha'); expect(screen.queryByText('Beta')).toBeNull(); await user.click(alphaItem); await waitFor(() => { expect(onSelectAlpha).toHaveBeenCalledTimes(1); }); expect(onSelectBeta).not.toHaveBeenCalled(); expect(onOpenChange).not.toHaveBeenCalled(); }); it('fires onSelect via Enter on filtered item', async () => { const user = userEvent.setup(); const onSelect = vi.fn(); render( Alpha ); await user.type(screen.getByPlaceholderText('Search...'), 'alp'); await user.keyboard('{Enter}'); await waitFor(() => { expect(onSelect).toHaveBeenCalledTimes(1); }); }); it('fires onSelect on pointer tap after search (touch path)', async () => { const user = userEvent.setup(); const onSelectAlpha = vi.fn(); const onSelectBeta = vi.fn(); const onPointerDown = vi.fn(); render( Alpha Beta ); await user.type(screen.getByPlaceholderText('Search...'), 'alp'); const alphaItem = await screen.findByText('Alpha'); expect(screen.queryByText('Beta')).toBeNull(); // pointerdown must preventDefault so the command input does not blur and // the item stays in the user-gesture stack for cmdk selection. fireEvent.pointerDown(alphaItem); expect(onPointerDown).toHaveBeenCalledTimes(1); expect(onPointerDown.mock.calls[0]?.[0].defaultPrevented).toBe(true); // Simulate the rest of a pointer tap (pointerup → synthesized click). fireEvent.pointerUp(alphaItem); fireEvent.click(alphaItem); await waitFor(() => { expect(onSelectAlpha).toHaveBeenCalledTimes(1); }); expect(onSelectBeta).not.toHaveBeenCalled(); }); it('fires onSelect on touch tap after search', async () => { const user = userEvent.setup(); const onSelect = vi.fn(); render( Alpha ); await user.type(screen.getByPlaceholderText('Search...'), 'alp'); const alphaItem = await screen.findByText('Alpha'); // Simulate a touch tap (touchstart → touchend → synthesized click). fireEvent.touchStart(alphaItem); fireEvent.touchEnd(alphaItem); fireEvent.click(alphaItem); await waitFor(() => { expect(onSelect).toHaveBeenCalledTimes(1); }); }); it('fires onSelect with controlled open state via CommandDialogRoot (no spurious dismiss)', async () => { const user = userEvent.setup(); const onSelect = vi.fn(); const onOpenChange = vi.fn(); function Harness() { const [open, setOpen] = useState(true); return ( { onOpenChange(next); setOpen(next); }} > { onSelect(); setOpen(false); }} > Alpha Beta ); } render(); await user.type(screen.getByPlaceholderText('Search...'), 'alp'); await user.click(await screen.findByText('Alpha')); await waitFor(() => { expect(onSelect).toHaveBeenCalledTimes(1); }); // Base UI must not spuriously dismiss before cmdk onSelect (would call onOpenChange(false) first). expect(onOpenChange).not.toHaveBeenCalled(); }); });