diff --git a/resources/js/components/search-dialog.tsx b/resources/js/components/search-dialog.tsx index 5661b73fed..9319c8ca22 100644 --- a/resources/js/components/search-dialog.tsx +++ b/resources/js/components/search-dialog.tsx @@ -143,7 +143,7 @@ export function SearchDialog({ }; const onKeyDown = (event: KeyboardEvent) => { - if (options.length === 0) { + if (event.nativeEvent.isComposing || options.length === 0) { return; } diff --git a/tests/Unit/SearchDialogComposition.test.tsx b/tests/Unit/SearchDialogComposition.test.tsx new file mode 100644 index 0000000000..5c91d84891 --- /dev/null +++ b/tests/Unit/SearchDialogComposition.test.tsx @@ -0,0 +1,99 @@ +/// +import assert from 'node:assert/strict'; +import * as React from 'react'; +import type { ReactElement, ReactNode } from 'react'; +import { test, vi } from 'vite-plus/test'; + +let state: unknown[] = []; +let cursor = 0; +const visit = vi.fn(); + +vi.doMock('react', () => ({ + ...React, + useEffect: () => {}, + useSyncExternalStore: (_subscribe: unknown, snapshot: () => unknown) => + snapshot(), + useState: (initial: unknown) => { + const index = cursor++; + if (!(index in state)) { + state[index] = typeof initial === 'function' ? initial() : initial; + } + return [ + state[index], + (value: unknown) => { + state[index] = + typeof value === 'function' ? value(state[index]) : value; + }, + ]; + }, +})); +vi.doMock('@inertiajs/react', async () => ({ + ...(await vi.importActual('@inertiajs/react')), + router: { visit }, +})); + +const { SearchDialog } = + await import('../../resources/js/components/search-dialog'); + +type Element = ReactElement>; + +function elements(node: ReactNode): Element[] { + if (!React.isValidElement(node)) { + return []; + } + const element = node as Element; + return [ + element, + ...React.Children.toArray(element.props.children as ReactNode).flatMap( + elements, + ), + ]; +} + +function input() { + cursor = 0; + const dialog = SearchDialog({ open: true, onOpenChange: () => {} }); + return elements(dialog).find( + (element) => element.props['data-test'] === 'search-input', + )!; +} + +function keyDown(key: string, isComposing: boolean) { + const event = { + key, + nativeEvent: { isComposing }, + preventDefault: vi.fn(), + }; + (input().props.onKeyDown as (event: unknown) => void)(event); + return event; +} + +test('leaves Enter and the arrows to the IME while composing', () => { + state = []; + assert.equal(input().props.value, ''); + assert.equal( + input().props['aria-activedescendant'], + 'search-suggestion-0', + ); + + const composedEnter = keyDown('Enter', true); + assert.equal(composedEnter.preventDefault.mock.calls.length, 0); + assert.equal(input().props.value, ''); + + const composedArrow = keyDown('ArrowDown', true); + assert.equal(composedArrow.preventDefault.mock.calls.length, 0); + assert.equal( + input().props['aria-activedescendant'], + 'search-suggestion-0', + ); + + keyDown('ArrowDown', false); + assert.equal( + input().props['aria-activedescendant'], + 'search-suggestion-1', + ); + const enter = keyDown('Enter', false); + assert.equal(enter.preventDefault.mock.calls.length, 1); + assert.equal(input().props.value, 'debugging in production'); + assert.equal(visit.mock.calls.length, 0); +}); diff --git a/tests/draft-images.config.ts b/tests/draft-images.config.ts index 81c1aaadb7..481d657046 100644 --- a/tests/draft-images.config.ts +++ b/tests/draft-images.config.ts @@ -16,6 +16,7 @@ export default defineConfig({ 'tests/Unit/EmojiSearch.test.tsx', 'tests/Unit/EmojiShortcodes.test.tsx', 'tests/Unit/QuoteReply.test.tsx', + 'tests/Unit/SearchDialogComposition.test.tsx', ], }, }); diff --git a/tests/tsconfig.json b/tests/tsconfig.json index 6603e1ca73..75337d9f0f 100644 --- a/tests/tsconfig.json +++ b/tests/tsconfig.json @@ -8,6 +8,7 @@ "Unit/**/*.test.ts", "Unit/DraftStash.test.tsx", "Unit/ComposerDraft.test.tsx", + "Unit/SearchDialogComposition.test.tsx", "../resources/js/types/*.d.ts", "draft-images.config.ts" ]