@@ -5,7 +5,7 @@ import { assert } from 'chai';
5
5
import { spy } from 'sinon' ;
6
6
import { createShallow , createMount , getClasses } from '../test-utils' ;
7
7
import Textarea from './Textarea' ;
8
- import Input , { isDirty } from './Input' ;
8
+ import Input , { hasValue , isDirty } from './Input' ;
9
9
10
10
describe ( '<Input />' , ( ) => {
11
11
let shallow ;
@@ -95,39 +95,50 @@ describe('<Input />', () => {
95
95
} ) ;
96
96
97
97
describe ( 'controlled' , ( ) => {
98
- let wrapper ;
99
- let handleDirty ;
100
- let handleClean ;
101
-
102
- before ( ( ) => {
103
- handleClean = spy ( ) ;
104
- handleDirty = spy ( ) ;
105
- wrapper = shallow ( < Input value = "" onDirty = { handleDirty } onClean = { handleClean } /> ) ;
106
- } ) ;
107
-
108
- it ( 'should check that the component is controlled' , ( ) => {
109
- const instance = wrapper . instance ( ) ;
110
- assert . strictEqual ( instance . isControlled ( ) , true , 'isControlled() should return true' ) ;
111
- } ) ;
112
-
113
- it ( 'should have called the handleClean callback' , ( ) => {
114
- assert . strictEqual ( handleClean . callCount , 1 , 'should have called the onClean cb' ) ;
115
- } ) ;
98
+ [ '' , 0 ] . forEach ( value => {
99
+ describe ( `${ typeof value } value` , ( ) => {
100
+ let wrapper ;
101
+ let handleDirty ;
102
+ let handleClean ;
103
+
104
+ before ( ( ) => {
105
+ handleClean = spy ( ) ;
106
+ handleDirty = spy ( ) ;
107
+ wrapper = shallow ( < Input value = { value } onDirty = { handleDirty } onClean = { handleClean } /> ) ;
108
+ } ) ;
116
109
117
- it ( 'should fire the onDirty callback when dirtied' , ( ) => {
118
- assert . strictEqual ( handleDirty . callCount , 0 , 'should not have called the onDirty cb yet' ) ;
119
- wrapper . setProps ( { value : 'hello' } ) ;
120
- assert . strictEqual ( handleDirty . callCount , 1 , 'should have called the onDirty cb' ) ;
121
- } ) ;
110
+ it ( 'should check that the component is controlled' , ( ) => {
111
+ const instance = wrapper . instance ( ) ;
112
+ assert . strictEqual ( instance . isControlled ( ) , true , 'isControlled() should return true' ) ;
113
+ } ) ;
122
114
123
- it ( 'should fire the onClean callback when dirtied' , ( ) => {
124
- assert . strictEqual (
125
- handleClean . callCount ,
126
- 1 ,
127
- 'should have called the onClean cb once already' ,
128
- ) ;
129
- wrapper . setProps ( { value : '' } ) ;
130
- assert . strictEqual ( handleClean . callCount , 2 , 'should have called the onClean cb again' ) ;
115
+ // don't test number because zero is a dirty state, whereas '' is not
116
+ if ( typeof value !== 'number' ) {
117
+ it ( 'should have called the handleClean callback' , ( ) => {
118
+ assert . strictEqual ( handleClean . callCount , 1 , 'should have called the onClean cb' ) ;
119
+ } ) ;
120
+
121
+ it ( 'should fire the onDirty callback when dirtied' , ( ) => {
122
+ assert . strictEqual (
123
+ handleDirty . callCount ,
124
+ 0 ,
125
+ 'should not have called the onDirty cb yet' ,
126
+ ) ;
127
+ wrapper . setProps ( { value : typeof value === 'number' ? 2 : 'hello' } ) ;
128
+ assert . strictEqual ( handleDirty . callCount , 1 , 'should have called the onDirty cb' ) ;
129
+ } ) ;
130
+
131
+ it ( 'should fire the onClean callback when dirtied' , ( ) => {
132
+ assert . strictEqual (
133
+ handleClean . callCount ,
134
+ 1 ,
135
+ 'should have called the onClean cb once already' ,
136
+ ) ;
137
+ wrapper . setProps ( { value } ) ;
138
+ assert . strictEqual ( handleClean . callCount , 2 , 'should have called the onClean cb again' ) ;
139
+ } ) ;
140
+ }
141
+ } ) ;
131
142
} ) ;
132
143
} ) ;
133
144
@@ -345,14 +356,37 @@ describe('<Input />', () => {
345
356
} ) ;
346
357
} ) ;
347
358
359
+ describe ( 'hasValue' , ( ) => {
360
+ [ '' , 0 ] . forEach ( value => {
361
+ it ( `is true for ${ value } ` , ( ) => {
362
+ assert . strictEqual ( hasValue ( value ) , true ) ;
363
+ } ) ;
364
+ } ) ;
365
+
366
+ [ null , undefined ] . forEach ( value => {
367
+ it ( `is false for ${ value } ` , ( ) => {
368
+ assert . strictEqual ( hasValue ( value ) , false ) ;
369
+ } ) ;
370
+ } ) ;
371
+ } ) ;
372
+
348
373
describe ( 'isDirty' , ( ) => {
349
- it ( 'should support integer' , ( ) => {
350
- assert . strictEqual (
351
- isDirty ( {
352
- value : 3 ,
353
- } ) ,
354
- true ,
355
- ) ;
374
+ [ ' ' , 0 ] . forEach ( value => {
375
+ it ( `is true for value ${ value } ` , ( ) => {
376
+ assert . strictEqual ( isDirty ( { value } ) , true ) ;
377
+ } ) ;
378
+
379
+ it ( `is true for SSR defaultValue ${ value } ` , ( ) => {
380
+ assert . strictEqual ( isDirty ( { defaultValue : value } , true ) , true ) ;
381
+ } ) ;
382
+ } ) ;
383
+ [ null , undefined , '' ] . forEach ( value => {
384
+ it ( `is false for value ${ value } ` , ( ) => {
385
+ assert . strictEqual ( isDirty ( { value } ) , false ) ;
386
+ } ) ;
387
+ it ( `is false for SSR defaultValue ${ value } ` , ( ) => {
388
+ assert . strictEqual ( isDirty ( { defaultValue : value } , true ) , false ) ;
389
+ } ) ;
356
390
} ) ;
357
391
} ) ;
358
392
} ) ;
0 commit comments