|
1 |
| -import { waitFor } from '@testing-library/dom'; |
| 1 | +import { fireEvent, waitFor } from '@testing-library/dom'; |
2 | 2 | import userEvent from '@testing-library/user-event';
|
3 | 3 |
|
4 | 4 | import {
|
@@ -645,6 +645,66 @@ describe('getInputProps', () => {
|
645 | 645 |
|
646 | 646 | expect(environment.clearTimeout).toHaveBeenLastCalledWith(999);
|
647 | 647 | });
|
| 648 | + |
| 649 | + test('stops process if IME composition is in progress', () => { |
| 650 | + const getSources = jest.fn((..._args: any[]) => { |
| 651 | + return [ |
| 652 | + createSource({ |
| 653 | + getItems() { |
| 654 | + return [{ label: '1' }, { label: '2' }]; |
| 655 | + }, |
| 656 | + }), |
| 657 | + ]; |
| 658 | + }); |
| 659 | + const { inputElement } = createPlayground(createAutocomplete, { |
| 660 | + getSources, |
| 661 | + }); |
| 662 | + |
| 663 | + // Typing 木 using the Wubihua input method |
| 664 | + // see: |
| 665 | + // - https://en.wikipedia.org/wiki/Stroke_count_method |
| 666 | + // - https://developer.mozilla.org/en-US/docs/Web/API/Element/compositionend_event |
| 667 | + const character = '木'; |
| 668 | + const strokes = ['一', '丨', '丿', '丶', character]; |
| 669 | + |
| 670 | + strokes.forEach((stroke, index) => { |
| 671 | + const isFirst = index === 0; |
| 672 | + const isLast = index === strokes.length - 1; |
| 673 | + const query = isLast ? stroke : strokes.slice(0, index + 1).join(''); |
| 674 | + |
| 675 | + if (isFirst) { |
| 676 | + fireEvent.compositionStart(inputElement); |
| 677 | + } |
| 678 | + |
| 679 | + fireEvent.compositionUpdate(inputElement, { |
| 680 | + data: query, |
| 681 | + }); |
| 682 | + |
| 683 | + fireEvent.input(inputElement, { |
| 684 | + isComposing: true, |
| 685 | + target: { |
| 686 | + value: query, |
| 687 | + }, |
| 688 | + }); |
| 689 | + |
| 690 | + if (isLast) { |
| 691 | + fireEvent.compositionEnd(inputElement, { |
| 692 | + data: query, |
| 693 | + target: { |
| 694 | + value: query, |
| 695 | + }, |
| 696 | + }); |
| 697 | + } |
| 698 | + }); |
| 699 | + |
| 700 | + expect(inputElement).toHaveValue(character); |
| 701 | + expect(getSources).toHaveBeenCalledTimes(1); |
| 702 | + expect(getSources).toHaveBeenLastCalledWith( |
| 703 | + expect.objectContaining({ |
| 704 | + query: character, |
| 705 | + }) |
| 706 | + ); |
| 707 | + }); |
648 | 708 | });
|
649 | 709 |
|
650 | 710 | describe('onKeyDown', () => {
|
@@ -1913,6 +1973,76 @@ describe('getInputProps', () => {
|
1913 | 1973 | );
|
1914 | 1974 | });
|
1915 | 1975 | });
|
| 1976 | + |
| 1977 | + test('stops process if IME is in progress', () => { |
| 1978 | + const onStateChange = jest.fn(); |
| 1979 | + const { inputElement } = createPlayground(createAutocomplete, { |
| 1980 | + openOnFocus: true, |
| 1981 | + onStateChange, |
| 1982 | + initialState: { |
| 1983 | + collections: [ |
| 1984 | + createCollection({ |
| 1985 | + source: { sourceId: 'testSource' }, |
| 1986 | + items: [ |
| 1987 | + { label: '1' }, |
| 1988 | + { label: '2' }, |
| 1989 | + { label: '3' }, |
| 1990 | + { label: '4' }, |
| 1991 | + ], |
| 1992 | + }), |
| 1993 | + ], |
| 1994 | + }, |
| 1995 | + }); |
| 1996 | + |
| 1997 | + inputElement.focus(); |
| 1998 | + |
| 1999 | + // 1. Pressing Arrow Down to select the first item |
| 2000 | + fireEvent.keyDown(inputElement, { key: 'ArrowDown' }); |
| 2001 | + expect(onStateChange).toHaveBeenLastCalledWith( |
| 2002 | + expect.objectContaining({ |
| 2003 | + state: expect.objectContaining({ |
| 2004 | + activeItemId: 0, |
| 2005 | + }), |
| 2006 | + }) |
| 2007 | + ); |
| 2008 | + |
| 2009 | + // 2. Typing かくてい with a Japanese IME |
| 2010 | + const strokes = ['か', 'く', 'て', 'い']; |
| 2011 | + strokes.forEach((_stroke, index) => { |
| 2012 | + const isFirst = index === 0; |
| 2013 | + const query = strokes.slice(0, index + 1).join(''); |
| 2014 | + |
| 2015 | + if (isFirst) { |
| 2016 | + fireEvent.compositionStart(inputElement); |
| 2017 | + } |
| 2018 | + |
| 2019 | + fireEvent.compositionUpdate(inputElement, { |
| 2020 | + data: query, |
| 2021 | + }); |
| 2022 | + |
| 2023 | + fireEvent.input(inputElement, { |
| 2024 | + isComposing: true, |
| 2025 | + data: query, |
| 2026 | + target: { |
| 2027 | + value: query, |
| 2028 | + }, |
| 2029 | + }); |
| 2030 | + }); |
| 2031 | + |
| 2032 | + // 3. Selecting the 3rd suggestion on the IME window |
| 2033 | + fireEvent.keyDown(inputElement, { key: 'ArrowDown', isComposing: true }); |
| 2034 | + fireEvent.keyDown(inputElement, { key: 'ArrowDown', isComposing: true }); |
| 2035 | + fireEvent.keyDown(inputElement, { key: 'ArrowDown', isComposing: true }); |
| 2036 | + |
| 2037 | + // 4. Checking that activeItemId has not changed |
| 2038 | + expect(onStateChange).toHaveBeenLastCalledWith( |
| 2039 | + expect.objectContaining({ |
| 2040 | + state: expect.objectContaining({ |
| 2041 | + activeItemId: 0, |
| 2042 | + }), |
| 2043 | + }) |
| 2044 | + ); |
| 2045 | + }); |
1916 | 2046 | });
|
1917 | 2047 |
|
1918 | 2048 | describe('onFocus', () => {
|
|
0 commit comments