Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/js/components/search-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function SearchDialog({
};

const onKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
if (options.length === 0) {
if (event.nativeEvent.isComposing || options.length === 0) {
return;
}

Expand Down
99 changes: 99 additions & 0 deletions tests/Unit/SearchDialogComposition.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/// <reference types="node" />
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<Record<string, unknown>>;

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);
});
1 change: 1 addition & 0 deletions tests/draft-images.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
},
});
1 change: 1 addition & 0 deletions tests/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
Expand Down
Loading