|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * An Enter the input method owns does only what the input method does with it |
| 11 | + * (objectstack#35996), in the three widgets objectstack#35797 left out: |
| 12 | + * |
| 13 | + * - `GridField`: a cell's Enter moves to the next line, and on the last line |
| 14 | + * appends one; the composed text then landed in the wrong line. |
| 15 | + * - `DatePickerField`: Enter commits the typed date text. |
| 16 | + * - `RecordPickerDialog`: Enter in the page-jump box jumps and clears it. |
| 17 | + * |
| 18 | + * Each widget is driven through the two sequences browsers really send |
| 19 | + * (Chromium / Firefox: the picking keydown inside the composition, with |
| 20 | + * `isComposing: true` and keyCode 229; Safari: the 229 keydown after |
| 21 | + * `compositionend`), and each case ends with the control: a plain Enter does |
| 22 | + * what the widget always did, so a guard that swallowed every Enter goes red. |
| 23 | + * |
| 24 | + * The predicate itself is pinned once, in `imeComposition-35797.test.tsx`; |
| 25 | + * `./imeKey` is the pass-through of the one definition on the |
| 26 | + * `@object-ui/components` entry, which the identity case below pins. |
| 27 | + */ |
| 28 | + |
| 29 | +import * as React from 'react'; |
| 30 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 31 | +import { render, screen, cleanup, waitFor, fireEvent } from '@testing-library/react'; |
| 32 | +import '@testing-library/jest-dom'; |
| 33 | + |
| 34 | +import { isImeKey as componentsIsImeKey } from '@object-ui/components'; |
| 35 | +import { isImeKey } from './imeKey'; |
| 36 | +import { GridField } from './GridField'; |
| 37 | +import { DatePickerField } from './date-picker/DatePickerField'; |
| 38 | +import { RecordPickerDialog } from './RecordPickerDialog'; |
| 39 | +import { getCellRenderer } from '../index'; |
| 40 | + |
| 41 | +/** The keydown that picks a candidate while the composition is open. */ |
| 42 | +const COMPOSING_ENTER = { key: 'Enter', code: 'Enter', keyCode: 229, which: 229, isComposing: true } as const; |
| 43 | +/** Safari's candidate-picking Enter, raised after `compositionend`. */ |
| 44 | +const SAFARI_POST_COMPOSITION_ENTER = { key: 'Enter', code: 'Enter', keyCode: 229, which: 229, isComposing: false } as const; |
| 45 | +/** An Enter the input method does not own. */ |
| 46 | +const PLAIN_ENTER = { key: 'Enter', code: 'Enter', keyCode: 13, which: 13 } as const; |
| 47 | + |
| 48 | +type Composer = (el: HTMLElement, text: string) => void; |
| 49 | +const type = (el: HTMLElement, text: string) => fireEvent.change(el, { target: { value: text } }); |
| 50 | + |
| 51 | +/** Chromium / Firefox: the picking keydown arrives inside the composition. */ |
| 52 | +const composeChromium: Composer = (el, text) => { |
| 53 | + fireEvent.compositionStart(el, { data: '' }); |
| 54 | + type(el, text); |
| 55 | + fireEvent.keyDown(el, COMPOSING_ENTER); |
| 56 | + fireEvent.compositionEnd(el, { data: text }); |
| 57 | +}; |
| 58 | + |
| 59 | +/** Safari: the composition has ended when the picking keydown arrives. */ |
| 60 | +const composeSafari: Composer = (el, text) => { |
| 61 | + fireEvent.compositionStart(el, { data: '' }); |
| 62 | + type(el, text); |
| 63 | + fireEvent.compositionEnd(el, { data: text }); |
| 64 | + fireEvent.keyDown(el, SAFARI_POST_COMPOSITION_ENTER); |
| 65 | +}; |
| 66 | + |
| 67 | +const BROWSERS = [['Chromium', composeChromium], ['Safari', composeSafari]] as const; |
| 68 | + |
| 69 | +afterEach(() => { |
| 70 | + cleanup(); |
| 71 | + vi.restoreAllMocks(); |
| 72 | +}); |
| 73 | + |
| 74 | +describe('one IME-key predicate in the repository (objectstack#35996)', () => { |
| 75 | + it('the fields widgets ask the very function the components entry exports', () => { |
| 76 | + expect(isImeKey).toBe(componentsIsImeKey); |
| 77 | + }); |
| 78 | +}); |
| 79 | + |
| 80 | +describe('GridField — an IME Enter in a cell neither moves nor appends a line (objectstack#35996)', () => { |
| 81 | + const FIELD = { name: 'lines', type: 'grid', columns: [{ name: 'desc', type: 'text', label: 'Desc' }] } as never; |
| 82 | + |
| 83 | + function mountGrid() { |
| 84 | + const onChange = vi.fn(); |
| 85 | + const Host = () => { |
| 86 | + const [value, setValue] = React.useState<Record<string, unknown>[]>([{ desc: 'a' }, { desc: 'b' }]); |
| 87 | + return ( |
| 88 | + <GridField |
| 89 | + value={value} |
| 90 | + onChange={(v: Record<string, unknown>[]) => { |
| 91 | + onChange(v); |
| 92 | + setValue(v); |
| 93 | + }} |
| 94 | + field={FIELD} |
| 95 | + /> |
| 96 | + ); |
| 97 | + }; |
| 98 | + const { container } = render(<Host />); |
| 99 | + const cell = (row: number) => container.querySelector(`[data-cell="${row}-0"]`) as HTMLInputElement | null; |
| 100 | + return { cell, onChange }; |
| 101 | + } |
| 102 | + |
| 103 | + for (const [browser, compose] of BROWSERS) { |
| 104 | + it(`${browser}: the candidate-picking Enter keeps focus on its line; the next Enter moves down`, () => { |
| 105 | + const { cell } = mountGrid(); |
| 106 | + cell(0)!.focus(); |
| 107 | + compose(cell(0)!, '你'); |
| 108 | + expect(document.activeElement).toBe(cell(0)); |
| 109 | + expect(cell(0)!.value).toBe('你'); |
| 110 | + |
| 111 | + fireEvent.keyDown(cell(0)!, PLAIN_ENTER); |
| 112 | + expect(document.activeElement).toBe(cell(1)); |
| 113 | + }); |
| 114 | + |
| 115 | + it(`${browser}: on the last line the candidate-picking Enter appends no line; the next Enter does`, async () => { |
| 116 | + const { cell, onChange } = mountGrid(); |
| 117 | + cell(1)!.focus(); |
| 118 | + compose(cell(1)!, '好'); |
| 119 | + // The composed text is the one write; no third line. |
| 120 | + expect(onChange.mock.calls.every(([rows]) => (rows as unknown[]).length === 2)).toBe(true); |
| 121 | + expect(cell(2)).toBeNull(); |
| 122 | + expect(document.activeElement).toBe(cell(1)); |
| 123 | + |
| 124 | + fireEvent.keyDown(cell(1)!, PLAIN_ENTER); |
| 125 | + expect(onChange).toHaveBeenLastCalledWith([expect.anything(), expect.anything(), expect.anything()]); |
| 126 | + await waitFor(() => expect(document.activeElement).toBe(cell(2))); |
| 127 | + }); |
| 128 | + } |
| 129 | +}); |
| 130 | + |
| 131 | +describe('DatePickerField — an IME Enter does not commit the typed text (objectstack#35996)', () => { |
| 132 | + const FIELD = { name: 'close_date', type: 'date' } as never; |
| 133 | + |
| 134 | + for (const [browser, compose] of BROWSERS) { |
| 135 | + it(`${browser}: the candidate-picking Enter commits nothing; the next Enter commits the date`, () => { |
| 136 | + const onChange = vi.fn(); |
| 137 | + render(<DatePickerField value="" onChange={onChange} field={FIELD} />); |
| 138 | + const input = screen.getByRole('textbox') as HTMLInputElement; |
| 139 | + input.focus(); |
| 140 | + compose(input, '10/16/2026'); |
| 141 | + expect(onChange).not.toHaveBeenCalled(); |
| 142 | + expect(input).not.toHaveAttribute('aria-invalid', 'true'); |
| 143 | + expect(input.value).toBe('10/16/2026'); |
| 144 | + |
| 145 | + fireEvent.keyDown(input, PLAIN_ENTER); |
| 146 | + expect(onChange).toHaveBeenLastCalledWith('2026-10-16'); |
| 147 | + }); |
| 148 | + } |
| 149 | +}); |
| 150 | + |
| 151 | +describe('RecordPickerDialog — an IME Enter in the page-jump box neither jumps nor clears it (objectstack#35996)', () => { |
| 152 | + const USERS = Array.from({ length: 12 }, (_, i) => { |
| 153 | + const n = String(i + 1).padStart(2, '0'); |
| 154 | + return { id: `u${n}`, name: `User ${n}` }; |
| 155 | + }); |
| 156 | + const FIELDS = { name: { type: 'text', label: 'Name' } }; |
| 157 | + |
| 158 | + function mountPicker() { |
| 159 | + const dataSource = { |
| 160 | + find: vi.fn(async (_o: string, params?: { $skip?: number; $top?: number }) => { |
| 161 | + const skip = params?.$skip ?? 0; |
| 162 | + const top = params?.$top ?? USERS.length; |
| 163 | + return { data: USERS.slice(skip, skip + top), total: USERS.length }; |
| 164 | + }), |
| 165 | + findOne: vi.fn(async (_o: string, id: string) => USERS.find((r) => r.id === id) ?? null), |
| 166 | + getObjectSchema: vi.fn(async () => ({ name: 'sys_user', fields: FIELDS })), |
| 167 | + } as any; |
| 168 | + render( |
| 169 | + <RecordPickerDialog |
| 170 | + open |
| 171 | + onOpenChange={() => {}} |
| 172 | + dataSource={dataSource} |
| 173 | + objectName="sys_user" |
| 174 | + columns={['name']} |
| 175 | + fieldsMeta={FIELDS} |
| 176 | + cellRenderer={getCellRenderer} |
| 177 | + onSelect={() => {}} |
| 178 | + />, |
| 179 | + ); |
| 180 | + } |
| 181 | + const rows = () => document.querySelectorAll('[data-testid^="record-row-"]'); |
| 182 | + |
| 183 | + for (const [browser, compose] of BROWSERS) { |
| 184 | + it(`${browser}: the candidate-picking Enter stays on page 1 and keeps the text; the next Enter jumps`, async () => { |
| 185 | + mountPicker(); |
| 186 | + await waitFor(() => expect(rows()).toHaveLength(10)); |
| 187 | + const jump = screen.getByTestId('record-picker-page-jump') as HTMLInputElement; |
| 188 | + jump.focus(); |
| 189 | + compose(jump, '2'); |
| 190 | + expect(jump.value).toBe('2'); |
| 191 | + expect(jump).toHaveAttribute('placeholder', '1'); |
| 192 | + |
| 193 | + fireEvent.keyDown(jump, PLAIN_ENTER); |
| 194 | + await waitFor(() => expect(rows()).toHaveLength(2)); |
| 195 | + expect(jump).toHaveAttribute('placeholder', '2'); |
| 196 | + expect(jump.value).toBe(''); |
| 197 | + }); |
| 198 | + } |
| 199 | +}); |
0 commit comments