@@ -303,9 +303,10 @@ Object.keys(testContexts).forEach((testKey) => {
303
303
test : modelReducer ( 'test' , initialState ) ,
304
304
} ) ;
305
305
306
+ const reactCheckbox = < Control . checkbox model = "test.single" /> ;
306
307
const field = TestUtils . renderIntoDocument (
307
308
< Provider store = { store } >
308
- < Control . checkbox model = "test.single" />
309
+ { reactCheckbox }
309
310
</ Provider >
310
311
) ;
311
312
@@ -325,12 +326,14 @@ Object.keys(testContexts).forEach((testKey) => {
325
326
assert . equal (
326
327
get ( store . getState ( ) . test , 'single' ) ,
327
328
false , 'false' ) ;
329
+ assert . equal ( checkbox . checked , false ) ;
328
330
329
331
TestUtils . Simulate . change ( checkbox ) ;
330
332
331
333
assert . equal (
332
334
get ( store . getState ( ) . test , 'single' ) ,
333
335
true , 'true' ) ;
336
+ assert . equal ( checkbox . checked , true ) ;
334
337
} ) ;
335
338
336
339
it ( 'should check/uncheck the checkbox when model is externally changed' , ( ) => {
@@ -350,6 +353,114 @@ Object.keys(testContexts).forEach((testKey) => {
350
353
} ) ;
351
354
} ) ;
352
355
356
+ describe ( 'with <Control.checkbox /> (single toggle, dynamic form data, checked by default)' , ( ) => {
357
+ const initialState = getInitialState ( { single : true } ) ;
358
+ const store = testCreateStore ( {
359
+ testForm : formReducer ( 'test' ) ,
360
+ test : modelReducer ( 'test' , initialState ) ,
361
+ } ) ;
362
+
363
+ const field = TestUtils . renderIntoDocument (
364
+ < Provider store = { store } >
365
+ < Control . checkbox model = "test.other" defaultChecked />
366
+ </ Provider >
367
+ ) ;
368
+
369
+ const checkbox = TestUtils . findRenderedDOMComponentWithTag ( field , 'input' ) ;
370
+
371
+ it ( 'should initially set the checkbox to checked if the model is truthy' , ( ) => {
372
+ assert . equal ( checkbox . checked , true ) ;
373
+ } ) ;
374
+
375
+ it ( 'should give each radio input a name attribute of the model' , ( ) => {
376
+ assert . equal ( checkbox . name , 'test.other' ) ;
377
+ } ) ;
378
+
379
+ it ( 'should dispatch a change event when changed' , ( ) => {
380
+ TestUtils . Simulate . change ( checkbox ) ;
381
+
382
+ assert . equal (
383
+ get ( store . getState ( ) . test , 'other' ) ,
384
+ false , 'false' ) ;
385
+ assert . equal ( checkbox . checked , false ) ;
386
+
387
+ TestUtils . Simulate . change ( checkbox ) ;
388
+
389
+ assert . equal (
390
+ get ( store . getState ( ) . test , 'other' ) ,
391
+ true , 'true' ) ;
392
+ assert . equal ( checkbox . checked , true ) ;
393
+ } ) ;
394
+
395
+ it ( 'should check/uncheck the checkbox when model is externally changed' , ( ) => {
396
+ store . dispatch ( actions . change ( 'test.other' , true ) ) ;
397
+ assert . equal ( checkbox . checked , true ) ;
398
+
399
+ store . dispatch ( actions . change ( 'test.other' , false ) ) ;
400
+ assert . equal ( checkbox . checked , false ) ;
401
+ } ) ;
402
+
403
+ it ( 'should uncheck the checkbox for any falsey value' , ( ) => {
404
+ store . dispatch ( actions . change ( 'test.other' , '' ) ) ;
405
+
406
+ assert . equal ( checkbox . checked , false ) ;
407
+ } ) ;
408
+ } ) ;
409
+
410
+ describe ( 'with <Control.checkbox /> (single toggle, dynamic form data, unchecked by default)' , ( ) => {
411
+ const initialState = getInitialState ( { single : true } ) ;
412
+ const store = testCreateStore ( {
413
+ testForm : formReducer ( 'test' ) ,
414
+ test : modelReducer ( 'test' , initialState ) ,
415
+ } ) ;
416
+
417
+ const field = TestUtils . renderIntoDocument (
418
+ < Provider store = { store } >
419
+ < Control . checkbox model = "test.other" defaultChecked = { false } />
420
+ </ Provider >
421
+ ) ;
422
+
423
+ const checkbox = TestUtils . findRenderedDOMComponentWithTag ( field , 'input' ) ;
424
+
425
+ it ( 'should initially set the checkbox to checked if the model is truthy' , ( ) => {
426
+ assert . equal ( checkbox . checked , false ) ;
427
+ } ) ;
428
+
429
+ it ( 'should give each radio input a name attribute of the model' , ( ) => {
430
+ assert . equal ( checkbox . name , 'test.other' ) ;
431
+ } ) ;
432
+
433
+ it ( 'should dispatch a change event when changed' , ( ) => {
434
+ TestUtils . Simulate . change ( checkbox ) ;
435
+
436
+ assert . equal (
437
+ get ( store . getState ( ) . test , 'other' ) ,
438
+ true , 'true' ) ;
439
+
440
+ TestUtils . Simulate . change ( checkbox ) ;
441
+
442
+ assert . equal (
443
+ get ( store . getState ( ) . test , 'other' ) ,
444
+ false , 'false' ) ;
445
+ } ) ;
446
+
447
+ it ( 'should check/uncheck the checkbox when model is externally changed' , ( ) => {
448
+ store . dispatch ( actions . change ( 'test.other' , false ) ) ;
449
+
450
+ assert . equal ( checkbox . checked , false ) ;
451
+
452
+ store . dispatch ( actions . change ( 'test.other' , true ) ) ;
453
+
454
+ assert . equal ( checkbox . checked , true ) ;
455
+ } ) ;
456
+
457
+ it ( 'should uncheck the checkbox for any falsey value' , ( ) => {
458
+ store . dispatch ( actions . change ( 'test.other' , '' ) ) ;
459
+
460
+ assert . equal ( checkbox . checked , false ) ;
461
+ } ) ;
462
+ } ) ;
463
+
353
464
describe ( 'with <Control.checkbox /> (multi toggle)' , ( ) => {
354
465
const initialState = getInitialState ( { foo : [ 1 ] } ) ;
355
466
const store = testCreateStore ( {
0 commit comments