1+ import * as React from 'react' ;
2+ import { render , fireEvent } from '@testing-library/react' ;
3+ import InputNumber from '../src' ;
4+
5+ describe ( 'InputNumber.AllowClear' , ( ) => {
6+ it ( 'should render clear icon when allowClear is true and value is not empty' , ( ) => {
7+ const { container } = render (
8+ < InputNumber allowClear value = { 123 } />
9+ ) ;
10+
11+ const clearIcon = container . querySelector ( '.rc-input-number-clear-icon' ) ;
12+ expect ( clearIcon ) . toBeTruthy ( ) ;
13+ expect ( clearIcon ) . not . toHaveClass ( 'rc-input-number-clear-icon-hidden' ) ;
14+ } ) ;
15+
16+ it ( 'should not render clear icon when value is empty' , ( ) => {
17+ const { container } = render (
18+ < InputNumber allowClear value = { null } />
19+ ) ;
20+
21+ const clearIcon = container . querySelector ( '.rc-input-number-clear-icon' ) ;
22+ expect ( clearIcon ) . toHaveClass ( 'rc-input-number-clear-icon-hidden' ) ;
23+ } ) ;
24+
25+ it ( 'should render clear icon when value is 0' , ( ) => {
26+ const { container } = render (
27+ < InputNumber allowClear value = { 0 } />
28+ ) ;
29+ const clearIcon = container . querySelector ( '.rc-input-number-clear-icon' ) ;
30+ expect ( clearIcon ) . not . toHaveClass ( 'rc-input-number-clear-icon-hidden' ) ;
31+ } ) ;
32+
33+ it ( 'should not render clear icon when disabled' , ( ) => {
34+ const { container } = render (
35+ < InputNumber allowClear value = { 123 } disabled />
36+ ) ;
37+
38+ const clearIcon = container . querySelector ( '.rc-input-number-clear-icon' ) ;
39+ expect ( clearIcon ) . toHaveClass ( 'rc-input-number-clear-icon-hidden' ) ;
40+ } ) ;
41+
42+ it ( 'should not render clear icon when readOnly' , ( ) => {
43+ const { container } = render (
44+ < InputNumber allowClear value = { 123 } readOnly />
45+ ) ;
46+
47+ const clearIcon = container . querySelector ( '.rc-input-number-clear-icon' ) ;
48+ expect ( clearIcon ) . toHaveClass ( 'rc-input-number-clear-icon-hidden' ) ;
49+ } ) ;
50+
51+ it ( 'should clear value to null when allowClear is true (boolean type)' , ( ) => {
52+ const onChange = jest . fn ( ) ;
53+ const { container } = render (
54+ < InputNumber allowClear value = { 123 } onChange = { onChange } />
55+ ) ;
56+
57+ const clearIcon = container . querySelector ( '.rc-input-number-clear-icon' ) ;
58+ fireEvent . click ( clearIcon ) ;
59+
60+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
61+ expect ( onChange ) . toHaveBeenCalledWith ( null ) ;
62+ } ) ;
63+
64+ it ( 'should clear value to custom value when allowClear is object with clearValue' , ( ) => {
65+ const onChange = jest . fn ( ) ;
66+ const { container } = render (
67+ < InputNumber
68+ allowClear = { { clearValue : 0 } }
69+ value = { 123 }
70+ onChange = { onChange }
71+ />
72+ ) ;
73+
74+ const clearIcon = container . querySelector ( '.rc-input-number-clear-icon' ) ;
75+ fireEvent . click ( clearIcon ) ;
76+
77+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
78+ expect ( onChange ) . toHaveBeenCalledWith ( 0 ) ;
79+ } ) ;
80+
81+ it ( 'should handle string clearValue correctly' , ( ) => {
82+ const onChange = jest . fn ( ) ;
83+ const { container } = render (
84+ < InputNumber
85+ allowClear = { { clearValue : 'reset' } }
86+ value = { 123 }
87+ onChange = { onChange }
88+ stringMode
89+ />
90+ ) ;
91+
92+ const clearIcon = container . querySelector ( '.rc-input-number-clear-icon' ) ;
93+ fireEvent . click ( clearIcon ) ;
94+
95+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
96+ expect ( onChange ) . toHaveBeenCalledWith ( null ) ;
97+ } ) ;
98+
99+ it ( 'should support all clearValue types' , ( ) => {
100+ const onChange1 = jest . fn ( ) ;
101+ const onChange2 = jest . fn ( ) ;
102+ const onChange3 = jest . fn ( ) ;
103+
104+ // Test number clearValue
105+ const { container : container1 , unmount : unmount1 } = render (
106+ < InputNumber allowClear = { { clearValue : 42 } } value = { 123 } onChange = { onChange1 } />
107+ ) ;
108+ fireEvent . click ( container1 . querySelector ( '.rc-input-number-clear-icon' ) ) ;
109+ expect ( onChange1 ) . toHaveBeenCalledWith ( 42 ) ;
110+ unmount1 ( ) ;
111+
112+ // Test zero clearValue
113+ const { container : container2 , unmount : unmount2 } = render (
114+ < InputNumber allowClear = { { clearValue : 0 } } value = { 123 } onChange = { onChange2 } />
115+ ) ;
116+ fireEvent . click ( container2 . querySelector ( '.rc-input-number-clear-icon' ) ) ;
117+ expect ( onChange2 ) . toHaveBeenCalledWith ( 0 ) ;
118+ unmount2 ( ) ;
119+
120+ // Test undefined clearValue
121+ const { container : container3 , unmount : unmount3 } = render (
122+ < InputNumber allowClear = { { clearValue : undefined } } value = { 123 } onChange = { onChange3 } />
123+ ) ;
124+ fireEvent . click ( container3 . querySelector ( '.rc-input-number-clear-icon' ) ) ;
125+ expect ( onChange3 ) . toHaveBeenCalledWith ( null ) ;
126+ unmount3 ( ) ;
127+ } ) ;
128+
129+ it ( 'should trigger onClear callback when clear icon is clicked' , ( ) => {
130+ const onClear = jest . fn ( ) ;
131+ const onChange = jest . fn ( ) ;
132+ const { container } = render (
133+ < InputNumber
134+ allowClear
135+ value = { 123 }
136+ onClear = { onClear }
137+ onChange = { onChange }
138+ />
139+ ) ;
140+
141+ const clearIcon = container . querySelector ( '.rc-input-number-clear-icon' ) ;
142+ fireEvent . click ( clearIcon ) ;
143+
144+ expect ( onClear ) . toHaveBeenCalledTimes ( 1 ) ;
145+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
146+ } ) ;
147+
148+ it ( 'should have correct className when suffix is provided' , ( ) => {
149+ const { container } = render (
150+ < InputNumber allowClear value = { 123 } suffix = "$" />
151+ ) ;
152+
153+ const clearIcon = container . querySelector ( '.rc-input-number-clear-icon' ) ;
154+ expect ( clearIcon ) . toHaveClass ( 'rc-input-number-clear-icon-has-suffix' ) ;
155+ } ) ;
156+
157+ it ( 'should work with defaultValue' , ( ) => {
158+ const onChange = jest . fn ( ) ;
159+ const { container } = render (
160+ < InputNumber
161+ allowClear
162+ defaultValue = { 100 }
163+ onChange = { onChange }
164+ />
165+ ) ;
166+
167+ const clearIcon = container . querySelector ( '.rc-input-number-clear-icon' ) ;
168+ expect ( clearIcon ) . not . toHaveClass ( 'rc-input-number-clear-icon-hidden' ) ;
169+ fireEvent . click ( clearIcon ) ;
170+
171+ expect ( onChange ) . toHaveBeenCalledTimes ( 1 ) ;
172+ expect ( onChange ) . toHaveBeenCalledWith ( null ) ;
173+ } ) ;
174+ } ) ;
0 commit comments