|
1 | 1 | import { fireEvent, render } from '@testing-library/react'; |
2 | 2 | import userEvent from '@testing-library/user-event'; |
3 | 3 | import { spyElementPrototypes } from '@rc-component/util/lib/test/domHook'; |
4 | | -import React from 'react'; |
| 4 | +import React, { ElementType } from 'react'; |
5 | 5 | import Input from '../src'; |
6 | 6 | import type { InputRef } from '../src/interface'; |
7 | 7 | import { resolveOnChange } from '../src/utils/commonUtils'; |
@@ -504,3 +504,40 @@ describe('resolveChange should work', () => { |
504 | 504 | fireEvent.compositionEnd(container.querySelector('textarea')!); |
505 | 505 | expect(onChange).toHaveBeenCalled(); |
506 | 506 | }); |
| 507 | + |
| 508 | +describe('Input IME behavior', () => { |
| 509 | + it('should ignore Enter during composition', () => { |
| 510 | + const onPressEnter = jest.fn(); |
| 511 | + const { container } = render(<Input onPressEnter={onPressEnter} />); |
| 512 | + const input = container.querySelector('input')!; |
| 513 | + |
| 514 | + fireEvent.compositionStart(input); |
| 515 | + |
| 516 | + fireEvent.keyDown(input, { |
| 517 | + key: 'Enter', |
| 518 | + keyCode: 229, |
| 519 | + isComposing: true, |
| 520 | + nativeEvent: { isComposing: true }, |
| 521 | + }); |
| 522 | + |
| 523 | + fireEvent.compositionUpdate(input, { data: '开始' }); |
| 524 | + |
| 525 | + expect(onPressEnter).not.toHaveBeenCalled(); |
| 526 | + |
| 527 | + fireEvent.compositionEnd(input); |
| 528 | + fireEvent.keyDown(input, { |
| 529 | + key: 'Enter', |
| 530 | + nativeEvent: { isComposing: false }, |
| 531 | + }); |
| 532 | + expect(onPressEnter).toHaveBeenCalledTimes(1); |
| 533 | + }); |
| 534 | + |
| 535 | + it('should work with actual IME input', async () => { |
| 536 | + const user = userEvent.setup(); |
| 537 | + const onPressEnter = jest.fn(); |
| 538 | + const { container } = render(<Input onPressEnter={onPressEnter} />); |
| 539 | + |
| 540 | + await user.type(container.querySelector('input')!, 'abc{enter}'); |
| 541 | + expect(onPressEnter).toHaveBeenCalled(); |
| 542 | + }); |
| 543 | +}); |
0 commit comments