@osimatic/helpers-js 1.5.2 → 1.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/count_down.js +46 -48
- package/date_time.js +0 -1
- package/details_sub_array.js +65 -50
- package/flash_message.js +10 -6
- package/form_date.js +144 -153
- package/form_helper.js +283 -232
- package/google_charts.js +154 -144
- package/google_maps.js +1 -1
- package/import_from_csv.js +198 -160
- package/multi_files_input.js +44 -35
- package/multiple_action_in_table.js +123 -109
- package/package.json +1 -1
- package/paging.js +103 -84
- package/select_all.js +65 -70
- package/sortable_list.js +12 -13
- package/tests/count_down.test.js +131 -352
- package/tests/details_sub_array.test.js +213 -258
- package/tests/flash_message.test.js +21 -153
- package/tests/form_date.test.js +287 -961
- package/tests/form_helper.test.js +553 -673
- package/tests/google_charts.test.js +338 -339
- package/tests/google_maps.test.js +3 -15
- package/tests/import_from_csv.test.js +421 -640
- package/tests/multi_files_input.test.js +305 -737
- package/tests/multiple_action_in_table.test.js +442 -429
- package/tests/open_street_map.test.js +15 -23
- package/tests/paging.test.js +344 -475
- package/tests/select_all.test.js +232 -318
- package/tests/sortable_list.test.js +176 -500
- package/tests/user.test.js +163 -54
- package/user.js +35 -38
package/tests/select_all.test.js
CHANGED
|
@@ -3,358 +3,272 @@
|
|
|
3
3
|
*/
|
|
4
4
|
const { SelectAll } = require('../select_all');
|
|
5
5
|
|
|
6
|
+
// ─── helpers ────────────────────────────────────────────────────────────────
|
|
7
|
+
|
|
8
|
+
function setupFormGroup({ nbCheckboxes = 3, nbChecked = 0 } = {}) {
|
|
9
|
+
const checkboxes = Array.from({ length: nbCheckboxes }, (_, i) => {
|
|
10
|
+
const checked = i < nbChecked ? ' checked' : '';
|
|
11
|
+
return `<input type="checkbox" name="item[]" value="${i + 1}"${checked}>`;
|
|
12
|
+
}).join('');
|
|
13
|
+
|
|
14
|
+
document.body.innerHTML = `
|
|
15
|
+
<div class="form-group">
|
|
16
|
+
<a class="check_all" href="#">Tout sélectionner</a>
|
|
17
|
+
${checkboxes}
|
|
18
|
+
</div>`;
|
|
19
|
+
|
|
20
|
+
return document.querySelector('.form-group');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function setupTable({ nbRows = 2, nbChecked = 0 } = {}) {
|
|
24
|
+
const rows = Array.from({ length: nbRows }, (_, i) => {
|
|
25
|
+
const checked = i < nbChecked ? ' checked' : '';
|
|
26
|
+
return `<tr><td><input type="checkbox" value="${i + 1}"${checked}></td></tr>`;
|
|
27
|
+
}).join('');
|
|
28
|
+
|
|
29
|
+
document.body.innerHTML = `
|
|
30
|
+
<table>
|
|
31
|
+
<thead><tr><th><input type="checkbox" class="check_all"></th></tr></thead>
|
|
32
|
+
<tbody>${rows}</tbody>
|
|
33
|
+
</table>`;
|
|
34
|
+
|
|
35
|
+
return document.querySelector('table');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function setupDivWithCheckAll({ nbItems = 3, nbChecked = 0 } = {}) {
|
|
39
|
+
const items = Array.from({ length: nbItems }, (_, i) => {
|
|
40
|
+
const checked = i < nbChecked ? ' checked' : '';
|
|
41
|
+
return `<div class="form-check"><input type="checkbox" value="${i + 1}"${checked}></div>`;
|
|
42
|
+
}).join('');
|
|
43
|
+
|
|
44
|
+
document.body.innerHTML = `
|
|
45
|
+
<div class="checkbox_with_check_all">
|
|
46
|
+
<input type="checkbox" class="check_all">
|
|
47
|
+
${items}
|
|
48
|
+
</div>`;
|
|
49
|
+
|
|
50
|
+
return document.querySelector('.checkbox_with_check_all');
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ─── tests ───────────────────────────────────────────────────────────────────
|
|
54
|
+
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
document.body.innerHTML = '';
|
|
57
|
+
});
|
|
58
|
+
|
|
6
59
|
describe('SelectAll', () => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
each: jest.fn(),
|
|
26
|
-
get: jest.fn(() => [])
|
|
27
|
-
})),
|
|
28
|
-
closest: jest.fn(() => mockFormGroup || mockDiv),
|
|
29
|
-
off: jest.fn().mockReturnThis(),
|
|
30
|
-
click: jest.fn().mockReturnThis(),
|
|
31
|
-
change: jest.fn().mockReturnThis(),
|
|
32
|
-
on: jest.fn()
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// Handle element wrapping (when passed an element)
|
|
37
|
-
if (selector && typeof selector === 'object') {
|
|
38
|
-
return {
|
|
39
|
-
find: jest.fn(() => ({
|
|
40
|
-
length: 0,
|
|
41
|
-
find: jest.fn(() => ({ length: 0 })),
|
|
42
|
-
text: jest.fn(),
|
|
43
|
-
prop: jest.fn(),
|
|
44
|
-
off: jest.fn().mockReturnThis(),
|
|
45
|
-
click: jest.fn().mockReturnThis(),
|
|
46
|
-
change: jest.fn().mockReturnThis()
|
|
47
|
-
})),
|
|
48
|
-
closest: jest.fn(() => mockFormGroup || mockDiv),
|
|
49
|
-
addClass: jest.fn().mockReturnThis(),
|
|
50
|
-
removeClass: jest.fn().mockReturnThis(),
|
|
51
|
-
off: jest.fn().mockReturnThis(),
|
|
52
|
-
click: jest.fn().mockReturnThis(),
|
|
53
|
-
on: jest.fn()
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return {
|
|
58
|
-
find: jest.fn(() => ({ length: 0 })),
|
|
59
|
-
closest: jest.fn(() => mockFormGroup)
|
|
60
|
-
};
|
|
60
|
+
|
|
61
|
+
describe('updateFormGroup', () => {
|
|
62
|
+
test('should set link text to "Tout désélectionner" when all checkboxes are checked', () => {
|
|
63
|
+
const formGroup = setupFormGroup({ nbCheckboxes: 3, nbChecked: 3 });
|
|
64
|
+
SelectAll.updateFormGroup(formGroup);
|
|
65
|
+
expect(formGroup.querySelector('a.check_all').textContent).toBe('Tout désélectionner');
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test('should set link text to "Tout sélectionner" when partially checked', () => {
|
|
69
|
+
const formGroup = setupFormGroup({ nbCheckboxes: 3, nbChecked: 1 });
|
|
70
|
+
SelectAll.updateFormGroup(formGroup);
|
|
71
|
+
expect(formGroup.querySelector('a.check_all').textContent).toBe('Tout sélectionner');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('should set link text to "Tout sélectionner" when none checked', () => {
|
|
75
|
+
const formGroup = setupFormGroup({ nbCheckboxes: 3, nbChecked: 0 });
|
|
76
|
+
SelectAll.updateFormGroup(formGroup);
|
|
77
|
+
expect(formGroup.querySelector('a.check_all').textContent).toBe('Tout sélectionner');
|
|
61
78
|
});
|
|
62
|
-
});
|
|
63
79
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
80
|
+
test('should do nothing when no a.check_all found', () => {
|
|
81
|
+
document.body.innerHTML = `<div class="form-group"><input type="checkbox"></div>`;
|
|
82
|
+
const formGroup = document.querySelector('.form-group');
|
|
83
|
+
expect(() => SelectAll.updateFormGroup(formGroup)).not.toThrow();
|
|
84
|
+
});
|
|
67
85
|
});
|
|
68
86
|
|
|
69
|
-
describe('
|
|
70
|
-
test('should
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if (selector === 'input[type="checkbox"]:not(.check_all)') {
|
|
75
|
-
return { length: 3 }; // 3 total checkboxes
|
|
76
|
-
}
|
|
77
|
-
if (selector === 'input[type="checkbox"]:not(.check_all):checked') {
|
|
78
|
-
return { length: 3 }; // 3 checked
|
|
79
|
-
}
|
|
80
|
-
if (selector === 'a.check_all') {
|
|
81
|
-
return mockLink;
|
|
82
|
-
}
|
|
83
|
-
return { length: 0 };
|
|
84
|
-
})
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
SelectAll.updateFormGroup(mockFormGroup);
|
|
88
|
-
|
|
89
|
-
expect(mockLink.text).toHaveBeenCalledWith('Tout désélectionner');
|
|
87
|
+
describe('initLinkInFormGroup', () => {
|
|
88
|
+
test('should show "Tout sélectionner" initially when none checked', () => {
|
|
89
|
+
const formGroup = setupFormGroup({ nbCheckboxes: 3, nbChecked: 0 });
|
|
90
|
+
SelectAll.initLinkInFormGroup(formGroup.querySelector('a.check_all'));
|
|
91
|
+
expect(formGroup.querySelector('a.check_all').textContent).toBe('Tout sélectionner');
|
|
90
92
|
});
|
|
91
93
|
|
|
92
|
-
test('should
|
|
93
|
-
const
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (selector === 'input[type="checkbox"]:not(.check_all)') {
|
|
97
|
-
return { length: 3 }; // 3 total checkboxes
|
|
98
|
-
}
|
|
99
|
-
if (selector === 'input[type="checkbox"]:not(.check_all):checked') {
|
|
100
|
-
return { length: 1 }; // Only 1 checked
|
|
101
|
-
}
|
|
102
|
-
if (selector === 'a.check_all') {
|
|
103
|
-
return mockLink;
|
|
104
|
-
}
|
|
105
|
-
return { length: 0 };
|
|
106
|
-
})
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
SelectAll.updateFormGroup(mockFormGroup);
|
|
110
|
-
|
|
111
|
-
expect(mockLink.text).toHaveBeenCalledWith('Tout sélectionner');
|
|
94
|
+
test('should show "Tout désélectionner" initially when all checked', () => {
|
|
95
|
+
const formGroup = setupFormGroup({ nbCheckboxes: 3, nbChecked: 3 });
|
|
96
|
+
SelectAll.initLinkInFormGroup(formGroup.querySelector('a.check_all'));
|
|
97
|
+
expect(formGroup.querySelector('a.check_all').textContent).toBe('Tout désélectionner');
|
|
112
98
|
});
|
|
113
99
|
|
|
114
|
-
test('
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
if (selector === 'input[type="checkbox"]:not(.check_all):checked') {
|
|
122
|
-
return { length: 0 }; // None checked
|
|
123
|
-
}
|
|
124
|
-
if (selector === 'a.check_all') {
|
|
125
|
-
return mockLink;
|
|
126
|
-
}
|
|
127
|
-
return { length: 0 };
|
|
128
|
-
})
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
SelectAll.updateFormGroup(mockFormGroup);
|
|
132
|
-
|
|
133
|
-
expect(mockLink.text).toHaveBeenCalledWith('Tout sélectionner');
|
|
100
|
+
test('clicking link should check all checkboxes when none checked', () => {
|
|
101
|
+
const formGroup = setupFormGroup({ nbCheckboxes: 3, nbChecked: 0 });
|
|
102
|
+
SelectAll.initLinkInFormGroup(formGroup.querySelector('a.check_all'));
|
|
103
|
+
formGroup.querySelector('a.check_all').click();
|
|
104
|
+
const checkboxes = formGroup.querySelectorAll('input[type="checkbox"]:not(.check_all)');
|
|
105
|
+
checkboxes.forEach(cb => expect(cb.checked).toBe(true));
|
|
134
106
|
});
|
|
135
|
-
});
|
|
136
107
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
return { length: 5 }; // 5 total checkboxes
|
|
144
|
-
}
|
|
145
|
-
if (selector === 'tbody input[type="checkbox"]:checked') {
|
|
146
|
-
return { length: 5 }; // 5 checked
|
|
147
|
-
}
|
|
148
|
-
if (selector === 'thead input.check_all') {
|
|
149
|
-
return mockCheckboxSelectAll;
|
|
150
|
-
}
|
|
151
|
-
return { length: 0 };
|
|
152
|
-
})
|
|
153
|
-
};
|
|
154
|
-
|
|
155
|
-
SelectAll.updateTable(mockTable);
|
|
156
|
-
|
|
157
|
-
expect(mockCheckboxSelectAll.prop).toHaveBeenCalledWith('checked', true);
|
|
108
|
+
test('clicking link should uncheck all when all are checked', () => {
|
|
109
|
+
const formGroup = setupFormGroup({ nbCheckboxes: 3, nbChecked: 3 });
|
|
110
|
+
SelectAll.initLinkInFormGroup(formGroup.querySelector('a.check_all'));
|
|
111
|
+
formGroup.querySelector('a.check_all').click();
|
|
112
|
+
const checkboxes = formGroup.querySelectorAll('input[type="checkbox"]:not(.check_all)');
|
|
113
|
+
checkboxes.forEach(cb => expect(cb.checked).toBe(false));
|
|
158
114
|
});
|
|
159
115
|
|
|
160
|
-
test('
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
return { length: 5 }; // 5 total checkboxes
|
|
166
|
-
}
|
|
167
|
-
if (selector === 'tbody input[type="checkbox"]:checked') {
|
|
168
|
-
return { length: 3 }; // Only 3 checked
|
|
169
|
-
}
|
|
170
|
-
if (selector === 'thead input.check_all') {
|
|
171
|
-
return mockCheckboxSelectAll;
|
|
172
|
-
}
|
|
173
|
-
return { length: 0 };
|
|
174
|
-
})
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
SelectAll.updateTable(mockTable);
|
|
178
|
-
|
|
179
|
-
expect(mockCheckboxSelectAll.prop).toHaveBeenCalledWith('checked', false);
|
|
116
|
+
test('clicking link updates the link text', () => {
|
|
117
|
+
const formGroup = setupFormGroup({ nbCheckboxes: 3, nbChecked: 0 });
|
|
118
|
+
SelectAll.initLinkInFormGroup(formGroup.querySelector('a.check_all'));
|
|
119
|
+
formGroup.querySelector('a.check_all').click();
|
|
120
|
+
expect(formGroup.querySelector('a.check_all').textContent).toBe('Tout désélectionner');
|
|
180
121
|
});
|
|
181
122
|
|
|
182
|
-
test('
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
if (selector === 'thead input.check_all') {
|
|
193
|
-
return mockCheckboxSelectAll;
|
|
194
|
-
}
|
|
195
|
-
return { length: 0 };
|
|
196
|
-
})
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
SelectAll.updateTable(mockTable);
|
|
200
|
-
|
|
201
|
-
expect(mockCheckboxSelectAll.prop).toHaveBeenCalledWith('checked', false);
|
|
123
|
+
test('changing a checkbox updates the link text', () => {
|
|
124
|
+
const formGroup = setupFormGroup({ nbCheckboxes: 2, nbChecked: 0 });
|
|
125
|
+
SelectAll.initLinkInFormGroup(formGroup.querySelector('a.check_all'));
|
|
126
|
+
const checkboxes = formGroup.querySelectorAll('input[type="checkbox"]');
|
|
127
|
+
checkboxes.forEach(cb => {
|
|
128
|
+
cb.checked = true;
|
|
129
|
+
cb.dispatchEvent(new Event('change'));
|
|
130
|
+
});
|
|
131
|
+
expect(formGroup.querySelector('a.check_all').textContent).toBe('Tout désélectionner');
|
|
202
132
|
});
|
|
203
133
|
});
|
|
204
134
|
|
|
205
|
-
describe('
|
|
206
|
-
test('should check
|
|
207
|
-
const
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
if (selector === 'input[type="checkbox"]:not(.check_all)') {
|
|
211
|
-
return { length: 4 }; // 4 total checkboxes
|
|
212
|
-
}
|
|
213
|
-
if (selector === 'input[type="checkbox"]:not(.check_all):checked') {
|
|
214
|
-
return { length: 4 }; // 4 checked
|
|
215
|
-
}
|
|
216
|
-
return { length: 0 };
|
|
217
|
-
})
|
|
218
|
-
};
|
|
219
|
-
mockDiv = {
|
|
220
|
-
find: jest.fn((selector) => {
|
|
221
|
-
if (selector === 'div.checkbox, div.form-check') {
|
|
222
|
-
return mockCheckboxContainer;
|
|
223
|
-
}
|
|
224
|
-
if (selector === 'input.check_all') {
|
|
225
|
-
return mockCheckboxSelectAll;
|
|
226
|
-
}
|
|
227
|
-
return { length: 0 };
|
|
228
|
-
})
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
SelectAll.updateDiv(mockDiv);
|
|
232
|
-
|
|
233
|
-
expect(mockCheckboxSelectAll.prop).toHaveBeenCalledWith('checked', true);
|
|
135
|
+
describe('updateTable', () => {
|
|
136
|
+
test('should check select-all when all checkboxes are checked', () => {
|
|
137
|
+
const table = setupTable({ nbRows: 3, nbChecked: 3 });
|
|
138
|
+
SelectAll.updateTable(table);
|
|
139
|
+
expect(table.querySelector('thead input.check_all').checked).toBe(true);
|
|
234
140
|
});
|
|
235
141
|
|
|
236
|
-
test('should uncheck
|
|
237
|
-
const
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
if (selector === 'input[type="checkbox"]:not(.check_all)') {
|
|
241
|
-
return { length: 4 }; // 4 total checkboxes
|
|
242
|
-
}
|
|
243
|
-
if (selector === 'input[type="checkbox"]:not(.check_all):checked') {
|
|
244
|
-
return { length: 2 }; // Only 2 checked
|
|
245
|
-
}
|
|
246
|
-
return { length: 0 };
|
|
247
|
-
})
|
|
248
|
-
};
|
|
249
|
-
mockDiv = {
|
|
250
|
-
find: jest.fn((selector) => {
|
|
251
|
-
if (selector === 'div.checkbox, div.form-check') {
|
|
252
|
-
return mockCheckboxContainer;
|
|
253
|
-
}
|
|
254
|
-
if (selector === 'input.check_all') {
|
|
255
|
-
return mockCheckboxSelectAll;
|
|
256
|
-
}
|
|
257
|
-
return { length: 0 };
|
|
258
|
-
})
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
SelectAll.updateDiv(mockDiv);
|
|
262
|
-
|
|
263
|
-
expect(mockCheckboxSelectAll.prop).toHaveBeenCalledWith('checked', false);
|
|
142
|
+
test('should uncheck select-all when partially checked', () => {
|
|
143
|
+
const table = setupTable({ nbRows: 3, nbChecked: 2 });
|
|
144
|
+
SelectAll.updateTable(table);
|
|
145
|
+
expect(table.querySelector('thead input.check_all').checked).toBe(false);
|
|
264
146
|
});
|
|
265
147
|
|
|
266
|
-
test('should uncheck
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
if (selector === 'input[type="checkbox"]:not(.check_all)') {
|
|
271
|
-
return { length: 4 }; // 4 total checkboxes
|
|
272
|
-
}
|
|
273
|
-
if (selector === 'input[type="checkbox"]:not(.check_all):checked') {
|
|
274
|
-
return { length: 0 }; // None checked
|
|
275
|
-
}
|
|
276
|
-
return { length: 0 };
|
|
277
|
-
})
|
|
278
|
-
};
|
|
279
|
-
mockDiv = {
|
|
280
|
-
find: jest.fn((selector) => {
|
|
281
|
-
if (selector === 'div.checkbox, div.form-check') {
|
|
282
|
-
return mockCheckboxContainer;
|
|
283
|
-
}
|
|
284
|
-
if (selector === 'input.check_all') {
|
|
285
|
-
return mockCheckboxSelectAll;
|
|
286
|
-
}
|
|
287
|
-
return { length: 0 };
|
|
288
|
-
})
|
|
289
|
-
};
|
|
290
|
-
|
|
291
|
-
SelectAll.updateDiv(mockDiv);
|
|
292
|
-
|
|
293
|
-
expect(mockCheckboxSelectAll.prop).toHaveBeenCalledWith('checked', false);
|
|
148
|
+
test('should uncheck select-all when none checked', () => {
|
|
149
|
+
const table = setupTable({ nbRows: 3, nbChecked: 0 });
|
|
150
|
+
SelectAll.updateTable(table);
|
|
151
|
+
expect(table.querySelector('thead input.check_all').checked).toBe(false);
|
|
294
152
|
});
|
|
295
|
-
});
|
|
296
153
|
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
if (selector === 'input[type="checkbox"]:not(.check_all):checked') {
|
|
306
|
-
return { length: 1 };
|
|
307
|
-
}
|
|
308
|
-
if (selector === 'a.check_all') {
|
|
309
|
-
return mockLinkText;
|
|
310
|
-
}
|
|
311
|
-
if (selector === 'input[type="checkbox"]') {
|
|
312
|
-
return {
|
|
313
|
-
change: jest.fn()
|
|
314
|
-
};
|
|
315
|
-
}
|
|
316
|
-
return { length: 0 };
|
|
317
|
-
})
|
|
318
|
-
};
|
|
319
|
-
|
|
320
|
-
const mockLink = {
|
|
321
|
-
off: jest.fn().mockReturnThis(),
|
|
322
|
-
click: jest.fn().mockReturnThis(),
|
|
323
|
-
closest: jest.fn(() => mockFormGroup)
|
|
324
|
-
};
|
|
325
|
-
|
|
326
|
-
expect(() => {
|
|
327
|
-
SelectAll.initLinkInFormGroup(mockLink);
|
|
328
|
-
}).not.toThrow();
|
|
329
|
-
|
|
330
|
-
expect(mockLink.off).toHaveBeenCalledWith('click');
|
|
331
|
-
expect(mockLinkText.text).toHaveBeenCalledWith('Tout sélectionner');
|
|
154
|
+
test('should do nothing when no check_all input in thead', () => {
|
|
155
|
+
document.body.innerHTML = `
|
|
156
|
+
<table>
|
|
157
|
+
<thead><tr><th></th></tr></thead>
|
|
158
|
+
<tbody><tr><td><input type="checkbox"></td></tr></tbody>
|
|
159
|
+
</table>`;
|
|
160
|
+
expect(() => SelectAll.updateTable(document.querySelector('table'))).not.toThrow();
|
|
332
161
|
});
|
|
333
162
|
});
|
|
334
163
|
|
|
335
164
|
describe('initInTable', () => {
|
|
336
165
|
test('should return early when no check_all input found', () => {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
166
|
+
document.body.innerHTML = `
|
|
167
|
+
<table>
|
|
168
|
+
<thead><tr><th></th></tr></thead>
|
|
169
|
+
<tbody><tr><td><input type="checkbox"></td></tr></tbody>
|
|
170
|
+
</table>`;
|
|
171
|
+
expect(() => SelectAll.initInTable(document.querySelector('table'))).not.toThrow();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test('clicking check-all should check all row checkboxes', () => {
|
|
175
|
+
const table = setupTable({ nbRows: 2, nbChecked: 0 });
|
|
176
|
+
SelectAll.initInTable(table);
|
|
177
|
+
table.querySelector('thead input.check_all').click();
|
|
178
|
+
const checkboxes = table.querySelectorAll('tbody input[type="checkbox"]');
|
|
179
|
+
checkboxes.forEach(cb => expect(cb.checked).toBe(true));
|
|
180
|
+
});
|
|
340
181
|
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
182
|
+
test('clicking check-all should uncheck all when all are checked', () => {
|
|
183
|
+
const table = setupTable({ nbRows: 2, nbChecked: 2 });
|
|
184
|
+
SelectAll.initInTable(table);
|
|
185
|
+
table.querySelector('thead input.check_all').click();
|
|
186
|
+
const checkboxes = table.querySelectorAll('tbody input[type="checkbox"]');
|
|
187
|
+
checkboxes.forEach(cb => expect(cb.checked).toBe(false));
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test('changing a row checkbox updates check-all state', () => {
|
|
191
|
+
const table = setupTable({ nbRows: 2, nbChecked: 0 });
|
|
192
|
+
SelectAll.initInTable(table);
|
|
193
|
+
const checkboxes = table.querySelectorAll('tbody input[type="checkbox"]');
|
|
194
|
+
checkboxes.forEach(cb => {
|
|
195
|
+
cb.checked = true;
|
|
196
|
+
cb.dispatchEvent(new Event('change'));
|
|
197
|
+
});
|
|
198
|
+
expect(table.querySelector('thead input.check_all').checked).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
describe('updateDiv', () => {
|
|
203
|
+
test('should check select-all when all checkboxes are checked', () => {
|
|
204
|
+
const div = setupDivWithCheckAll({ nbItems: 3, nbChecked: 3 });
|
|
205
|
+
SelectAll.updateDiv(div);
|
|
206
|
+
expect(div.querySelector('input.check_all').checked).toBe(true);
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('should uncheck select-all when partially checked', () => {
|
|
210
|
+
const div = setupDivWithCheckAll({ nbItems: 3, nbChecked: 2 });
|
|
211
|
+
SelectAll.updateDiv(div);
|
|
212
|
+
expect(div.querySelector('input.check_all').checked).toBe(false);
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
test('should uncheck select-all when none checked', () => {
|
|
216
|
+
const div = setupDivWithCheckAll({ nbItems: 3, nbChecked: 0 });
|
|
217
|
+
SelectAll.updateDiv(div);
|
|
218
|
+
expect(div.querySelector('input.check_all').checked).toBe(false);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
test('should do nothing when no check_all input found', () => {
|
|
222
|
+
document.body.innerHTML = `<div class="checkbox_with_check_all"><div class="form-check"><input type="checkbox"></div></div>`;
|
|
223
|
+
expect(() => SelectAll.updateDiv(document.querySelector('.checkbox_with_check_all'))).not.toThrow();
|
|
344
224
|
});
|
|
345
225
|
});
|
|
346
226
|
|
|
347
227
|
describe('initDiv', () => {
|
|
348
|
-
test('should
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
228
|
+
test('should do nothing when no check_all inputs found', () => {
|
|
229
|
+
document.body.innerHTML = `<div id="content"><div class="form-check"><input type="checkbox"></div></div>`;
|
|
230
|
+
expect(() => SelectAll.initDiv(document.getElementById('content'))).not.toThrow();
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test('clicking check-all should check all items', () => {
|
|
234
|
+
const div = setupDivWithCheckAll({ nbItems: 2, nbChecked: 0 });
|
|
235
|
+
document.body.innerHTML = `<div id="content">${div.outerHTML}</div>`;
|
|
236
|
+
const contentDiv = document.getElementById('content');
|
|
237
|
+
SelectAll.initDiv(contentDiv);
|
|
238
|
+
contentDiv.querySelector('input.check_all').click();
|
|
239
|
+
const checkboxes = contentDiv.querySelectorAll('input[type="checkbox"]:not(.check_all)');
|
|
240
|
+
checkboxes.forEach(cb => expect(cb.checked).toBe(true));
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test('clicking check-all should uncheck all when all are checked', () => {
|
|
244
|
+
const div = setupDivWithCheckAll({ nbItems: 2, nbChecked: 2 });
|
|
245
|
+
document.body.innerHTML = `<div id="content">${div.outerHTML}</div>`;
|
|
246
|
+
const contentDiv = document.getElementById('content');
|
|
247
|
+
SelectAll.initDiv(contentDiv);
|
|
248
|
+
contentDiv.querySelector('input.check_all').click();
|
|
249
|
+
const checkboxes = contentDiv.querySelectorAll('input[type="checkbox"]:not(.check_all)');
|
|
250
|
+
checkboxes.forEach(cb => expect(cb.checked).toBe(false));
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test('changing an item checkbox updates check-all state', () => {
|
|
254
|
+
const div = setupDivWithCheckAll({ nbItems: 2, nbChecked: 0 });
|
|
255
|
+
document.body.innerHTML = `<div id="content">${div.outerHTML}</div>`;
|
|
256
|
+
const contentDiv = document.getElementById('content');
|
|
257
|
+
SelectAll.initDiv(contentDiv);
|
|
258
|
+
const checkboxes = contentDiv.querySelectorAll('input[type="checkbox"]:not(.check_all)');
|
|
259
|
+
checkboxes.forEach(cb => {
|
|
260
|
+
cb.checked = true;
|
|
261
|
+
cb.dispatchEvent(new Event('change'));
|
|
262
|
+
});
|
|
263
|
+
expect(contentDiv.querySelector('input.check_all').checked).toBe(true);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test('should set initial check-all state based on checked items', () => {
|
|
267
|
+
const div = setupDivWithCheckAll({ nbItems: 2, nbChecked: 2 });
|
|
268
|
+
document.body.innerHTML = `<div id="content">${div.outerHTML}</div>`;
|
|
269
|
+
const contentDiv = document.getElementById('content');
|
|
270
|
+
SelectAll.initDiv(contentDiv);
|
|
271
|
+
expect(contentDiv.querySelector('input.check_all').checked).toBe(true);
|
|
358
272
|
});
|
|
359
273
|
});
|
|
360
274
|
});
|