@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.
@@ -1,3 +1,6 @@
1
+ /**
2
+ * @jest-environment jsdom
3
+ */
1
4
  const { FormDate, InputPeriod } = require('../form_date');
2
5
 
3
6
  describe('FormDate', () => {
@@ -24,7 +27,7 @@ describe('FormDate', () => {
24
27
  expect(periodList).toHaveProperty('1d');
25
28
  expect(periodList['1d']).toHaveProperty('label', 'Un jour');
26
29
  expect(periodList['1d']).toHaveProperty('list');
27
- expect(periodList['1d'].list).toHaveProperty('ajd', "Aujourd’hui");
30
+ expect(periodList['1d'].list).toHaveProperty('ajd', 'Aujourd’hui');
28
31
  expect(periodList['1d'].list).toHaveProperty('hier', 'Hier');
29
32
  expect(periodList['1d'].list).toHaveProperty('jourMoins2', 'Avant-hier');
30
33
  });
@@ -172,13 +175,11 @@ describe('FormDate', () => {
172
175
  });
173
176
  });
174
177
 
175
- describe('fillYearSelect with jQuery mock', () => {
178
+ describe('fillYearSelect', () => {
176
179
  let mockSelect;
177
180
 
178
181
  beforeEach(() => {
179
- mockSelect = {
180
- append: jest.fn()
181
- };
182
+ mockSelect = { insertAdjacentHTML: jest.fn() };
182
183
  });
183
184
 
184
185
  test('should fill select with years from 5 years before to current year', () => {
@@ -187,15 +188,17 @@ describe('FormDate', () => {
187
188
  const currentYear = new Date().getUTCFullYear();
188
189
 
189
190
  // Should be called 6 times (current year + 5 years before)
190
- expect(mockSelect.append).toHaveBeenCalledTimes(6);
191
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledTimes(6);
191
192
 
192
193
  // Check first year
193
- expect(mockSelect.append).toHaveBeenCalledWith(
194
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledWith(
195
+ 'beforeend',
194
196
  `<option value="${currentYear - 5}">${currentYear - 5}</option>`
195
197
  );
196
198
 
197
199
  // Check last year (current year)
198
- expect(mockSelect.append).toHaveBeenCalledWith(
200
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledWith(
201
+ 'beforeend',
199
202
  `<option value="${currentYear}">${currentYear}</option>`
200
203
  );
201
204
  });
@@ -206,15 +209,17 @@ describe('FormDate', () => {
206
209
  const currentYear = new Date().getUTCFullYear();
207
210
 
208
211
  // Should be called 6 times (3 before + current + 2 after)
209
- expect(mockSelect.append).toHaveBeenCalledTimes(6);
212
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledTimes(6);
210
213
 
211
214
  // Check first year
212
- expect(mockSelect.append).toHaveBeenCalledWith(
215
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledWith(
216
+ 'beforeend',
213
217
  `<option value="${currentYear - 3}">${currentYear - 3}</option>`
214
218
  );
215
219
 
216
220
  // Check last year
217
- expect(mockSelect.append).toHaveBeenCalledWith(
221
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledWith(
222
+ 'beforeend',
218
223
  `<option value="${currentYear + 2}">${currentYear + 2}</option>`
219
224
  );
220
225
  });
@@ -222,10 +227,8 @@ describe('FormDate', () => {
222
227
  test('should handle only future years', () => {
223
228
  FormDate.fillYearSelect(mockSelect, 0, 5);
224
229
 
225
- const currentYear = new Date().getUTCFullYear();
226
-
227
230
  // Should be called 6 times (current + 5 after)
228
- expect(mockSelect.append).toHaveBeenCalledTimes(6);
231
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledTimes(6);
229
232
  });
230
233
 
231
234
  test('should handle only one year (current)', () => {
@@ -234,20 +237,19 @@ describe('FormDate', () => {
234
237
  const currentYear = new Date().getUTCFullYear();
235
238
 
236
239
  // Should be called 1 time (only current year)
237
- expect(mockSelect.append).toHaveBeenCalledTimes(1);
238
- expect(mockSelect.append).toHaveBeenCalledWith(
240
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledTimes(1);
241
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledWith(
242
+ 'beforeend',
239
243
  `<option value="${currentYear}">${currentYear}</option>`
240
244
  );
241
245
  });
242
246
  });
243
247
 
244
- describe('fillMonthSelect with jQuery mock', () => {
248
+ describe('fillMonthSelect', () => {
245
249
  let mockSelect;
246
250
 
247
251
  beforeEach(() => {
248
- mockSelect = {
249
- append: jest.fn()
250
- };
252
+ mockSelect = { insertAdjacentHTML: jest.fn() };
251
253
 
252
254
  // Mock String.prototype.capitalize for this test
253
255
  if (!String.prototype.capitalize) {
@@ -258,32 +260,19 @@ describe('FormDate', () => {
258
260
  });
259
261
 
260
262
  test('should fill select with 12 months', () => {
261
- // Mock DateTime.getMonthNameByMonth
262
- global.DateTime = {
263
- getMonthNameByMonth: jest.fn((month, locale) => {
264
- const months = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin',
265
- 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'];
266
- return months[month - 1];
267
- })
268
- };
269
-
270
263
  FormDate.fillMonthSelect(mockSelect, 'fr');
271
264
 
272
- expect(mockSelect.append).toHaveBeenCalledTimes(12);
273
- expect(mockSelect.append).toHaveBeenCalledWith('<option value="1">Janvier</option>');
274
- expect(mockSelect.append).toHaveBeenCalledWith('<option value="12">Décembre</option>');
275
-
276
- delete global.DateTime;
265
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledTimes(12);
266
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledWith('beforeend', '<option value="1">Janvier</option>');
267
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledWith('beforeend', '<option value="12">Décembre</option>');
277
268
  });
278
269
  });
279
270
 
280
- describe('fillDayOfWeekSelect with jQuery mock', () => {
271
+ describe('fillDayOfWeekSelect', () => {
281
272
  let mockSelect;
282
273
 
283
274
  beforeEach(() => {
284
- mockSelect = {
285
- append: jest.fn()
286
- };
275
+ mockSelect = { insertAdjacentHTML: jest.fn() };
287
276
 
288
277
  if (!String.prototype.capitalize) {
289
278
  String.prototype.capitalize = function() {
@@ -293,37 +282,32 @@ describe('FormDate', () => {
293
282
  });
294
283
 
295
284
  test('should fill select with 7 days of week', () => {
296
- // Mock DateTime.getDayNameByDayOfWeek
297
- global.DateTime = {
298
- getDayNameByDayOfWeek: jest.fn((dayOfWeek, locale) => {
299
- const days = ['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche'];
300
- return days[dayOfWeek - 1];
301
- })
302
- };
303
-
304
285
  FormDate.fillDayOfWeekSelect(mockSelect, 'fr');
305
286
 
306
- expect(mockSelect.append).toHaveBeenCalledTimes(7);
307
- expect(mockSelect.append).toHaveBeenCalledWith('<option value="1">Lundi</option>');
308
- expect(mockSelect.append).toHaveBeenCalledWith('<option value="7">Dimanche</option>');
309
-
310
- delete global.DateTime;
287
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledTimes(7);
288
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledWith('beforeend', '<option value="1">Lundi</option>');
289
+ expect(mockSelect.insertAdjacentHTML).toHaveBeenCalledWith('beforeend', '<option value="7">Dimanche</option>');
311
290
  });
312
291
  });
313
292
 
314
- describe('getSelectedDate with jQuery mock', () => {
293
+ function makeDateFormGroupMock(day, month, year) {
294
+ const dayObj = { value: day != null ? String(day) : undefined };
295
+ const monthObj = { value: month != null ? String(month) : undefined };
296
+ const yearObj = { value: year != null ? String(year) : undefined };
297
+ return {
298
+ querySelector: jest.fn((selector) => {
299
+ if (selector === 'select.day') return dayObj;
300
+ if (selector === 'select.month') return monthObj;
301
+ if (selector === 'select.year') return yearObj;
302
+ return null;
303
+ }),
304
+ _day: dayObj, _month: monthObj, _year: yearObj
305
+ };
306
+ }
307
+
308
+ describe('getSelectedDate', () => {
315
309
  test('should return date from select values', () => {
316
- const mockFormGroup = {
317
- find: jest.fn((selector) => {
318
- if (selector === 'select.day') {
319
- return { val: () => '15' };
320
- } else if (selector === 'select.month') {
321
- return { val: () => '6' };
322
- } else if (selector === 'select.year') {
323
- return { val: () => '2023' };
324
- }
325
- })
326
- };
310
+ const mockFormGroup = makeDateFormGroupMock(15, 6, 2023);
327
311
 
328
312
  const result = FormDate.getSelectedDate(mockFormGroup);
329
313
 
@@ -333,14 +317,12 @@ describe('FormDate', () => {
333
317
  expect(result.getFullYear()).toBe(2023);
334
318
  });
335
319
 
336
- test('should return current date when selects return null', () => {
337
- const mockFormGroup = {
338
- find: jest.fn(() => {
339
- return { val: () => null };
340
- })
320
+ test('should return current date when selects return null values', () => {
321
+ const periodFormGroup = {
322
+ querySelector: jest.fn(() => ({ value: null }))
341
323
  };
342
324
 
343
- const result = FormDate.getSelectedDate(mockFormGroup);
325
+ const result = FormDate.getSelectedDate(periodFormGroup);
344
326
  const now = new Date();
345
327
 
346
328
  expect(result).toBeInstanceOf(Date);
@@ -349,594 +331,263 @@ describe('FormDate', () => {
349
331
  });
350
332
  });
351
333
 
352
- describe('setSelectedDate with jQuery mock', () => {
334
+ describe('setSelectedDate', () => {
353
335
  test('should set values on selects', () => {
354
- const mockDaySelect = { val: jest.fn() };
355
- const mockMonthSelect = { val: jest.fn() };
356
- const mockYearSelect = { val: jest.fn() };
357
-
358
- const mockFormGroup = {
359
- find: jest.fn((selector) => {
360
- if (selector === 'select.day') return mockDaySelect;
361
- if (selector === 'select.month') return mockMonthSelect;
362
- if (selector === 'select.year') return mockYearSelect;
363
- })
364
- };
336
+ const mock = makeDateFormGroupMock();
365
337
 
366
- FormDate.setSelectedDate(mockFormGroup, 25, 12, 2024);
338
+ FormDate.setSelectedDate(mock, 25, 12, 2024);
367
339
 
368
- expect(mockDaySelect.val).toHaveBeenCalledWith(25);
369
- expect(mockMonthSelect.val).toHaveBeenCalledWith(12);
370
- expect(mockYearSelect.val).toHaveBeenCalledWith(2024);
340
+ expect(mock._day.value).toBe(25);
341
+ expect(mock._month.value).toBe(12);
342
+ expect(mock._year.value).toBe(2024);
371
343
  });
372
344
 
373
345
  test('should handle -1 values (for not setting day/month)', () => {
374
- const mockDaySelect = { val: jest.fn() };
375
- const mockMonthSelect = { val: jest.fn() };
376
- const mockYearSelect = { val: jest.fn() };
377
-
378
- const mockFormGroup = {
379
- find: jest.fn((selector) => {
380
- if (selector === 'select.day') return mockDaySelect;
381
- if (selector === 'select.month') return mockMonthSelect;
382
- if (selector === 'select.year') return mockYearSelect;
383
- })
384
- };
346
+ const mock = makeDateFormGroupMock();
385
347
 
386
- FormDate.setSelectedDate(mockFormGroup, -1, -1, 2024);
348
+ FormDate.setSelectedDate(mock, -1, -1, 2024);
387
349
 
388
- expect(mockDaySelect.val).toHaveBeenCalledWith(-1);
389
- expect(mockMonthSelect.val).toHaveBeenCalledWith(-1);
390
- expect(mockYearSelect.val).toHaveBeenCalledWith(2024);
350
+ expect(mock._day.value).toBe(-1);
351
+ expect(mock._month.value).toBe(-1);
352
+ expect(mock._year.value).toBe(2024);
391
353
  });
392
354
  });
393
355
 
394
- describe('setTodaySelected with jQuery mock', () => {
356
+ describe('setTodaySelected', () => {
395
357
  test('should set today as selected date', () => {
396
- const mockDaySelect = { val: jest.fn() };
397
- const mockMonthSelect = { val: jest.fn() };
398
- const mockYearSelect = { val: jest.fn() };
399
-
400
- const mockFormGroup = {
401
- find: jest.fn((selector) => {
402
- if (selector === 'select.day') return mockDaySelect;
403
- if (selector === 'select.month') return mockMonthSelect;
404
- if (selector === 'select.year') return mockYearSelect;
405
- })
406
- };
407
-
358
+ const mock = makeDateFormGroupMock();
408
359
  const today = new Date();
409
- FormDate.setTodaySelected(mockFormGroup);
410
360
 
411
- expect(mockDaySelect.val).toHaveBeenCalledWith(today.getDate());
412
- expect(mockMonthSelect.val).toHaveBeenCalledWith(today.getMonth() + 1);
413
- expect(mockYearSelect.val).toHaveBeenCalledWith(today.getFullYear());
361
+ FormDate.setTodaySelected(mock);
362
+
363
+ expect(mock._day.value).toBe(today.getDate());
364
+ expect(mock._month.value).toBe(today.getMonth() + 1);
365
+ expect(mock._year.value).toBe(today.getFullYear());
414
366
  });
415
367
  });
416
368
 
417
- describe('setCurrentMonthSelected with jQuery mock', () => {
369
+ describe('setCurrentMonthSelected', () => {
418
370
  test('should set current month and year', () => {
419
- const mockDaySelect = { val: jest.fn() };
420
- const mockMonthSelect = { val: jest.fn() };
421
- const mockYearSelect = { val: jest.fn() };
422
-
423
- const mockFormGroup = {
424
- find: jest.fn((selector) => {
425
- if (selector === 'select.day') return mockDaySelect;
426
- if (selector === 'select.month') return mockMonthSelect;
427
- if (selector === 'select.year') return mockYearSelect;
428
- })
429
- };
430
-
371
+ const mock = makeDateFormGroupMock();
431
372
  const today = new Date();
432
- FormDate.setCurrentMonthSelected(mockFormGroup);
433
373
 
434
- expect(mockDaySelect.val).toHaveBeenCalledWith(-1);
435
- expect(mockMonthSelect.val).toHaveBeenCalledWith(today.getMonth() + 1);
436
- expect(mockYearSelect.val).toHaveBeenCalledWith(today.getFullYear());
374
+ FormDate.setCurrentMonthSelected(mock);
375
+
376
+ expect(mock._day.value).toBe(-1);
377
+ expect(mock._month.value).toBe(today.getMonth() + 1);
378
+ expect(mock._year.value).toBe(today.getFullYear());
437
379
  });
438
380
  });
439
381
 
440
- describe('setCurrentYearSelected with jQuery mock', () => {
382
+ describe('setCurrentYearSelected', () => {
441
383
  test('should set current year only', () => {
442
- const mockDaySelect = { val: jest.fn() };
443
- const mockMonthSelect = { val: jest.fn() };
444
- const mockYearSelect = { val: jest.fn() };
445
-
446
- const mockFormGroup = {
447
- find: jest.fn((selector) => {
448
- if (selector === 'select.day') return mockDaySelect;
449
- if (selector === 'select.month') return mockMonthSelect;
450
- if (selector === 'select.year') return mockYearSelect;
451
- })
452
- };
453
-
384
+ const mock = makeDateFormGroupMock();
454
385
  const today = new Date();
455
- FormDate.setCurrentYearSelected(mockFormGroup);
456
386
 
457
- expect(mockDaySelect.val).toHaveBeenCalledWith(-1);
458
- expect(mockMonthSelect.val).toHaveBeenCalledWith(-1);
459
- expect(mockYearSelect.val).toHaveBeenCalledWith(today.getFullYear());
387
+ FormDate.setCurrentYearSelected(mock);
388
+
389
+ expect(mock._day.value).toBe(-1);
390
+ expect(mock._month.value).toBe(-1);
391
+ expect(mock._year.value).toBe(today.getFullYear());
460
392
  });
461
393
  });
462
394
 
463
- describe('addNbDaysToToday with jQuery mock', () => {
395
+ describe('addNbDaysToToday', () => {
464
396
  test('should add 5 days to today', () => {
465
- const mockDaySelect = { val: jest.fn() };
466
- const mockMonthSelect = { val: jest.fn() };
467
- const mockYearSelect = { val: jest.fn() };
468
-
469
- const mockFormGroup = {
470
- find: jest.fn((selector) => {
471
- if (selector === 'select.day') return mockDaySelect;
472
- if (selector === 'select.month') return mockMonthSelect;
473
- if (selector === 'select.year') return mockYearSelect;
474
- })
475
- };
397
+ const mock = makeDateFormGroupMock();
476
398
 
477
- FormDate.addNbDaysToToday(mockFormGroup, 5);
399
+ FormDate.addNbDaysToToday(mock, 5);
478
400
 
479
401
  const expectedDate = new Date();
480
402
  expectedDate.setDate(expectedDate.getDate() + 5);
481
403
 
482
- expect(mockDaySelect.val).toHaveBeenCalledWith(expectedDate.getDate());
483
- expect(mockMonthSelect.val).toHaveBeenCalledWith(expectedDate.getMonth() + 1);
484
- expect(mockYearSelect.val).toHaveBeenCalledWith(expectedDate.getFullYear());
404
+ expect(mock._day.value).toBe(expectedDate.getDate());
405
+ expect(mock._month.value).toBe(expectedDate.getMonth() + 1);
406
+ expect(mock._year.value).toBe(expectedDate.getFullYear());
485
407
  });
486
408
 
487
409
  test('should subtract 3 days from today', () => {
488
- const mockDaySelect = { val: jest.fn() };
489
- const mockMonthSelect = { val: jest.fn() };
490
- const mockYearSelect = { val: jest.fn() };
491
-
492
- const mockFormGroup = {
493
- find: jest.fn((selector) => {
494
- if (selector === 'select.day') return mockDaySelect;
495
- if (selector === 'select.month') return mockMonthSelect;
496
- if (selector === 'select.year') return mockYearSelect;
497
- })
498
- };
410
+ const mock = makeDateFormGroupMock();
499
411
 
500
- FormDate.addNbDaysToToday(mockFormGroup, -3);
412
+ FormDate.addNbDaysToToday(mock, -3);
501
413
 
502
414
  const expectedDate = new Date();
503
415
  expectedDate.setDate(expectedDate.getDate() - 3);
504
416
 
505
- expect(mockDaySelect.val).toHaveBeenCalledWith(expectedDate.getDate());
417
+ expect(mock._day.value).toBe(expectedDate.getDate());
506
418
  });
507
419
  });
508
420
 
509
- describe('addNbMonthsToToday with jQuery mock', () => {
421
+ describe('addNbMonthsToToday', () => {
510
422
  test('should add 2 months to today', () => {
511
- const mockDaySelect = { val: jest.fn() };
512
- const mockMonthSelect = { val: jest.fn() };
513
- const mockYearSelect = { val: jest.fn() };
514
-
515
- const mockFormGroup = {
516
- find: jest.fn((selector) => {
517
- if (selector === 'select.day') return mockDaySelect;
518
- if (selector === 'select.month') return mockMonthSelect;
519
- if (selector === 'select.year') return mockYearSelect;
520
- })
521
- };
423
+ const mock = makeDateFormGroupMock();
522
424
 
523
- FormDate.addNbMonthsToToday(mockFormGroup, 2);
425
+ FormDate.addNbMonthsToToday(mock, 2);
524
426
 
525
427
  const expectedDate = new Date();
526
428
  expectedDate.setDate(1);
527
429
  expectedDate.setMonth(expectedDate.getMonth() - 2);
528
430
 
529
- expect(mockDaySelect.val).toHaveBeenCalledWith(-1);
530
- expect(mockMonthSelect.val).toHaveBeenCalledWith(expectedDate.getMonth() + 1);
431
+ expect(mock._day.value).toBe(-1);
432
+ expect(mock._month.value).toBe(expectedDate.getMonth() + 1);
531
433
  });
532
434
  });
533
435
 
534
- describe('addNbYearsToToday with jQuery mock', () => {
436
+ describe('addNbYearsToToday', () => {
535
437
  test('should subtract 1 year from today', () => {
536
- const mockDaySelect = { val: jest.fn() };
537
- const mockMonthSelect = { val: jest.fn() };
538
- const mockYearSelect = { val: jest.fn() };
539
-
540
- const mockFormGroup = {
541
- find: jest.fn((selector) => {
542
- if (selector === 'select.day') return mockDaySelect;
543
- if (selector === 'select.month') return mockMonthSelect;
544
- if (selector === 'select.year') return mockYearSelect;
545
- })
546
- };
438
+ const mock = makeDateFormGroupMock();
547
439
 
548
- FormDate.addNbYearsToToday(mockFormGroup, -1);
440
+ FormDate.addNbYearsToToday(mock, -1);
549
441
 
550
442
  const expectedDate = new Date();
551
443
 
552
- expect(mockDaySelect.val).toHaveBeenCalledWith(-1);
553
- expect(mockMonthSelect.val).toHaveBeenCalledWith(-1);
554
- expect(mockYearSelect.val).toHaveBeenCalledWith(expectedDate.getFullYear() + 1);
444
+ expect(mock._day.value).toBe(-1);
445
+ expect(mock._month.value).toBe(-1);
446
+ expect(mock._year.value).toBe(expectedDate.getFullYear() + 1);
555
447
  });
556
448
  });
557
449
 
558
- describe('addNbDaysToSelectedDate with jQuery mock', () => {
450
+ describe('addNbDaysToSelectedDate', () => {
559
451
  test('should add days to selected date', () => {
560
- const mockDaySelect = { val: jest.fn() };
561
- const mockMonthSelect = { val: jest.fn() };
562
- const mockYearSelect = { val: jest.fn() };
563
-
564
- const mockFormGroup = {
565
- find: jest.fn((selector) => {
566
- if (selector === 'select.day') {
567
- return { val: () => '15' };
568
- } else if (selector === 'select.month') {
569
- return { val: () => '6' };
570
- } else if (selector === 'select.year') {
571
- return { val: () => '2023' };
572
- }
573
-
574
- // For setting values
575
- if (selector === 'select.day') return mockDaySelect;
576
- if (selector === 'select.month') return mockMonthSelect;
577
- if (selector === 'select.year') return mockYearSelect;
578
- })
579
- };
580
-
581
- // Mock for reading
582
- let callCount = 0;
583
- mockFormGroup.find = jest.fn((selector) => {
584
- if (selector === 'select.day') {
585
- callCount++;
586
- return callCount <= 1 ? { val: () => '15' } : mockDaySelect;
587
- } else if (selector === 'select.month') {
588
- callCount++;
589
- return callCount <= 2 ? { val: () => '6' } : mockMonthSelect;
590
- } else if (selector === 'select.year') {
591
- callCount++;
592
- return callCount <= 3 ? { val: () => '2023' } : mockYearSelect;
593
- }
594
- });
452
+ const mock = makeDateFormGroupMock(15, 6, 2023);
595
453
 
596
- FormDate.addNbDaysToSelectedDate(mockFormGroup, 7);
454
+ FormDate.addNbDaysToSelectedDate(mock, 7);
597
455
 
598
456
  const expectedDate = new Date(2023, 5, 15);
599
457
  expectedDate.setDate(expectedDate.getDate() + 7);
600
458
 
601
- expect(mockDaySelect.val).toHaveBeenCalledWith(expectedDate.getDate());
459
+ expect(mock._day.value).toBe(expectedDate.getDate());
602
460
  });
603
461
 
604
462
  test('should add days to today when fromSelectedDate is false', () => {
605
- const mockDaySelect = { val: jest.fn() };
606
- const mockMonthSelect = { val: jest.fn() };
607
- const mockYearSelect = { val: jest.fn() };
608
-
609
- const mockFormGroup = {
610
- find: jest.fn((selector) => {
611
- if (selector === 'select.day') return mockDaySelect;
612
- if (selector === 'select.month') return mockMonthSelect;
613
- if (selector === 'select.year') return mockYearSelect;
614
- })
615
- };
463
+ const mock = makeDateFormGroupMock();
616
464
 
617
- FormDate.addNbDaysToSelectedDate(mockFormGroup, 5, false);
465
+ FormDate.addNbDaysToSelectedDate(mock, 5, false);
618
466
 
619
467
  const expectedDate = new Date();
620
468
  expectedDate.setDate(expectedDate.getDate() + 5);
621
469
 
622
- expect(mockDaySelect.val).toHaveBeenCalledWith(expectedDate.getDate());
470
+ expect(mock._day.value).toBe(expectedDate.getDate());
623
471
  });
624
472
  });
625
473
 
626
- describe('addNbMonthsToSelectedDate with jQuery mock', () => {
474
+ describe('addNbMonthsToSelectedDate', () => {
627
475
  test('should subtract months from selected date', () => {
628
- const mockDaySelect = { val: jest.fn() };
629
- const mockMonthSelect = { val: jest.fn() };
630
- const mockYearSelect = { val: jest.fn() };
631
-
632
- let callCount = 0;
633
- const mockFormGroup = {
634
- find: jest.fn((selector) => {
635
- if (selector === 'select.day') {
636
- callCount++;
637
- return callCount <= 1 ? { val: () => '15' } : mockDaySelect;
638
- } else if (selector === 'select.month') {
639
- callCount++;
640
- return callCount <= 2 ? { val: () => '8' } : mockMonthSelect;
641
- } else if (selector === 'select.year') {
642
- callCount++;
643
- return callCount <= 3 ? { val: () => '2023' } : mockYearSelect;
644
- }
645
- })
646
- };
476
+ const mock = makeDateFormGroupMock(15, 8, 2023);
647
477
 
648
- FormDate.addNbMonthsToSelectedDate(mockFormGroup, -2);
478
+ FormDate.addNbMonthsToSelectedDate(mock, -2);
649
479
 
650
480
  const expectedDate = new Date(2023, 7, 15);
651
481
  expectedDate.setDate(1);
652
482
  expectedDate.setMonth(expectedDate.getMonth() + 2);
653
483
 
654
- expect(mockDaySelect.val).toHaveBeenCalledWith(-1);
655
- expect(mockMonthSelect.val).toHaveBeenCalledWith(expectedDate.getMonth() + 1);
484
+ expect(mock._day.value).toBe(-1);
485
+ expect(mock._month.value).toBe(expectedDate.getMonth() + 1);
656
486
  });
657
487
  });
658
488
 
659
- describe('addNbYearsToSelectedDate with jQuery mock', () => {
489
+ describe('addNbYearsToSelectedDate', () => {
660
490
  test('should subtract years from selected date', () => {
661
- const mockDaySelect = { val: jest.fn() };
662
- const mockMonthSelect = { val: jest.fn() };
663
- const mockYearSelect = { val: jest.fn() };
664
-
665
- let callCount = 0;
666
- const mockFormGroup = {
667
- find: jest.fn((selector) => {
668
- if (selector === 'select.day') {
669
- callCount++;
670
- return callCount <= 1 ? { val: () => '20' } : mockDaySelect;
671
- } else if (selector === 'select.month') {
672
- callCount++;
673
- return callCount <= 2 ? { val: () => '3' } : mockMonthSelect;
674
- } else if (selector === 'select.year') {
675
- callCount++;
676
- return callCount <= 3 ? { val: () => '2023' } : mockYearSelect;
677
- }
678
- })
679
- };
491
+ const mock = makeDateFormGroupMock(20, 3, 2023);
680
492
 
681
- FormDate.addNbYearsToSelectedDate(mockFormGroup, -2);
493
+ FormDate.addNbYearsToSelectedDate(mock, -2);
682
494
 
683
- expect(mockDaySelect.val).toHaveBeenCalledWith(-1);
684
- expect(mockMonthSelect.val).toHaveBeenCalledWith(-1);
685
- expect(mockYearSelect.val).toHaveBeenCalledWith(2025);
495
+ expect(mock._day.value).toBe(-1);
496
+ expect(mock._month.value).toBe(-1);
497
+ expect(mock._year.value).toBe(2025);
686
498
  });
687
499
  });
688
500
 
689
- describe('initForm with jQuery mock', () => {
690
- let mockForm;
691
- let capturedCallbacks;
692
-
501
+ describe('initForm', () => {
693
502
  beforeEach(() => {
694
- capturedCallbacks = {};
695
-
696
- mockForm = {
697
- find: jest.fn((selector) => {
698
- // Return empty for most selectors initially
699
- const result = {
700
- length: 0,
701
- change: jest.fn(function(callback) {
702
- capturedCallbacks[selector + '_change'] = callback;
703
- return this;
704
- }),
705
- click: jest.fn(function(callback) {
706
- capturedCallbacks[selector + '_click'] = callback;
707
- return this;
708
- }),
709
- after: jest.fn(),
710
- append: jest.fn(),
711
- closest: jest.fn(() => mockForm),
712
- val: jest.fn(() => 'perso'),
713
- data: jest.fn(() => null),
714
- attr: jest.fn()
715
- };
716
- return result;
717
- })
718
- };
719
-
720
- // Mock String.prototype.capitalize
721
503
  if (!String.prototype.capitalize) {
722
504
  String.prototype.capitalize = function() {
723
505
  return this.charAt(0).toUpperCase() + this.slice(1);
724
506
  };
725
507
  }
726
-
727
- // Mock DateTime
728
- global.DateTime = {
729
- getMonthNameByMonth: jest.fn((month) => {
730
- const months = ['janvier', 'février', 'mars', 'avril', 'mai', 'juin',
731
- 'juillet', 'août', 'septembre', 'octobre', 'novembre', 'décembre'];
732
- return months[month - 1];
733
- }),
734
- getDayNameByDayOfWeek: jest.fn((day) => {
735
- const days = ['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche'];
736
- return days[day - 1];
737
- })
738
- };
739
- });
740
-
741
- afterEach(() => {
742
- delete global.DateTime;
743
508
  });
744
509
 
745
510
  test('should initialize without errors when no selects present', () => {
511
+ const form = document.createElement('form');
746
512
  expect(() => {
747
- FormDate.initForm(mockForm);
513
+ FormDate.initForm(form);
748
514
  }).not.toThrow();
749
515
  });
750
516
 
751
517
  test('should fill period select when select.periode is present', () => {
752
- const mockPeriodSelect = {
753
- length: 1,
754
- append: jest.fn(),
755
- data: jest.fn(() => null),
756
- val: jest.fn(() => 'ajd'),
757
- change: jest.fn(function(callback) {
758
- capturedCallbacks['periode_change'] = callback;
759
- return this;
760
- }),
761
- closest: jest.fn(() => ({
762
- next: jest.fn(() => ({
763
- addClass: jest.fn(),
764
- removeClass: jest.fn()
765
- }))
766
- }))
767
- };
518
+ const form = document.createElement('form');
519
+ form.innerHTML = `
520
+ <div class="form-group">
521
+ <select class="periode"></select>
522
+ </div>
523
+ <div class="form-group hide"></div>
524
+ `;
525
+ document.body.appendChild(form);
768
526
 
769
- mockForm.find = jest.fn((selector) => {
770
- if (selector === 'select.periode') {
771
- return mockPeriodSelect;
772
- }
773
- if (selector === 'select.periodeCompare') {
774
- return { length: 0 };
775
- }
776
- return { length: 0 };
777
- });
527
+ FormDate.initForm(form);
778
528
 
779
- FormDate.initForm(mockForm);
529
+ const periodeSelect = form.querySelector('select.periode');
530
+ expect(periodeSelect.children.length).toBeGreaterThan(0);
780
531
 
781
- // Should append optgroups for all period categories
782
- expect(mockPeriodSelect.append.mock.calls.length).toBeGreaterThan(0);
783
- expect(mockPeriodSelect.change).toHaveBeenCalled();
532
+ document.body.removeChild(form);
784
533
  });
785
534
 
786
535
  test('should add quick select links for day/month/year selects', () => {
787
- const mockYearSelect = {
788
- length: 1,
789
- after: jest.fn()
790
- };
791
-
792
- mockForm.find = jest.fn((selector) => {
793
- if (selector === 'select.day' || selector === 'select.month') {
794
- return { length: 1 };
795
- }
796
- if (selector === 'select.year') {
797
- return mockYearSelect;
798
- }
799
- return { length: 0 };
800
- });
536
+ const form = document.createElement('form');
537
+ form.innerHTML = `
538
+ <select class="day"></select>
539
+ <select class="month"></select>
540
+ <select class="year"></select>
541
+ `;
801
542
 
802
- FormDate.initForm(mockForm);
543
+ FormDate.initForm(form);
803
544
 
804
- expect(mockYearSelect.after).toHaveBeenCalled();
805
- const addedHtml = mockYearSelect.after.mock.calls[0][0];
806
- expect(addedHtml).toContain('lien_form_today');
807
- expect(addedHtml).toContain('lien_form_yesterday');
808
- expect(addedHtml).toContain('lien_form_current_month');
545
+ expect(form.querySelector('a.lien_form_today')).not.toBeNull();
546
+ expect(form.querySelector('a.lien_form_yesterday')).not.toBeNull();
547
+ expect(form.querySelector('a.lien_form_current_month')).not.toBeNull();
809
548
  });
810
549
 
811
550
  test('should add click handlers for today link', () => {
812
- const mockLink = {
813
- length: 1,
814
- click: jest.fn(function(callback) {
815
- capturedCallbacks['today_click'] = callback;
816
- return this;
817
- })
818
- };
551
+ const form = document.createElement('form');
552
+ form.innerHTML = `
553
+ <select class="day"></select>
554
+ <select class="month"></select>
555
+ <select class="year"></select>
556
+ `;
819
557
 
820
- mockForm.find = jest.fn((selector) => {
821
- if (selector === 'a.lien_form_today') {
822
- return mockLink;
823
- }
824
- return { length: 0 };
825
- });
558
+ FormDate.initForm(form);
826
559
 
827
- FormDate.initForm(mockForm);
828
-
829
- expect(mockLink.click).toHaveBeenCalled();
830
- });
831
-
832
- test('should handle period comparison select', () => {
833
- const mockPeriodSelect = {
834
- length: 1,
835
- append: jest.fn(),
836
- data: jest.fn(() => null),
837
- val: jest.fn(() => 'ajd'),
838
- change: jest.fn(),
839
- find: jest.fn(() => ({
840
- attr: jest.fn(),
841
- parent: jest.fn(() => ({
842
- children: jest.fn(() => ({
843
- attr: jest.fn()
844
- }))
845
- }))
846
- })),
847
- closest: jest.fn(() => ({
848
- next: jest.fn(() => ({
849
- addClass: jest.fn(),
850
- removeClass: jest.fn()
851
- }))
852
- }))
853
- };
560
+ const todayLink = form.querySelector('a.lien_form_today');
561
+ expect(todayLink).not.toBeNull();
562
+ });
854
563
 
855
- const mockCompareSelect = {
856
- length: 1,
857
- append: jest.fn(),
858
- data: jest.fn(() => null),
859
- val: jest.fn(() => 'hier'),
860
- change: jest.fn(),
861
- find: jest.fn(() => ({
862
- attr: jest.fn(),
863
- parent: jest.fn(() => ({
864
- children: jest.fn(() => ({
865
- attr: jest.fn()
866
- }))
867
- }))
868
- })),
869
- closest: jest.fn(() => ({
870
- next: jest.fn(() => ({
871
- addClass: jest.fn(),
872
- removeClass: jest.fn()
873
- }))
874
- }))
875
- };
564
+ test('should fill period comparison select when present', () => {
565
+ const form = document.createElement('form');
566
+ form.innerHTML = `
567
+ <div class="form-group">
568
+ <select class="periode"></select>
569
+ </div>
570
+ <div class="form-group hide"></div>
571
+ <div class="form-group">
572
+ <select class="periodeCompare"></select>
573
+ </div>
574
+ <div class="form-group hide"></div>
575
+ `;
576
+ document.body.appendChild(form);
876
577
 
877
- mockForm.find = jest.fn((selector) => {
878
- if (selector === 'select.periode') {
879
- return mockPeriodSelect;
880
- }
881
- if (selector === 'select.periodeCompare') {
882
- return mockCompareSelect;
883
- }
884
- return { length: 0 };
885
- });
578
+ FormDate.initForm(form);
886
579
 
887
- FormDate.initForm(mockForm);
580
+ expect(form.querySelector('select.periode').children.length).toBeGreaterThan(0);
581
+ expect(form.querySelector('select.periodeCompare').children.length).toBeGreaterThan(0);
888
582
 
889
- expect(mockPeriodSelect.append.mock.calls.length).toBeGreaterThan(0);
890
- expect(mockCompareSelect.append.mock.calls.length).toBeGreaterThan(0);
583
+ document.body.removeChild(form);
891
584
  });
892
585
  });
893
586
  });
894
587
 
895
588
  describe('InputPeriod', () => {
896
- let mockForm;
897
- let capturedCallbacks;
898
-
899
589
  beforeEach(() => {
900
- capturedCallbacks = {};
901
-
902
- // Mock DateTime
903
- global.DateTime = {
904
- getDateForInputDate: jest.fn((date) => {
905
- const year = date.getUTCFullYear();
906
- const month = String(date.getUTCMonth() + 1).padStart(2, '0');
907
- const day = String(date.getUTCDate()).padStart(2, '0');
908
- return `${year}-${month}-${day}`;
909
- }),
910
- getFirstDayOfWeek: jest.fn((date) => {
911
- const d = new Date(date);
912
- const day = d.getUTCDay();
913
- const diff = d.getUTCDate() - day + (day === 0 ? -6 : 1);
914
- return new Date(d.setUTCDate(diff));
915
- }),
916
- getLastDayOfWeek: jest.fn((date) => {
917
- const d = new Date(date);
918
- const day = d.getUTCDay();
919
- const diff = d.getUTCDate() + (day === 0 ? 0 : 7 - day);
920
- return new Date(d.setUTCDate(diff));
921
- }),
922
- getFirstDayOfMonth: jest.fn((date) => {
923
- return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), 1));
924
- }),
925
- getLastDayOfMonth: jest.fn((date) => {
926
- return new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + 1, 0));
927
- }),
928
- getFirstDayOfYear: jest.fn((date) => {
929
- return new Date(Date.UTC(date.getUTCFullYear(), 0, 1));
930
- }),
931
- getLastDayOfYear: jest.fn((date) => {
932
- return new Date(Date.UTC(date.getUTCFullYear(), 11, 31));
933
- })
934
- };
935
- });
936
-
937
- afterEach(() => {
938
- delete global.DateTime;
939
- delete global.$;
590
+ document.body.innerHTML = '';
940
591
  });
941
592
 
942
593
  test('should be a class', () => {
@@ -963,72 +614,45 @@ describe('InputPeriod', () => {
963
614
 
964
615
  describe('addLinks', () => {
965
616
  test('should add period selection links after input-group parent', () => {
966
- const mockParent = {
967
- hasClass: jest.fn(() => true),
968
- parent: jest.fn(function() {
969
- return {
970
- append: jest.fn()
971
- };
972
- })
973
- };
974
-
975
- const mockInput = {
976
- parent: jest.fn(() => mockParent)
977
- };
617
+ document.body.innerHTML = `
618
+ <form>
619
+ <div class="input-group">
620
+ <input type="date" data-add_period_select_links="1" />
621
+ </div>
622
+ </form>
623
+ `;
624
+ const form = document.querySelector('form');
978
625
 
979
- mockForm = {
980
- find: jest.fn((selector) => {
981
- if (selector === 'input[type="date"][data-add_period_select_links]') {
982
- return mockInput;
983
- }
984
- return { length: 0 };
985
- })
986
- };
987
-
988
- // Mock $ for init call
989
- global.$ = jest.fn();
990
-
991
- InputPeriod.addLinks(mockForm);
626
+ InputPeriod.addLinks(form);
992
627
 
993
- expect(mockParent.hasClass).toHaveBeenCalledWith('input-group');
994
- expect(mockParent.parent).toHaveBeenCalled();
628
+ expect(form.querySelector('.select_period_links')).not.toBeNull();
629
+ // Links should be outside the input-group (sibling of input-group)
630
+ const inputGroup = form.querySelector('.input-group');
631
+ expect(inputGroup.nextElementSibling).not.toBeNull();
632
+ expect(inputGroup.nextElementSibling.classList.contains('select_period_links')).toBe(true);
995
633
  });
996
634
 
997
635
  test('should add links directly to parent when not input-group', () => {
998
- const mockParent = {
999
- hasClass: jest.fn(() => false),
1000
- append: jest.fn()
1001
- };
636
+ document.body.innerHTML = `
637
+ <form>
638
+ <div>
639
+ <input type="date" data-add_period_select_links="1" />
640
+ </div>
641
+ </form>
642
+ `;
643
+ const form = document.querySelector('form');
1002
644
 
1003
- const mockInput = {
1004
- parent: jest.fn(() => mockParent)
1005
- };
645
+ InputPeriod.addLinks(form);
1006
646
 
1007
- mockForm = {
1008
- find: jest.fn((selector) => {
1009
- if (selector === 'input[type="date"][data-add_period_select_links]') {
1010
- return mockInput;
1011
- }
1012
- return { length: 0 };
1013
- })
1014
- };
1015
-
1016
- // Mock $ for init call
1017
- global.$ = jest.fn();
1018
-
1019
- InputPeriod.addLinks(mockForm);
1020
-
1021
- expect(mockParent.append).toHaveBeenCalled();
1022
- const addedHtml = mockParent.append.mock.calls[0][0];
1023
- expect(addedHtml).toContain('period_select_yesterday');
1024
- expect(addedHtml).toContain('period_select_current_week');
1025
- expect(addedHtml).toContain('period_select_last_month');
647
+ expect(form.querySelector('.select_period_links')).not.toBeNull();
648
+ expect(form.querySelector('a.period_select_yesterday')).not.toBeNull();
649
+ expect(form.querySelector('a.period_select_current_week')).not.toBeNull();
650
+ expect(form.querySelector('a.period_select_last_month')).not.toBeNull();
1026
651
  });
1027
652
  });
1028
653
 
1029
654
  describe('init', () => {
1030
655
  test('should set up click handlers for all period links', () => {
1031
- const mockLinks = {};
1032
656
  const linkSelectors = [
1033
657
  'a.period_select_today',
1034
658
  'a.period_select_yesterday',
@@ -1041,32 +665,25 @@ describe('InputPeriod', () => {
1041
665
  'a.period_select_last_year'
1042
666
  ];
1043
667
 
668
+ const mockLinks = {};
1044
669
  linkSelectors.forEach(selector => {
1045
- mockLinks[selector] = {
1046
- length: 1,
1047
- click: jest.fn(function(callback) {
1048
- capturedCallbacks[selector] = callback;
1049
- return this;
1050
- })
1051
- };
670
+ mockLinks[selector] = { addEventListener: jest.fn() };
1052
671
  });
1053
672
 
1054
- mockForm = {
1055
- find: jest.fn((selector) => {
1056
- return mockLinks[selector] || { length: 0 };
1057
- })
673
+ const mockForm = {
674
+ querySelector: jest.fn((selector) => mockLinks[selector] || null)
1058
675
  };
1059
676
 
1060
677
  InputPeriod.init(mockForm);
1061
678
 
1062
679
  linkSelectors.forEach(selector => {
1063
- expect(mockLinks[selector].click).toHaveBeenCalled();
680
+ expect(mockLinks[selector].addEventListener).toHaveBeenCalledWith('click', expect.any(Function));
1064
681
  });
1065
682
  });
1066
683
 
1067
684
  test('should handle missing links gracefully', () => {
1068
- mockForm = {
1069
- find: jest.fn(() => ({ length: 0 }))
685
+ const mockForm = {
686
+ querySelector: jest.fn(() => null)
1070
687
  };
1071
688
 
1072
689
  expect(() => {
@@ -1075,410 +692,149 @@ describe('InputPeriod', () => {
1075
692
  });
1076
693
  });
1077
694
 
695
+ function makeContainerWithPeriodInputs() {
696
+ document.body.innerHTML = `
697
+ <div id="outer">
698
+ <div id="inner">
699
+ <a href="#" id="link"></a>
700
+ <input type="date" name="start_date" />
701
+ <input type="date" name="end_date" />
702
+ </div>
703
+ </div>
704
+ `;
705
+ return document.getElementById('link');
706
+ }
707
+
1078
708
  describe('selectToday', () => {
1079
709
  test('should select today date in period inputs', () => {
1080
- const mockStartInput = { val: jest.fn() };
1081
- const mockEndInput = { val: jest.fn() };
1082
-
1083
- const mockLink = {
1084
- parent: jest.fn(() => ({
1085
- parent: jest.fn(() => ({
1086
- find: jest.fn((selector) => {
1087
- if (selector === 'input[type="date"]') {
1088
- return {
1089
- filter: jest.fn((filterSelector) => {
1090
- if (filterSelector.includes('start')) {
1091
- return mockStartInput;
1092
- }
1093
- return mockEndInput;
1094
- }),
1095
- length: 2
1096
- };
1097
- }
1098
- return { length: 0 };
1099
- })
1100
- }))
1101
- }))
1102
- };
1103
-
1104
- // Need to set length on mockStartInput and mockEndInput
1105
- mockStartInput.length = 1;
1106
- mockEndInput.length = 1;
710
+ const link = makeContainerWithPeriodInputs();
1107
711
 
1108
- InputPeriod.selectToday(mockLink);
712
+ InputPeriod.selectToday(link);
1109
713
 
1110
- expect(mockStartInput.val).toHaveBeenCalled();
1111
- expect(mockEndInput.val).toHaveBeenCalled();
1112
- expect(global.DateTime.getDateForInputDate).toHaveBeenCalled();
714
+ const today = new Date();
715
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
716
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1113
717
  });
1114
718
  });
1115
719
 
1116
720
  describe('selectPreviousDay', () => {
1117
721
  test('should select yesterday', () => {
1118
- const mockStartInput = { val: jest.fn(), length: 1 };
1119
- const mockEndInput = { val: jest.fn(), length: 1 };
1120
-
1121
- const mockLink = {
1122
- parent: jest.fn(() => ({
1123
- parent: jest.fn(() => ({
1124
- find: jest.fn((selector) => {
1125
- if (selector === 'input[type="date"]') {
1126
- return {
1127
- filter: jest.fn((filterSelector) => {
1128
- if (filterSelector.includes('start')) {
1129
- return mockStartInput;
1130
- }
1131
- return mockEndInput;
1132
- }),
1133
- length: 2
1134
- };
1135
- }
1136
- return { length: 0 };
1137
- })
1138
- }))
1139
- }))
1140
- };
722
+ const link = makeContainerWithPeriodInputs();
1141
723
 
1142
- InputPeriod.selectPreviousDay(mockLink, 1);
724
+ InputPeriod.selectPreviousDay(link, 1);
1143
725
 
1144
- expect(mockStartInput.val).toHaveBeenCalled();
1145
- expect(mockEndInput.val).toHaveBeenCalled();
726
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
727
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1146
728
  });
1147
729
  });
1148
730
 
1149
731
  describe('selectFollowingDay', () => {
1150
732
  test('should select tomorrow', () => {
1151
- const mockStartInput = { val: jest.fn(), length: 1 };
1152
- const mockEndInput = { val: jest.fn(), length: 1 };
1153
-
1154
- const mockLink = {
1155
- parent: jest.fn(() => ({
1156
- parent: jest.fn(() => ({
1157
- find: jest.fn((selector) => {
1158
- if (selector === 'input[type="date"]') {
1159
- return {
1160
- filter: jest.fn((filterSelector) => {
1161
- if (filterSelector.includes('start')) {
1162
- return mockStartInput;
1163
- }
1164
- return mockEndInput;
1165
- }),
1166
- length: 2
1167
- };
1168
- }
1169
- return { length: 0 };
1170
- })
1171
- }))
1172
- }))
1173
- };
733
+ const link = makeContainerWithPeriodInputs();
1174
734
 
1175
- InputPeriod.selectFollowingDay(mockLink, 1);
735
+ InputPeriod.selectFollowingDay(link, 1);
1176
736
 
1177
- expect(mockStartInput.val).toHaveBeenCalled();
1178
- expect(mockEndInput.val).toHaveBeenCalled();
737
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
738
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1179
739
  });
1180
740
  });
1181
741
 
1182
742
  describe('selectCurrentWeek', () => {
1183
743
  test('should select current week period', () => {
1184
- const mockStartInput = { val: jest.fn(), length: 1 };
1185
- const mockEndInput = { val: jest.fn(), length: 1 };
1186
-
1187
- const mockLink = {
1188
- parent: jest.fn(() => ({
1189
- parent: jest.fn(() => ({
1190
- find: jest.fn((selector) => {
1191
- if (selector === 'input[type="date"]') {
1192
- return {
1193
- filter: jest.fn((filterSelector) => {
1194
- if (filterSelector.includes('start')) {
1195
- return mockStartInput;
1196
- }
1197
- return mockEndInput;
1198
- }),
1199
- length: 2
1200
- };
1201
- }
1202
- return { length: 0 };
1203
- })
1204
- }))
1205
- }))
1206
- };
744
+ const link = makeContainerWithPeriodInputs();
1207
745
 
1208
- InputPeriod.selectCurrentWeek(mockLink);
746
+ InputPeriod.selectCurrentWeek(link);
1209
747
 
1210
- expect(global.DateTime.getFirstDayOfWeek).toHaveBeenCalled();
1211
- expect(global.DateTime.getLastDayOfWeek).toHaveBeenCalled();
1212
- expect(mockStartInput.val).toHaveBeenCalled();
1213
- expect(mockEndInput.val).toHaveBeenCalled();
748
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
749
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1214
750
  });
1215
751
  });
1216
752
 
1217
753
  describe('selectPreviousWeek', () => {
1218
754
  test('should select previous week', () => {
1219
- const mockStartInput = { val: jest.fn(), length: 1 };
1220
- const mockEndInput = { val: jest.fn(), length: 1 };
1221
-
1222
- const mockLink = {
1223
- parent: jest.fn(() => ({
1224
- parent: jest.fn(() => ({
1225
- find: jest.fn((selector) => {
1226
- if (selector === 'input[type="date"]') {
1227
- return {
1228
- filter: jest.fn((filterSelector) => {
1229
- if (filterSelector.includes('start')) {
1230
- return mockStartInput;
1231
- }
1232
- return mockEndInput;
1233
- }),
1234
- length: 2
1235
- };
1236
- }
1237
- return { length: 0 };
1238
- })
1239
- }))
1240
- }))
1241
- };
755
+ const link = makeContainerWithPeriodInputs();
1242
756
 
1243
- InputPeriod.selectPreviousWeek(mockLink, 1);
757
+ InputPeriod.selectPreviousWeek(link, 1);
1244
758
 
1245
- expect(mockStartInput.val).toHaveBeenCalled();
1246
- expect(mockEndInput.val).toHaveBeenCalled();
759
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
760
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1247
761
  });
1248
762
  });
1249
763
 
1250
764
  describe('selectFollowingWeek', () => {
1251
765
  test('should select following week', () => {
1252
- const mockStartInput = { val: jest.fn(), length: 1 };
1253
- const mockEndInput = { val: jest.fn(), length: 1 };
1254
-
1255
- const mockLink = {
1256
- parent: jest.fn(() => ({
1257
- parent: jest.fn(() => ({
1258
- find: jest.fn((selector) => {
1259
- if (selector === 'input[type="date"]') {
1260
- return {
1261
- filter: jest.fn((filterSelector) => {
1262
- if (filterSelector.includes('start')) {
1263
- return mockStartInput;
1264
- }
1265
- return mockEndInput;
1266
- }),
1267
- length: 2
1268
- };
1269
- }
1270
- return { length: 0 };
1271
- })
1272
- }))
1273
- }))
1274
- };
766
+ const link = makeContainerWithPeriodInputs();
1275
767
 
1276
- InputPeriod.selectFollowingWeek(mockLink, 2);
768
+ InputPeriod.selectFollowingWeek(link, 2);
1277
769
 
1278
- expect(mockStartInput.val).toHaveBeenCalled();
1279
- expect(mockEndInput.val).toHaveBeenCalled();
770
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
771
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1280
772
  });
1281
773
  });
1282
774
 
1283
775
  describe('selectCurrentMonth', () => {
1284
776
  test('should select current month period', () => {
1285
- const mockStartInput = { val: jest.fn(), length: 1 };
1286
- const mockEndInput = { val: jest.fn(), length: 1 };
1287
-
1288
- const mockLink = {
1289
- parent: jest.fn(() => ({
1290
- parent: jest.fn(() => ({
1291
- find: jest.fn((selector) => {
1292
- if (selector === 'input[type="date"]') {
1293
- return {
1294
- filter: jest.fn((filterSelector) => {
1295
- if (filterSelector.includes('start')) {
1296
- return mockStartInput;
1297
- }
1298
- return mockEndInput;
1299
- }),
1300
- length: 2
1301
- };
1302
- }
1303
- return { length: 0 };
1304
- })
1305
- }))
1306
- }))
1307
- };
777
+ const link = makeContainerWithPeriodInputs();
1308
778
 
1309
- InputPeriod.selectCurrentMonth(mockLink);
779
+ InputPeriod.selectCurrentMonth(link);
1310
780
 
1311
- expect(global.DateTime.getFirstDayOfMonth).toHaveBeenCalled();
1312
- expect(global.DateTime.getLastDayOfMonth).toHaveBeenCalled();
1313
- expect(mockStartInput.val).toHaveBeenCalled();
1314
- expect(mockEndInput.val).toHaveBeenCalled();
781
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
782
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1315
783
  });
1316
784
  });
1317
785
 
1318
786
  describe('selectPreviousMonth', () => {
1319
787
  test('should select previous month', () => {
1320
- const mockStartInput = { val: jest.fn(), length: 1 };
1321
- const mockEndInput = { val: jest.fn(), length: 1 };
1322
-
1323
- const mockLink = {
1324
- parent: jest.fn(() => ({
1325
- parent: jest.fn(() => ({
1326
- find: jest.fn((selector) => {
1327
- if (selector === 'input[type="date"]') {
1328
- return {
1329
- filter: jest.fn((filterSelector) => {
1330
- if (filterSelector.includes('start')) {
1331
- return mockStartInput;
1332
- }
1333
- return mockEndInput;
1334
- }),
1335
- length: 2
1336
- };
1337
- }
1338
- return { length: 0 };
1339
- })
1340
- }))
1341
- }))
1342
- };
788
+ const link = makeContainerWithPeriodInputs();
1343
789
 
1344
- InputPeriod.selectPreviousMonth(mockLink, 1);
790
+ InputPeriod.selectPreviousMonth(link, 1);
1345
791
 
1346
- expect(mockStartInput.val).toHaveBeenCalled();
1347
- expect(mockEndInput.val).toHaveBeenCalled();
792
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
793
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1348
794
  });
1349
795
  });
1350
796
 
1351
797
  describe('selectFollowingMonth', () => {
1352
798
  test('should select following month', () => {
1353
- const mockStartInput = { val: jest.fn(), length: 1 };
1354
- const mockEndInput = { val: jest.fn(), length: 1 };
1355
-
1356
- const mockLink = {
1357
- parent: jest.fn(() => ({
1358
- parent: jest.fn(() => ({
1359
- find: jest.fn((selector) => {
1360
- if (selector === 'input[type="date"]') {
1361
- return {
1362
- filter: jest.fn((filterSelector) => {
1363
- if (filterSelector.includes('start')) {
1364
- return mockStartInput;
1365
- }
1366
- return mockEndInput;
1367
- }),
1368
- length: 2
1369
- };
1370
- }
1371
- return { length: 0 };
1372
- })
1373
- }))
1374
- }))
1375
- };
799
+ const link = makeContainerWithPeriodInputs();
1376
800
 
1377
- InputPeriod.selectFollowingMonth(mockLink, 3);
801
+ InputPeriod.selectFollowingMonth(link, 3);
1378
802
 
1379
- expect(mockStartInput.val).toHaveBeenCalled();
1380
- expect(mockEndInput.val).toHaveBeenCalled();
803
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
804
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1381
805
  });
1382
806
  });
1383
807
 
1384
808
  describe('selectCurrentYear', () => {
1385
809
  test('should select current year period', () => {
1386
- const mockStartInput = { val: jest.fn(), length: 1 };
1387
- const mockEndInput = { val: jest.fn(), length: 1 };
1388
-
1389
- const mockLink = {
1390
- parent: jest.fn(() => ({
1391
- parent: jest.fn(() => ({
1392
- find: jest.fn((selector) => {
1393
- if (selector === 'input[type="date"]') {
1394
- return {
1395
- filter: jest.fn((filterSelector) => {
1396
- if (filterSelector.includes('start')) {
1397
- return mockStartInput;
1398
- }
1399
- return mockEndInput;
1400
- }),
1401
- length: 2
1402
- };
1403
- }
1404
- return { length: 0 };
1405
- })
1406
- }))
1407
- }))
1408
- };
810
+ const link = makeContainerWithPeriodInputs();
1409
811
 
1410
- InputPeriod.selectCurrentYear(mockLink);
812
+ InputPeriod.selectCurrentYear(link);
1411
813
 
1412
- expect(global.DateTime.getFirstDayOfYear).toHaveBeenCalled();
1413
- expect(global.DateTime.getLastDayOfYear).toHaveBeenCalled();
1414
- expect(mockStartInput.val).toHaveBeenCalled();
1415
- expect(mockEndInput.val).toHaveBeenCalled();
814
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
815
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1416
816
  });
1417
817
  });
1418
818
 
1419
819
  describe('selectPreviousYear', () => {
1420
820
  test('should select previous year', () => {
1421
- const mockStartInput = { val: jest.fn(), length: 1 };
1422
- const mockEndInput = { val: jest.fn(), length: 1 };
1423
-
1424
- const mockLink = {
1425
- parent: jest.fn(() => ({
1426
- parent: jest.fn(() => ({
1427
- find: jest.fn((selector) => {
1428
- if (selector === 'input[type="date"]') {
1429
- return {
1430
- filter: jest.fn((filterSelector) => {
1431
- if (filterSelector.includes('start')) {
1432
- return mockStartInput;
1433
- }
1434
- return mockEndInput;
1435
- }),
1436
- length: 2
1437
- };
1438
- }
1439
- return { length: 0 };
1440
- })
1441
- }))
1442
- }))
1443
- };
821
+ const link = makeContainerWithPeriodInputs();
1444
822
 
1445
- InputPeriod.selectPreviousYear(mockLink, 1);
823
+ InputPeriod.selectPreviousYear(link, 1);
1446
824
 
1447
- expect(mockStartInput.val).toHaveBeenCalled();
1448
- expect(mockEndInput.val).toHaveBeenCalled();
825
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
826
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1449
827
  });
1450
828
  });
1451
829
 
1452
830
  describe('selectFollowingYear', () => {
1453
831
  test('should select following year', () => {
1454
- const mockStartInput = { val: jest.fn(), length: 1 };
1455
- const mockEndInput = { val: jest.fn(), length: 1 };
1456
-
1457
- const mockLink = {
1458
- parent: jest.fn(() => ({
1459
- parent: jest.fn(() => ({
1460
- find: jest.fn((selector) => {
1461
- if (selector === 'input[type="date"]') {
1462
- return {
1463
- filter: jest.fn((filterSelector) => {
1464
- if (filterSelector.includes('start')) {
1465
- return mockStartInput;
1466
- }
1467
- return mockEndInput;
1468
- }),
1469
- length: 2
1470
- };
1471
- }
1472
- return { length: 0 };
1473
- })
1474
- }))
1475
- }))
1476
- };
832
+ const link = makeContainerWithPeriodInputs();
1477
833
 
1478
- InputPeriod.selectFollowingYear(mockLink, 2);
834
+ InputPeriod.selectFollowingYear(link, 2);
1479
835
 
1480
- expect(mockStartInput.val).toHaveBeenCalled();
1481
- expect(mockEndInput.val).toHaveBeenCalled();
836
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
837
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1482
838
  });
1483
839
  });
1484
840
 
@@ -1486,17 +842,10 @@ describe('InputPeriod', () => {
1486
842
  test('should return early when no start input found', () => {
1487
843
  const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
1488
844
 
1489
- const mockLink = {
1490
- parent: jest.fn(() => ({
1491
- parent: jest.fn(() => ({
1492
- find: jest.fn(() => ({
1493
- filter: jest.fn(() => ({ length: 0 }))
1494
- }))
1495
- }))
1496
- }))
1497
- };
845
+ document.body.innerHTML = `<div><div><a href="#" id="link"></a></div></div>`;
846
+ const link = document.getElementById('link');
1498
847
 
1499
- InputPeriod.selectPeriod(mockLink, new Date(), new Date());
848
+ InputPeriod.selectPeriod(link, new Date(), new Date());
1500
849
 
1501
850
  expect(consoleLogSpy).toHaveBeenCalledWith('no period input found');
1502
851
  consoleLogSpy.mockRestore();
@@ -1505,55 +854,32 @@ describe('InputPeriod', () => {
1505
854
  test('should return early when no end input found', () => {
1506
855
  const consoleLogSpy = jest.spyOn(console, 'log').mockImplementation();
1507
856
 
1508
- const mockLink = {
1509
- parent: jest.fn(() => ({
1510
- parent: jest.fn(() => ({
1511
- find: jest.fn(() => ({
1512
- filter: jest.fn((filterSelector) => {
1513
- if (filterSelector.includes('start')) {
1514
- return { length: 1, val: jest.fn() };
1515
- }
1516
- return { length: 0 };
1517
- })
1518
- }))
1519
- }))
1520
- }))
1521
- };
857
+ document.body.innerHTML = `
858
+ <div>
859
+ <div>
860
+ <a href="#" id="link"></a>
861
+ <input type="date" name="start_date" />
862
+ </div>
863
+ </div>
864
+ `;
865
+ const link = document.getElementById('link');
1522
866
 
1523
- InputPeriod.selectPeriod(mockLink, new Date(), new Date());
867
+ InputPeriod.selectPeriod(link, new Date(), new Date());
1524
868
 
1525
869
  expect(consoleLogSpy).toHaveBeenCalledWith('no period input found');
1526
870
  consoleLogSpy.mockRestore();
1527
871
  });
1528
872
 
1529
873
  test('should set values on both inputs when found', () => {
1530
- const mockStartInput = { val: jest.fn(), length: 1 };
1531
- const mockEndInput = { val: jest.fn(), length: 1 };
1532
-
1533
- const mockLink = {
1534
- parent: jest.fn(() => ({
1535
- parent: jest.fn(() => ({
1536
- find: jest.fn(() => ({
1537
- filter: jest.fn((filterSelector) => {
1538
- if (filterSelector.includes('start')) {
1539
- return mockStartInput;
1540
- }
1541
- return mockEndInput;
1542
- })
1543
- }))
1544
- }))
1545
- }))
1546
- };
874
+ const link = makeContainerWithPeriodInputs();
1547
875
 
1548
876
  const startDate = new Date(2023, 5, 1);
1549
877
  const endDate = new Date(2023, 5, 30);
1550
878
 
1551
- InputPeriod.selectPeriod(mockLink, startDate, endDate);
879
+ InputPeriod.selectPeriod(link, startDate, endDate);
1552
880
 
1553
- expect(mockStartInput.val).toHaveBeenCalled();
1554
- expect(mockEndInput.val).toHaveBeenCalled();
1555
- expect(global.DateTime.getDateForInputDate).toHaveBeenCalledWith(startDate);
1556
- expect(global.DateTime.getDateForInputDate).toHaveBeenCalledWith(endDate);
881
+ expect(document.querySelector('input[name="start_date"]').value).not.toBe('');
882
+ expect(document.querySelector('input[name="end_date"]').value).not.toBe('');
1557
883
  });
1558
884
  });
1559
885
  });