@@ -120,16 +120,94 @@ describe("Input", () => {
120
120
121
121
const { container} = render ( < Input label = "test input" onFocus = { onFocus } /> ) ;
122
122
123
- container . querySelector ( "input" ) ?. focus ( ) ;
124
- container . querySelector ( "input" ) ?. blur ( ) ;
123
+ act ( ( ) => {
124
+ container . querySelector ( "input" ) ?. focus ( ) ;
125
+ } ) ;
126
+ act ( ( ) => {
127
+ container . querySelector ( "input" ) ?. blur ( ) ;
128
+ } ) ;
125
129
126
130
expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
127
131
} ) ;
128
132
133
+ it ( "should work with keyboard input" , async ( ) => {
134
+ const { getByTestId} = render ( < Input data-testid = "input" /> ) ;
135
+
136
+ const input = getByTestId ( "input" ) as HTMLInputElement ;
137
+
138
+ const user = userEvent . setup ( ) ;
139
+
140
+ act ( ( ) => {
141
+ input . focus ( ) ;
142
+ } ) ;
143
+ expect ( input . value ) . toBe ( "" ) ;
144
+
145
+ await user . keyboard ( "Hello World!" ) ;
146
+ expect ( input . value ) . toBe ( "Hello World!" ) ;
147
+
148
+ await user . keyboard ( "[Backspace][Backspace]" ) ;
149
+ expect ( input . value ) . toBe ( "Hello Worl" ) ;
150
+
151
+ await user . keyboard ( "[ArrowLeft][Delete]" ) ;
152
+ expect ( input . value ) . toBe ( "Hello Wor" ) ;
153
+ } ) ;
154
+
155
+ it ( "should highlight text with user multi-clicks" , async ( ) => {
156
+ const { getByTestId} = render ( < Input data-testid = "input" defaultValue = "Hello World!" /> ) ;
157
+
158
+ const input = getByTestId ( "input" ) as HTMLInputElement ;
159
+
160
+ const user = userEvent . setup ( ) ;
161
+
162
+ expect ( input . value ) . toBe ( "Hello World!" ) ;
163
+
164
+ // in react testing library, input dblClick selects the word/symbol, tripleClick selects the entire text
165
+ await user . tripleClick ( input ) ;
166
+ await user . keyboard ( "Goodbye World!" ) ;
167
+ expect ( input . value ) . toBe ( "Goodbye World!" ) ;
168
+
169
+ await user . tripleClick ( input ) ;
170
+ await user . keyboard ( "[Delete]" ) ;
171
+ expect ( input . value ) . toBe ( "" ) ;
172
+ } ) ;
173
+
174
+ it ( "should focus input on click" , async ( ) => {
175
+ const { getByTestId} = render ( < Input data-testid = "input" /> ) ;
176
+
177
+ const input = getByTestId ( "input" ) as HTMLInputElement ;
178
+ const innerWrapper = document . querySelector ( "[data-slot='inner-wrapper']" ) as HTMLDivElement ;
179
+ const inputWrapper = document . querySelector ( "[data-slot='input-wrapper']" ) as HTMLDivElement ;
180
+
181
+ const user = userEvent . setup ( ) ;
182
+
183
+ expect ( document . activeElement ) . not . toBe ( input ) ;
184
+
185
+ await user . click ( input ) ;
186
+ expect ( document . activeElement ) . toBe ( input ) ;
187
+ act ( ( ) => {
188
+ input . blur ( ) ;
189
+ } ) ;
190
+ expect ( document . activeElement ) . not . toBe ( input ) ;
191
+
192
+ await user . click ( innerWrapper ) ;
193
+ expect ( document . activeElement ) . toBe ( input ) ;
194
+ act ( ( ) => {
195
+ input . blur ( ) ;
196
+ } ) ;
197
+ expect ( document . activeElement ) . not . toBe ( input ) ;
198
+
199
+ await user . click ( inputWrapper ) ;
200
+ expect ( document . activeElement ) . toBe ( input ) ;
201
+ act ( ( ) => {
202
+ input . blur ( ) ;
203
+ } ) ;
204
+ expect ( document . activeElement ) . not . toBe ( input ) ;
205
+ } ) ;
206
+
129
207
it ( "ref should update the value" , ( ) => {
130
208
const ref = React . createRef < HTMLInputElement > ( ) ;
131
209
132
- const { container } = render ( < Input ref = { ref } type = "text" /> ) ;
210
+ render ( < Input ref = { ref } type = "text" /> ) ;
133
211
134
212
if ( ! ref . current ) {
135
213
throw new Error ( "ref is null" ) ;
@@ -138,8 +216,6 @@ describe("Input", () => {
138
216
139
217
ref . current ! . value = value ;
140
218
141
- container . querySelector ( "input" ) ?. focus ( ) ;
142
-
143
219
expect ( ref . current ?. value ) ?. toBe ( value ) ;
144
220
} ) ;
145
221
0 commit comments