3
3
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
4
4
*/
5
5
6
- import { DOWN_ARROW , UP_ARROW } from '@angular/cdk/keycodes' ;
6
+ import { DOWN_ARROW , ENTER , UP_ARROW } from '@angular/cdk/keycodes' ;
7
7
import { Component , ElementRef , viewChild } from '@angular/core' ;
8
8
import { ComponentFixture , TestBed } from '@angular/core/testing' ;
9
9
import { FormsModule } from '@angular/forms' ;
@@ -134,19 +134,93 @@ describe('Input number', () => {
134
134
} ) ;
135
135
} ) ;
136
136
137
- it ( 'should be update value through user typing' , ( ) => {
138
- component . min = 1 ;
139
- component . max = 2 ;
140
- fixture . detectChanges ( ) ;
137
+ describe ( 'should be update value through user typing' , ( ) => {
138
+ it ( 'normal' , ( ) => {
139
+ input ( '123' ) ;
140
+ expect ( component . value ) . toBe ( 123 ) ;
141
+ enter ( ) ;
142
+ expect ( component . value ) . toBe ( 123 ) ;
143
+ blur ( ) ;
144
+ expect ( component . value ) . toBe ( 123 ) ;
145
+
146
+ input ( 'NonNumber' ) ;
147
+ expect ( component . value ) . toBe ( 123 ) ;
148
+ enter ( ) ;
149
+ expect ( component . value ) . toBe ( 123 ) ;
150
+ blur ( ) ;
151
+ expect ( component . value ) . toBe ( 123 ) ;
152
+
153
+ input ( '' ) ;
154
+ expect ( component . value ) . toBe ( null ) ;
155
+ enter ( ) ;
156
+ expect ( component . value ) . toBe ( null ) ;
157
+ blur ( ) ;
158
+ expect ( component . value ) . toBe ( null ) ;
159
+ } ) ;
141
160
142
- userTypingInput ( '3' ) ;
143
- expect ( component . value ) . toBe ( 2 ) ;
144
- userTypingInput ( '0' ) ;
145
- expect ( component . value ) . toBe ( 1 ) ;
146
- userTypingInput ( '1' ) ;
147
- expect ( component . value ) . toBe ( 1 ) ;
148
- userTypingInput ( 'abc' ) ;
149
- expect ( component . value ) . toBe ( null ) ;
161
+ it ( 'with range' , ( ) => {
162
+ component . min = 1 ;
163
+ component . max = 10 ;
164
+ fixture . detectChanges ( ) ;
165
+
166
+ input ( '1' ) ;
167
+ expect ( component . value ) . toBe ( 1 ) ;
168
+
169
+ input ( '99' ) ;
170
+ expect ( component . value ) . toBe ( 1 ) ;
171
+ blur ( ) ;
172
+ expect ( component . value ) . toBe ( 10 ) ;
173
+
174
+ input ( '-99' ) ;
175
+ expect ( component . value ) . toBe ( 10 ) ;
176
+ blur ( ) ;
177
+ expect ( component . value ) . toBe ( 1 ) ;
178
+
179
+ input ( '10' ) ;
180
+ expect ( component . value ) . toBe ( 10 ) ;
181
+ blur ( ) ;
182
+ expect ( component . value ) . toBe ( 10 ) ;
183
+
184
+ input ( '' ) ;
185
+ expect ( component . value ) . toBe ( null ) ;
186
+ blur ( ) ;
187
+ expect ( component . value ) . toBe ( null ) ;
188
+ } ) ;
189
+
190
+ it ( 'with formatter' , ( ) => {
191
+ component . formatter = ( value : number ) : string => `${ value } %` ;
192
+ component . parser = ( value : string ) : number => parseFloat ( value ?. replace ( '%' , '' ) ) ;
193
+ fixture . detectChanges ( ) ;
194
+
195
+ const inputElement = getInputElement ( ) ;
196
+
197
+ input ( '123' ) ;
198
+ fixture . detectChanges ( ) ;
199
+ expect ( component . value ) . toBe ( 123 ) ;
200
+ expect ( inputElement . value ) . toBe ( '123%' ) ;
201
+ blur ( ) ;
202
+ fixture . detectChanges ( ) ;
203
+ expect ( component . value ) . toBe ( 123 ) ;
204
+ expect ( inputElement . value ) . toBe ( '123%' ) ;
205
+
206
+ input ( 'NonNumber' ) ;
207
+ fixture . detectChanges ( ) ;
208
+ expect ( component . value ) . toBe ( 123 ) ;
209
+ expect ( inputElement . value ) . toBe ( 'NonNumber' ) ;
210
+ blur ( ) ;
211
+ fixture . detectChanges ( ) ;
212
+ expect ( component . value ) . toBe ( 123 ) ;
213
+ expect ( inputElement . value ) . toBe ( '123%' ) ;
214
+
215
+ input ( '' ) ;
216
+ fixture . detectChanges ( ) ;
217
+ expect ( component . value ) . toBe ( null ) ;
218
+ expect ( inputElement . value ) . toBe ( '' ) ;
219
+ blur ( ) ;
220
+ fixture . detectChanges ( ) ;
221
+ expect ( component . value ) . toBe ( null ) ;
222
+ expect ( inputElement . value ) . toBe ( '' ) ;
223
+ } ) ;
150
224
} ) ;
151
225
152
226
it ( 'should be apply out-of-range class' , async ( ) => {
@@ -163,56 +237,85 @@ describe('Input number', () => {
163
237
expect ( hostElement . classList ) . toContain ( 'ant-input-number-out-of-range' ) ;
164
238
} ) ;
165
239
166
- it ( 'should be set min and max with precision' , ( ) => {
167
- component . precision = 0 ;
240
+ describe ( 'should be set min and max with precision' , ( ) => {
241
+ beforeEach ( ( ) => {
242
+ component . precision = 0 ;
243
+ component . value = null ;
244
+ } ) ;
168
245
169
- // max > 0
170
- component . min = Number . MIN_SAFE_INTEGER ;
171
- component . max = 1.5 ;
172
- fixture . detectChanges ( ) ;
173
- userTypingInput ( '1.1' ) ;
174
- expect ( component . value ) . toBe ( 1 ) ;
175
- userTypingInput ( '1.5' ) ;
176
- expect ( component . value ) . toBe ( 1 ) ;
246
+ it ( 'max > 0' , ( ) => {
247
+ component . min = Number . MIN_SAFE_INTEGER ;
248
+ component . max = 1.5 ;
249
+ fixture . detectChanges ( ) ;
177
250
178
- // max < 0
179
- component . min = Number . MIN_SAFE_INTEGER ;
180
- component . max = - 1.5 ;
181
- fixture . detectChanges ( ) ;
182
- userTypingInput ( '-1.1' ) ;
183
- expect ( component . value ) . toBe ( - 2 ) ;
184
- userTypingInput ( '-1.5' ) ;
185
- expect ( component . value ) . toBe ( - 2 ) ;
186
-
187
- // min > 0
188
- component . min = 1.5 ;
189
- component . max = Number . MAX_SAFE_INTEGER ;
190
- fixture . detectChanges ( ) ;
191
- userTypingInput ( '1.1' ) ;
192
- expect ( component . value ) . toBe ( 2 ) ;
193
- userTypingInput ( '1.5' ) ;
194
- expect ( component . value ) . toBe ( 2 ) ;
251
+ input ( '1.1' ) ;
252
+ expect ( component . value ) . toBe ( 1.1 ) ;
253
+ blur ( ) ;
254
+ expect ( component . value ) . toBe ( 1 ) ;
255
+ input ( '1.5' ) ;
256
+ expect ( component . value ) . toBe ( 1.5 ) ;
257
+ blur ( ) ;
258
+ expect ( component . value ) . toBe ( 1 ) ;
259
+ } ) ;
195
260
196
- // min < 0
197
- component . min = - 1.5 ;
198
- component . max = Number . MAX_SAFE_INTEGER ;
199
- fixture . detectChanges ( ) ;
200
- userTypingInput ( '-1.1' ) ;
201
- expect ( component . value ) . toBe ( - 1 ) ;
202
- userTypingInput ( '-1.5' ) ;
203
- expect ( component . value ) . toBe ( - 1 ) ;
261
+ it ( 'max < 0' , ( ) => {
262
+ component . min = Number . MIN_SAFE_INTEGER ;
263
+ component . max = - 1.5 ;
264
+ fixture . detectChanges ( ) ;
265
+
266
+ input ( '-1.1' ) ;
267
+ expect ( component . value ) . toBe ( null ) ;
268
+ blur ( ) ;
269
+ expect ( component . value ) . toBe ( - 2 ) ;
270
+ input ( '-1.5' ) ;
271
+ expect ( component . value ) . toBe ( - 1.5 ) ;
272
+ blur ( ) ;
273
+ expect ( component . value ) . toBe ( - 2 ) ;
274
+ } ) ;
275
+
276
+ it ( 'min > 0' , ( ) => {
277
+ component . min = 1.5 ;
278
+ component . max = Number . MAX_SAFE_INTEGER ;
279
+ fixture . detectChanges ( ) ;
280
+
281
+ input ( '1.1' ) ;
282
+ expect ( component . value ) . toBe ( null ) ;
283
+ blur ( ) ;
284
+ expect ( component . value ) . toBe ( 2 ) ;
285
+ input ( '1.5' ) ;
286
+ expect ( component . value ) . toBe ( 1.5 ) ;
287
+ blur ( ) ;
288
+ expect ( component . value ) . toBe ( 2 ) ;
289
+ } ) ;
290
+
291
+ it ( 'min < 0' , ( ) => {
292
+ component . min = - 1.5 ;
293
+ component . max = Number . MAX_SAFE_INTEGER ;
294
+ fixture . detectChanges ( ) ;
295
+
296
+ input ( '-1.1' ) ;
297
+ expect ( component . value ) . toBe ( - 1.1 ) ;
298
+ blur ( ) ;
299
+ expect ( component . value ) . toBe ( - 1 ) ;
300
+ input ( '-1.5' ) ;
301
+ expect ( component . value ) . toBe ( - 1.5 ) ;
302
+ blur ( ) ;
303
+ expect ( component . value ) . toBe ( - 1 ) ;
304
+ } ) ;
204
305
} ) ;
205
306
206
- it ( 'should set precision' , async ( ) => {
307
+ it ( 'should set value with precision' , async ( ) => {
207
308
component . precision = 1 ;
208
- component . value = 1.23 ;
209
309
fixture . detectChanges ( ) ;
210
- await fixture . whenStable ( ) ;
310
+
311
+ input ( '1.23' ) ;
312
+ expect ( component . value ) . toBe ( 1.23 ) ;
313
+ blur ( ) ;
211
314
expect ( component . value ) . toBe ( 1.2 ) ;
212
315
213
- component . value = 1.25 ;
214
- fixture . detectChanges ( ) ;
215
- await fixture . whenStable ( ) ;
316
+ input ( ' 1.25' ) ;
317
+ expect ( component . value ) . toBe ( 1.25 ) ;
318
+ blur ( ) ;
216
319
expect ( component . value ) . toBe ( 1.3 ) ;
217
320
} ) ;
218
321
@@ -284,16 +387,23 @@ describe('Input number', () => {
284
387
function upStepByKeyboard ( ) : void {
285
388
hostElement . dispatchEvent ( new KeyboardEvent ( 'keydown' , { keyCode : UP_ARROW } ) ) ;
286
389
}
287
-
288
390
function downStepByKeyboard ( ) : void {
289
391
hostElement . dispatchEvent ( new KeyboardEvent ( 'keydown' , { keyCode : DOWN_ARROW } ) ) ;
290
392
}
393
+ function enter ( ) : void {
394
+ hostElement . dispatchEvent ( new KeyboardEvent ( 'keydown' , { keyCode : ENTER } ) ) ;
395
+ }
291
396
292
- function userTypingInput ( text : string ) : void {
293
- const input = hostElement . querySelector ( 'input' ) ! ;
294
- input . value = text ;
295
- input . dispatchEvent ( new Event ( 'input' ) ) ;
296
- input . dispatchEvent ( new Event ( 'change' ) ) ;
397
+ function getInputElement ( ) : HTMLInputElement {
398
+ return fixture . nativeElement . querySelector ( 'input' ) ! ;
399
+ }
400
+ function input ( text : string ) : void {
401
+ const element = getInputElement ( ) ;
402
+ element . value = text ;
403
+ element . dispatchEvent ( new Event ( 'input' ) ) ;
404
+ }
405
+ function blur ( ) : void {
406
+ getInputElement ( ) . dispatchEvent ( new Event ( 'blur' ) ) ;
297
407
}
298
408
} ) ;
299
409
@@ -359,6 +469,8 @@ describe('Input number with affixes or addons', () => {
359
469
[nzBordered]="bordered"
360
470
[nzKeyboard]="keyboard"
361
471
[nzControls]="controls"
472
+ [nzParser]="parser"
473
+ [nzFormatter]="formatter"
362
474
[(ngModel)]="value"
363
475
[disabled]="controlDisabled"
364
476
/>
@@ -378,6 +490,8 @@ class InputNumberTestComponent {
378
490
bordered = true ;
379
491
keyboard = true ;
380
492
controls = true ;
493
+ parser : ( ( value : string ) => number ) | undefined = undefined ;
494
+ formatter : ( ( value : number ) => string ) | undefined = undefined ;
381
495
382
496
value : number | null = null ;
383
497
controlDisabled = false ;
0 commit comments