1d4f8be7 by David Han Sze Chuen

- add checkboxes for testing too

- factorise some code into a createInputElement() function
1 parent 829fe3d2
1 describe('Routines', function() { 1 describe('Routines', function() {
2 var el, input, trueRadioInput, falseRadioInput; 2 var el, input, trueRadioInput, falseRadioInput, checkboxInput;
3
4 var createInputElement = function(type, value) {
5 var elem = document.createElement('input');
6 elem.setAttribute('type', type);
7 if (value !== undefined){
8 elem.setAttribute('value', value);
9 }
10 document.body.appendChild(elem);
11 return elem;
12 };
3 13
4 beforeEach(function() { 14 beforeEach(function() {
5 rivets.configure({ 15 rivets.configure({
...@@ -14,21 +24,16 @@ describe('Routines', function() { ...@@ -14,21 +24,16 @@ describe('Routines', function() {
14 el = document.createElement('div'); 24 el = document.createElement('div');
15 document.body.appendChild(el); 25 document.body.appendChild(el);
16 26
17 input = document.createElement('input'); 27 input = createInputElement('text');
18 input.setAttribute('type', 'text');
19 document.body.appendChild(input);
20 28
21 // to test the radio input scenario when its value is "true" 29 // to test the radio input scenario when its value is "true"
22 trueRadioInput = document.createElement('input'); 30 trueRadioInput = createInputElement('radio', 'true');
23 trueRadioInput.setAttribute('type', 'radio');
24 trueRadioInput.value = 'true';
25 document.body.appendChild(trueRadioInput);
26 31
27 // to test the radio input scenario when its value is "false" 32 // to test the radio input scenario when its value is "false"
28 falseRadioInput = document.createElement('input'); 33 falseRadioInput = createInputElement('radio', 'false');
29 falseRadioInput.setAttribute('type', 'radio'); 34
30 falseRadioInput.value = 'false'; 35 // to test the checkbox input scenario
31 document.body.appendChild(falseRadioInput); 36 checkboxInput = createInputElement('checkbox');
32 37
33 }); 38 });
34 39
...@@ -37,6 +42,7 @@ describe('Routines', function() { ...@@ -37,6 +42,7 @@ describe('Routines', function() {
37 input.parentNode.removeChild(input); 42 input.parentNode.removeChild(input);
38 trueRadioInput.parentNode.removeChild(trueRadioInput); 43 trueRadioInput.parentNode.removeChild(trueRadioInput);
39 falseRadioInput.parentNode.removeChild(falseRadioInput); 44 falseRadioInput.parentNode.removeChild(falseRadioInput);
45 checkboxInput.parentNode.removeChild(checkboxInput);
40 }); 46 });
41 47
42 describe('text', function() { 48 describe('text', function() {
...@@ -149,6 +155,22 @@ describe('Routines', function() { ...@@ -149,6 +155,22 @@ describe('Routines', function() {
149 }); 155 });
150 156
151 describe('checked', function() { 157 describe('checked', function() {
158 describe('with a checkbox input', function() {
159 describe('and a truthy value', function() {
160 it('checks the checkbox input', function() {
161 rivets.binders.checked.routine(checkboxInput, true);
162 expect(checkboxInput.checked).toBe(true);
163 });
164 });
165
166 describe('with a falsey value', function() {
167 it('unchecks the checkbox input', function() {
168 rivets.binders.checked.routine(checkboxInput, false);
169 expect(checkboxInput.checked).toBe(false);
170 });
171 });
172 });
173
152 describe('with a radio input with value="true"', function() { 174 describe('with a radio input with value="true"', function() {
153 describe('and a truthy value', function() { 175 describe('and a truthy value', function() {
154 it('checks the radio input', function() { 176 it('checks the radio input', function() {
...@@ -183,6 +205,22 @@ describe('Routines', function() { ...@@ -183,6 +205,22 @@ describe('Routines', function() {
183 }); 205 });
184 206
185 describe('unchecked', function() { 207 describe('unchecked', function() {
208 describe('and a truthy value', function() {
209 describe('and a truthy value', function() {
210 it('checks the checkbox input', function() {
211 rivets.binders.unchecked.routine(checkboxInput, true);
212 expect(checkboxInput.checked).toBe(false);
213 });
214 });
215
216 describe('with a falsey value', function() {
217 it('unchecks the checkbox input', function() {
218 rivets.binders.unchecked.routine(checkboxInput, false);
219 expect(checkboxInput.checked).toBe(true);
220 });
221 });
222 });
223
186 describe('with a radio input with value="true"', function() { 224 describe('with a radio input with value="true"', function() {
187 describe('and a truthy value', function() { 225 describe('and a truthy value', function() {
188 it('checks the radio input', function() { 226 it('checks the radio input', function() {
......