Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 2d96550

Browse files
committed
Added failing test
1 parent 05bef70 commit 2d96550

File tree

2 files changed

+113
-1
lines changed

2 files changed

+113
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ examples/sandbox
4646

4747
# Other
4848
.vscode/
49+
.idea/

test/control-component-spec.js

Lines changed: 112 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,10 @@ Object.keys(testContexts).forEach((testKey) => {
303303
test: modelReducer('test', initialState),
304304
});
305305

306+
const reactCheckbox = <Control.checkbox model="test.single" />;
306307
const field = TestUtils.renderIntoDocument(
307308
<Provider store={store}>
308-
<Control.checkbox model="test.single" />
309+
{reactCheckbox}
309310
</Provider>
310311
);
311312

@@ -325,12 +326,14 @@ Object.keys(testContexts).forEach((testKey) => {
325326
assert.equal(
326327
get(store.getState().test, 'single'),
327328
false, 'false');
329+
assert.equal(checkbox.checked, false);
328330

329331
TestUtils.Simulate.change(checkbox);
330332

331333
assert.equal(
332334
get(store.getState().test, 'single'),
333335
true, 'true');
336+
assert.equal(checkbox.checked, true);
334337
});
335338

336339
it('should check/uncheck the checkbox when model is externally changed', () => {
@@ -350,6 +353,114 @@ Object.keys(testContexts).forEach((testKey) => {
350353
});
351354
});
352355

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+
353464
describe('with <Control.checkbox /> (multi toggle)', () => {
354465
const initialState = getInitialState({ foo: [1] });
355466
const store = testCreateStore({

0 commit comments

Comments
 (0)