11import { fireEvent , render } from '@testing-library/react' ;
2+ import userEvent from '@testing-library/user-event' ;
23import type { ChangeEvent , FC } from 'react' ;
34import React , { useState } from 'react' ;
45import BaseInput , { type HolderRef } from '../src/BaseInput' ;
@@ -45,11 +46,13 @@ describe('BaseInput', () => {
4546 expect ( container ) . toMatchSnapshot ( ) ;
4647 } ) ;
4748
48- it ( 'allowClear should work' , ( ) => {
49+ describe ( 'allowClear should work' , ( ) => {
4950 const onChange = jest . fn ( ) ;
5051 const onBlur = jest . fn ( ) ;
5152 const onFocus = jest . fn ( ) ;
5253
54+ const user = userEvent . setup ( ) ;
55+
5356 const Demo : FC = ( ) => {
5457 const [ value , setValue ] = useState < string > ( '' ) ;
5558
@@ -74,23 +77,53 @@ describe('BaseInput', () => {
7477 ) ;
7578 } ;
7679
77- const { container } = render ( < Demo /> ) ;
80+ it ( 'By click' , ( ) => {
81+ const { container } = render ( < Demo /> ) ;
7882
79- const inputEl = container . querySelector ( 'input' ) ;
80- fireEvent . focus ( inputEl ! ) ;
81- expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
83+ const inputEl = container . querySelector ( 'input' ) ;
84+ fireEvent . focus ( inputEl ! ) ;
85+ expect ( onFocus ) . toHaveBeenCalledTimes ( 1 ) ;
8286
83- fireEvent . change ( inputEl ! , { target : { value : 'some text' } } ) ;
84- expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
85- expect ( inputEl ! . value ) . toBe ( 'some text' ) ;
87+ fireEvent . change ( inputEl ! , { target : { value : 'some text' } } ) ;
88+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
89+ expect ( inputEl ! . value ) . toBe ( 'some text' ) ;
8690
87- const clearIcon = container . querySelector ( '.rc-input-clear-icon' ) ;
88- fireEvent . mouseDown ( clearIcon ! ) ;
89- fireEvent . click ( clearIcon ! ) ;
90- fireEvent . mouseUp ( clearIcon ! ) ;
91- expect ( onBlur ) . not . toHaveBeenCalled ( ) ;
92- expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
93- expect ( inputEl ! . value ) . toBe ( '' ) ;
91+ const clearIcon = container . querySelector ( '.rc-input-clear-icon' ) ;
92+ fireEvent . mouseDown ( clearIcon ! ) ;
93+ fireEvent . click ( clearIcon ! ) ;
94+ fireEvent . mouseUp ( clearIcon ! ) ;
95+ expect ( onBlur ) . not . toHaveBeenCalled ( ) ;
96+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
97+ expect ( inputEl ! . value ) . toBe ( '' ) ;
98+ } ) ;
99+
100+ it ( 'By focus and Space' , async ( ) => {
101+ const { container } = render ( < Demo /> ) ;
102+
103+ const inputEl = container . querySelector ( 'input' ) ;
104+ await user . click ( inputEl ! ) ;
105+
106+ await user . type ( inputEl ! , 'some text' ) ;
107+ expect ( inputEl ! . value ) . toBe ( 'some text' ) ;
108+
109+ await user . tab ( ) ;
110+ await user . keyboard ( '[Space]' ) ;
111+ expect ( inputEl ! . value ) . toBe ( '' ) ;
112+ } ) ;
113+
114+ it ( 'By focus and Enter' , async ( ) => {
115+ const { container } = render ( < Demo /> ) ;
116+
117+ const inputEl = container . querySelector ( 'input' ) ;
118+ await user . click ( inputEl ! ) ;
119+
120+ await user . type ( inputEl ! , 'some text' ) ;
121+ expect ( inputEl ! . value ) . toBe ( 'some text' ) ;
122+
123+ await user . tab ( ) ;
124+ await user . keyboard ( '[Enter]' ) ;
125+ expect ( inputEl ! . value ) . toBe ( '' ) ;
126+ } ) ;
94127 } ) ;
95128
96129 it ( 'should display clearIcon correctly' , ( ) => {
0 commit comments