@@ -3,7 +3,7 @@ import { expect } from "chai";
3
3
import { Simulate } from "react-addons-test-utils" ;
4
4
import sinon from "sinon" ;
5
5
6
- import { createFormComponent , createSandbox } from "./test_utils" ;
6
+ import { createFormComponent , createSandbox , setProps } from "./test_utils" ;
7
7
8
8
describe ( "NumberField" , ( ) => {
9
9
let sandbox ;
@@ -17,15 +17,15 @@ describe("NumberField", () => {
17
17
} ) ;
18
18
19
19
describe ( "TextWidget" , ( ) => {
20
- it ( "should render a string field " , ( ) => {
20
+ it ( "should render a number input " , ( ) => {
21
21
const { node } = createFormComponent ( {
22
22
schema : {
23
23
type : "number" ,
24
24
} ,
25
25
} ) ;
26
26
27
27
expect (
28
- node . querySelectorAll ( ".field input[type=text ]" )
28
+ node . querySelectorAll ( ".field input[type=number ]" )
29
29
) . to . have . length . of ( 1 ) ;
30
30
} ) ;
31
31
@@ -129,18 +129,107 @@ describe("NumberField", () => {
129
129
expect ( node . querySelector ( ".field input" ) . value ) . eql ( "2" ) ;
130
130
} ) ;
131
131
132
- it ( "should not cast the input as a number if it ends with a dot ", ( ) => {
132
+ describe ( "when inputting a number that ends with a dot and/or zero it should normalize it, without changing the input value ", ( ) => {
133
133
const { comp, node } = createFormComponent ( {
134
134
schema : {
135
135
type : "number" ,
136
136
} ,
137
137
} ) ;
138
138
139
- Simulate . change ( node . querySelector ( "input" ) , {
140
- target : { value : "2." } ,
139
+ const $input = node . querySelector ( "input" ) ;
140
+
141
+ const tests = [
142
+ {
143
+ input : "2." ,
144
+ output : 2 ,
145
+ } ,
146
+ {
147
+ input : "2.0" ,
148
+ output : 2 ,
149
+ } ,
150
+ {
151
+ input : "2.3" ,
152
+ output : 2.3 ,
153
+ } ,
154
+ {
155
+ input : "2.30" ,
156
+ output : 2.3 ,
157
+ } ,
158
+ {
159
+ input : "2.300" ,
160
+ output : 2.3 ,
161
+ } ,
162
+ {
163
+ input : "2.3001" ,
164
+ output : 2.3001 ,
165
+ } ,
166
+ {
167
+ input : "2.03" ,
168
+ output : 2.03 ,
169
+ } ,
170
+ {
171
+ input : "2.003" ,
172
+ output : 2.003 ,
173
+ } ,
174
+ {
175
+ input : "2.00300" ,
176
+ output : 2.003 ,
177
+ } ,
178
+ {
179
+ input : "200300" ,
180
+ output : 200300 ,
181
+ } ,
182
+ ] ;
183
+
184
+ tests . forEach ( test => {
185
+ it ( `should work with an input value of ${ test . input } ` , ( ) => {
186
+ Simulate . change ( $input , {
187
+ target : { value : test . input } ,
188
+ } ) ;
189
+
190
+ expect ( comp . state . formData ) . eql ( test . output ) ;
191
+ expect ( $input . value ) . eql ( test . input ) ;
192
+ } ) ;
193
+ } ) ;
194
+ } ) ;
195
+
196
+ it ( "should normalize values beginning with a decimal point" , ( ) => {
197
+ const { comp, node } = createFormComponent ( {
198
+ schema : {
199
+ type : "number" ,
200
+ } ,
141
201
} ) ;
142
202
143
- expect ( comp . state . formData ) . eql ( "2." ) ;
203
+ const $input = node . querySelector ( "input" ) ;
204
+
205
+ Simulate . change ( $input , {
206
+ target : { value : ".00" } ,
207
+ } ) ;
208
+
209
+ expect ( comp . state . formData ) . eql ( 0 ) ;
210
+ expect ( $input . value ) . eql ( "0" ) ;
211
+ } ) ;
212
+
213
+ it ( "should update input values correctly when formData prop changes" , ( ) => {
214
+ const schema = {
215
+ type : "number" ,
216
+ } ;
217
+
218
+ const { comp, node } = createFormComponent ( {
219
+ schema,
220
+ formData : 2.03 ,
221
+ } ) ;
222
+
223
+ const $input = node . querySelector ( "input" ) ;
224
+
225
+ expect ( $input . value ) . eql ( "2.03" ) ;
226
+
227
+ setProps ( comp , {
228
+ schema,
229
+ formData : 203 ,
230
+ } ) ;
231
+
232
+ expect ( $input . value ) . eql ( "203" ) ;
144
233
} ) ;
145
234
146
235
it ( "should render the widget with the expected id" , ( ) => {
@@ -150,7 +239,7 @@ describe("NumberField", () => {
150
239
} ,
151
240
} ) ;
152
241
153
- expect ( node . querySelector ( "input[type=text ]" ) . id ) . eql ( "root" ) ;
242
+ expect ( node . querySelector ( "input[type=number ]" ) . id ) . eql ( "root" ) ;
154
243
} ) ;
155
244
156
245
it ( "should render with trailing zeroes" , ( ) => {
@@ -181,6 +270,19 @@ describe("NumberField", () => {
181
270
expect ( node . querySelector ( ".field input" ) . value ) . eql ( "2.000" ) ;
182
271
} ) ;
183
272
273
+ it ( "should allow a zero to be input" , ( ) => {
274
+ const { node } = createFormComponent ( {
275
+ schema : {
276
+ type : "number" ,
277
+ } ,
278
+ } ) ;
279
+
280
+ Simulate . change ( node . querySelector ( "input" ) , {
281
+ target : { value : "0" } ,
282
+ } ) ;
283
+ expect ( node . querySelector ( ".field input" ) . value ) . eql ( "0" ) ;
284
+ } ) ;
285
+
184
286
it ( "should render customized StringField" , ( ) => {
185
287
const CustomStringField = ( ) => < div id = "custom" /> ;
186
288
@@ -195,6 +297,17 @@ describe("NumberField", () => {
195
297
196
298
expect ( node . querySelector ( "#custom" ) ) . to . exist ;
197
299
} ) ;
300
+
301
+ it ( "should use step to represent the multipleOf keyword" , ( ) => {
302
+ const { node } = createFormComponent ( {
303
+ schema : {
304
+ type : "number" ,
305
+ multipleOf : 5 ,
306
+ } ,
307
+ } ) ;
308
+
309
+ expect ( node . querySelector ( "input" ) . step ) . to . eql ( "5" ) ;
310
+ } ) ;
198
311
} ) ;
199
312
200
313
describe ( "SelectWidget" , ( ) => {
0 commit comments