|
1 | 1 | import type * as ReactNavigationNative from '@react-navigation/native'; |
2 | 2 | import {fireEvent, render, screen, userEvent, within} from '@testing-library/react-native'; |
3 | | -import {addMonths, addYears, endOfMonth, startOfMonth, subMonths, subYears} from 'date-fns'; |
| 3 | +import {addMonths, addYears, subMonths, subYears} from 'date-fns'; |
4 | 4 | import CalendarPicker from '@components/DatePicker/CalendarPicker'; |
5 | 5 | import DateUtils from '@libs/DateUtils'; |
6 | 6 |
|
@@ -425,58 +425,28 @@ describe('CalendarPicker', () => { |
425 | 425 | }); |
426 | 426 |
|
427 | 427 | test('month picker filtering should exclude months before minDate', () => { |
428 | | - const currentYear = 2023; |
429 | | - const minDate = new Date('2023-06-01'); |
430 | | - const maxDate = new Date('2030-12-31'); |
431 | | - |
432 | | - const filteredMonths = monthNames |
433 | | - .map((month, index) => { |
434 | | - const monthStart = startOfMonth(new Date(currentYear, index)); |
435 | | - const monthEnd = endOfMonth(new Date(currentYear, index)); |
436 | | - const isBeforeMin = monthEnd < startOfMonth(new Date(minDate)); |
437 | | - const isAfterMax = monthStart > endOfMonth(new Date(maxDate)); |
438 | | - if (isBeforeMin || isAfterMax) { |
439 | | - return null; |
440 | | - } |
441 | | - return {text: month, value: index}; |
442 | | - }) |
443 | | - .filter(Boolean); |
| 428 | + const filteredMonths = DateUtils.getFilteredMonthItems(monthNames, 2023, 6, new Date('2023-06-01'), new Date('2030-12-31')); |
444 | 429 |
|
445 | 430 | // Months before June (index 5) should be excluded |
446 | | - expect(filteredMonths.find((m) => m?.value === 0)).toBeUndefined(); |
447 | | - expect(filteredMonths.find((m) => m?.value === 4)).toBeUndefined(); |
| 431 | + expect(filteredMonths.find((m) => m.value === 0)).toBeUndefined(); |
| 432 | + expect(filteredMonths.find((m) => m.value === 4)).toBeUndefined(); |
448 | 433 |
|
449 | 434 | // June and later months should be included |
450 | | - expect(filteredMonths.find((m) => m?.value === 5)).toBeTruthy(); |
451 | | - expect(filteredMonths.find((m) => m?.value === 11)).toBeTruthy(); |
| 435 | + expect(filteredMonths.find((m) => m.value === 5)).toBeTruthy(); |
| 436 | + expect(filteredMonths.find((m) => m.value === 11)).toBeTruthy(); |
452 | 437 | expect(filteredMonths).toHaveLength(7); |
453 | 438 | }); |
454 | 439 |
|
455 | 440 | test('month picker filtering should exclude months after maxDate', () => { |
456 | | - const currentYear = 2023; |
457 | | - const minDate = new Date('2020-01-01'); |
458 | | - const maxDate = new Date('2023-09-30'); |
459 | | - |
460 | | - const filteredMonths = monthNames |
461 | | - .map((month, index) => { |
462 | | - const monthStart = startOfMonth(new Date(currentYear, index)); |
463 | | - const monthEnd = endOfMonth(new Date(currentYear, index)); |
464 | | - const isBeforeMin = monthEnd < startOfMonth(new Date(minDate)); |
465 | | - const isAfterMax = monthStart > endOfMonth(new Date(maxDate)); |
466 | | - if (isBeforeMin || isAfterMax) { |
467 | | - return null; |
468 | | - } |
469 | | - return {text: month, value: index}; |
470 | | - }) |
471 | | - .filter(Boolean); |
| 441 | + const filteredMonths = DateUtils.getFilteredMonthItems(monthNames, 2023, 0, new Date('2020-01-01'), new Date('2023-09-30')); |
472 | 442 |
|
473 | 443 | // Months after September (index 8) should be excluded |
474 | | - expect(filteredMonths.find((m) => m?.value === 10)).toBeUndefined(); |
475 | | - expect(filteredMonths.find((m) => m?.value === 11)).toBeUndefined(); |
| 444 | + expect(filteredMonths.find((m) => m.value === 10)).toBeUndefined(); |
| 445 | + expect(filteredMonths.find((m) => m.value === 11)).toBeUndefined(); |
476 | 446 |
|
477 | 447 | // September and earlier months should be included |
478 | | - expect(filteredMonths.find((m) => m?.value === 8)).toBeTruthy(); |
479 | | - expect(filteredMonths.find((m) => m?.value === 0)).toBeTruthy(); |
| 448 | + expect(filteredMonths.find((m) => m.value === 8)).toBeTruthy(); |
| 449 | + expect(filteredMonths.find((m) => m.value === 0)).toBeTruthy(); |
480 | 450 | expect(filteredMonths).toHaveLength(9); |
481 | 451 | }); |
482 | 452 | }); |
0 commit comments