|
1 | 1 | /** |
2 | | - * Copyright IBM Corp. 2016, 2023 |
| 2 | + * Copyright IBM Corp. 2016, 2025 |
3 | 3 | * |
4 | 4 | * This source code is licensed under the Apache-2.0 license found in the |
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | */ |
7 | 7 |
|
8 | | -import { render, screen } from '@testing-library/react'; |
| 8 | +import { fireEvent, render, screen } from '@testing-library/react'; |
9 | 9 | import userEvent from '@testing-library/user-event'; |
10 | 10 | import React, { useState } from 'react'; |
11 | 11 | import { useControllableState } from '../useControllableState'; |
12 | 12 |
|
| 13 | +const TextInput = ({ onChange, value: controlledValue }) => { |
| 14 | + const [value, setValue] = useControllableState({ |
| 15 | + value: controlledValue, |
| 16 | + defaultValue: '', |
| 17 | + onChange, |
| 18 | + }); |
| 19 | + |
| 20 | + return ( |
| 21 | + <input |
| 22 | + data-testid="input" |
| 23 | + type="text" |
| 24 | + onChange={(event) => { |
| 25 | + setValue(event.target.value); |
| 26 | + }} |
| 27 | + value={value} |
| 28 | + /> |
| 29 | + ); |
| 30 | +}; |
| 31 | + |
| 32 | +const ControlledTextInput = () => { |
| 33 | + const [value, setValue] = useState(''); |
| 34 | + |
| 35 | + return <TextInput value={value} onChange={setValue} />; |
| 36 | +}; |
| 37 | + |
| 38 | +const Toggle = ({ defaultControlled }) => { |
| 39 | + const [value, setValue] = useState(''); |
| 40 | + const [controlled, setControlled] = useState(defaultControlled); |
| 41 | + |
| 42 | + return ( |
| 43 | + <> |
| 44 | + <TextInput |
| 45 | + value={controlled ? value : undefined} |
| 46 | + onChange={controlled ? setValue : undefined} |
| 47 | + /> |
| 48 | + <button |
| 49 | + data-testid="toggle" |
| 50 | + type="button" |
| 51 | + onClick={() => { |
| 52 | + setControlled(!controlled); |
| 53 | + }}> |
| 54 | + toggle |
| 55 | + </button> |
| 56 | + </> |
| 57 | + ); |
| 58 | +}; |
| 59 | + |
| 60 | +const FunctionalInput = () => { |
| 61 | + const [value, setValue] = useControllableState({ defaultValue: 0 }); |
| 62 | + |
| 63 | + return ( |
| 64 | + <> |
| 65 | + <div data-testid="display">{value}</div> |
| 66 | + <button |
| 67 | + data-testid="increment" |
| 68 | + onClick={() => { |
| 69 | + setValue((prev) => prev + 1); |
| 70 | + }}> |
| 71 | + Increment |
| 72 | + </button> |
| 73 | + </> |
| 74 | + ); |
| 75 | +}; |
| 76 | + |
| 77 | +const UncontrolledInput = ({ onChange }) => { |
| 78 | + const [value, setValue] = useControllableState({ |
| 79 | + defaultValue: '', |
| 80 | + onChange, |
| 81 | + }); |
| 82 | + |
| 83 | + return ( |
| 84 | + <input |
| 85 | + data-testid="input" |
| 86 | + type="text" |
| 87 | + onChange={(event) => { |
| 88 | + setValue(event.target.value); |
| 89 | + }} |
| 90 | + value={value} |
| 91 | + /> |
| 92 | + ); |
| 93 | +}; |
| 94 | + |
| 95 | +const DefaultValueInput = ({ defaultValue }) => { |
| 96 | + const [value, setValue] = useControllableState({ defaultValue }); |
| 97 | + |
| 98 | + return ( |
| 99 | + <input |
| 100 | + data-testid="input" |
| 101 | + type="text" |
| 102 | + onChange={(event) => { |
| 103 | + setValue(event.target.value); |
| 104 | + }} |
| 105 | + value={value} |
| 106 | + /> |
| 107 | + ); |
| 108 | +}; |
| 109 | + |
| 110 | +const NoOnChangeInput = () => { |
| 111 | + const [value, setValue] = useControllableState({ defaultValue: '' }); |
| 112 | + |
| 113 | + return ( |
| 114 | + <input |
| 115 | + data-testid="input" |
| 116 | + type="text" |
| 117 | + onChange={(event) => { |
| 118 | + setValue(event.target.value); |
| 119 | + }} |
| 120 | + value={value} |
| 121 | + /> |
| 122 | + ); |
| 123 | +}; |
| 124 | + |
13 | 125 | describe('useControllableState', () => { |
14 | 126 | test('uncontrolled', async () => { |
15 | 127 | render(<TextInput />); |
@@ -47,51 +159,54 @@ describe('useControllableState', () => { |
47 | 159 |
|
48 | 160 | warn.mockRestore(); |
49 | 161 | }); |
50 | | -}); |
51 | 162 |
|
52 | | -function TextInput({ onChange, value: controlledValue }) { |
53 | | - const [value, setValue] = useControllableState({ |
54 | | - value: controlledValue, |
55 | | - defaultValue: '', |
56 | | - onChange, |
| 163 | + test('should handle functional updater correctly', async () => { |
| 164 | + render(<FunctionalInput />); |
| 165 | + |
| 166 | + expect(screen.getByTestId('display').textContent).toBe('0'); |
| 167 | + await userEvent.click(screen.getByTestId('increment')); |
| 168 | + expect(screen.getByTestId('display').textContent).toBe('1'); |
| 169 | + await userEvent.click(screen.getByTestId('increment')); |
| 170 | + expect(screen.getByTestId('display').textContent).toBe('2'); |
57 | 171 | }); |
58 | 172 |
|
59 | | - function handleOnChange(event) { |
60 | | - setValue(event.target.value); |
61 | | - } |
| 173 | + test('should call `onChange` with cumulative values for each character typed (uncontrolled)', async () => { |
| 174 | + const onChangeMock = jest.fn(); |
| 175 | + const text = '🟦 ⬜ ⬛ 👁️ 🐝 Ⓜ️ 🟦 ⬜ ⬛'; |
62 | 176 |
|
63 | | - return ( |
64 | | - <input |
65 | | - data-testid="input" |
66 | | - type="text" |
67 | | - onChange={handleOnChange} |
68 | | - value={value} |
69 | | - /> |
70 | | - ); |
71 | | -} |
| 177 | + render(<UncontrolledInput onChange={onChangeMock} />); |
72 | 178 |
|
73 | | -function ControlledTextInput() { |
74 | | - const [value, setValue] = useState(''); |
75 | | - return <TextInput value={value} onChange={setValue} />; |
76 | | -} |
| 179 | + const input = screen.getByTestId('input'); |
77 | 180 |
|
78 | | -function Toggle({ defaultControlled }) { |
79 | | - const [value, setValue] = useState(''); |
80 | | - const [controlled, setControlled] = useState(defaultControlled); |
81 | | - return ( |
82 | | - <> |
83 | | - <TextInput |
84 | | - value={controlled ? value : undefined} |
85 | | - onChange={controlled ? setValue : undefined} |
86 | | - /> |
87 | | - <button |
88 | | - data-testid="toggle" |
89 | | - type="button" |
90 | | - onClick={() => { |
91 | | - setControlled(!controlled); |
92 | | - }}> |
93 | | - toggle |
94 | | - </button> |
95 | | - </> |
96 | | - ); |
97 | | -} |
| 181 | + await userEvent.type(input, text); |
| 182 | + expect(onChangeMock).toHaveBeenCalledTimes(text.length); |
| 183 | + |
| 184 | + [...text].forEach((_, i) => { |
| 185 | + const expected = text.slice(0, i + 1); |
| 186 | + |
| 187 | + expect(onChangeMock.mock.calls[i][0]).toBe(expected); |
| 188 | + }); |
| 189 | + }); |
| 190 | + |
| 191 | + test('should maintain `defaultValue` after re-render (uncontrolled)', () => { |
| 192 | + const { rerender } = render(<DefaultValueInput defaultValue="initial" />); |
| 193 | + const input = screen.getByTestId('input'); |
| 194 | + |
| 195 | + expect(input.value).toBe('initial'); |
| 196 | + |
| 197 | + fireEvent.change(input, { target: { value: 'changed' } }); |
| 198 | + expect(input.value).toBe('changed'); |
| 199 | + |
| 200 | + rerender(<DefaultValueInput defaultValue="initial" />); |
| 201 | + expect(input.value).toBe('changed'); |
| 202 | + }); |
| 203 | + |
| 204 | + test('should work without `onChange` callback', async () => { |
| 205 | + render(<NoOnChangeInput />); |
| 206 | + |
| 207 | + const input = screen.getByTestId('input'); |
| 208 | + |
| 209 | + await userEvent.type(input, 'test'); |
| 210 | + expect(input.value).toBe('test'); |
| 211 | + }); |
| 212 | +}); |
0 commit comments