fix: wrap SearchPanel Enter keyDown in act() to prevent flaky test

The 'selects result on Enter' test could fail intermittently because
fireEvent.keyDown(window) fired before the useEffect re-registered
the keyboard handler after results loaded. Wrapping in act() ensures
effects are flushed before the event dispatches.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Test
2026-04-04 22:25:55 +02:00
parent d32b11f02e
commit 9fa1c52a96

View File

@@ -1,4 +1,4 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { SearchPanel } from './SearchPanel'
import type { VaultEntry } from '../types'
@@ -239,12 +239,12 @@ describe('SearchPanel', () => {
expect(screen.getByText('How to Design AI-first APIs')).toBeInTheDocument()
})
fireEvent.keyDown(window, { key: 'Enter' })
await waitFor(() => {
expect(onSelectNote).toHaveBeenCalledWith(MOCK_ENTRIES[0])
expect(onClose).toHaveBeenCalled()
await act(async () => {
fireEvent.keyDown(window, { key: 'Enter' })
})
expect(onSelectNote).toHaveBeenCalledWith(MOCK_ENTRIES[0])
expect(onClose).toHaveBeenCalled()
})
it('shows result count and elapsed time', async () => {