|
| 1 | +/* eslint-disable no-console */ |
1 | 2 | import React from 'react';
|
2 |
| -import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 3 | +import { |
| 4 | + render, |
| 5 | + screen, |
| 6 | + fireEvent, |
| 7 | + waitFor, |
| 8 | + within, |
| 9 | +} from '@testing-library/react'; |
3 | 10 | import { Form } from '../Form';
|
4 | 11 |
|
5 | 12 | const setInput = jest.fn();
|
6 | 13 | const input = {};
|
7 | 14 | const handleSubmit = jest.fn();
|
8 | 15 | const onValidate = jest.fn();
|
| 16 | +const onCompositionStart = jest.fn(); |
| 17 | +const onCompositionEnd = jest.fn(); |
| 18 | +const onKeyDown = jest.fn(); |
9 | 19 |
|
10 | 20 | const defaultProps = {
|
11 | 21 | allowAttachments: true,
|
12 | 22 | setInput,
|
13 | 23 | input,
|
14 | 24 | handleSubmit,
|
15 | 25 | onValidate,
|
| 26 | + onCompositionStart, |
| 27 | + onCompositionEnd, |
| 28 | + onKeyDown, |
16 | 29 | };
|
17 | 30 |
|
18 | 31 | describe('Form', () => {
|
@@ -57,4 +70,54 @@ describe('Form', () => {
|
57 | 70 | expect(fileInput.files).not.toBeNull();
|
58 | 71 | expect(fileInput.files![0]).toStrictEqual(testFile);
|
59 | 72 | });
|
| 73 | + |
| 74 | + it('updates IME input with composition completion', async () => { |
| 75 | + const input = { currentTarget: { value: '你' } }; |
| 76 | + const updatedInput = { currentTarget: { value: '你好' } }; |
| 77 | + |
| 78 | + const result = render(<Form {...defaultProps} />); |
| 79 | + expect(result.container).toBeDefined(); |
| 80 | + |
| 81 | + const textFieldContainer = screen.getByTestId('text-input'); |
| 82 | + |
| 83 | + const textInput = |
| 84 | + textFieldContainer.querySelector('textarea') ?? |
| 85 | + within(textFieldContainer).getByRole('textbox'); |
| 86 | + |
| 87 | + await waitFor(() => { |
| 88 | + fireEvent.compositionStart(textInput); |
| 89 | + fireEvent.compositionEnd(textInput, input); |
| 90 | + }); |
| 91 | + |
| 92 | + await waitFor(() => { |
| 93 | + fireEvent.compositionEnd(textInput, updatedInput); |
| 94 | + }); |
| 95 | + |
| 96 | + expect(setInput).toHaveBeenCalledTimes(2); |
| 97 | + }); |
| 98 | + |
| 99 | + it('updates IME input with composition update', async () => { |
| 100 | + const input = { currentTarget: { value: 'しあわせ' } }; |
| 101 | + const updatedInput = { currentTarget: { value: '幸せならおkです' } }; |
| 102 | + |
| 103 | + const result = render(<Form {...defaultProps} />); |
| 104 | + expect(result.container).toBeDefined(); |
| 105 | + |
| 106 | + const textFieldContainer = screen.getByTestId('text-input'); |
| 107 | + |
| 108 | + const textInput = |
| 109 | + textFieldContainer.querySelector('textarea') ?? |
| 110 | + within(textFieldContainer).getByRole('textbox'); |
| 111 | + |
| 112 | + await waitFor(() => { |
| 113 | + fireEvent.compositionStart(textInput); |
| 114 | + fireEvent.compositionUpdate(textInput, input); |
| 115 | + }); |
| 116 | + |
| 117 | + await waitFor(() => { |
| 118 | + fireEvent.compositionUpdate(textInput, updatedInput); |
| 119 | + }); |
| 120 | + |
| 121 | + expect(setInput).toHaveBeenCalledTimes(2); |
| 122 | + }); |
60 | 123 | });
|
0 commit comments