index.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import dayjs from 'dayjs';
  2. import { wrapDate, getDates } from '../../src/utils';
  3. import { holidays, workdays, inLieuDays } from '../../src/holidays/constants';
  4. import Arrangement, { Holiday } from '../../src/holidays/arrangement';
  5. import {
  6. isHoliday,
  7. isWorkday,
  8. isInLieu,
  9. getDayDetail,
  10. getHolidays,
  11. getWorkdays,
  12. findWorkday,
  13. } from '../../src/holidays';
  14. describe('Holiday Functions', () => {
  15. test('isHoliday should return correct boolean values', () => {
  16. const date1 = '2024-05-01';
  17. const date2 = '2024-05-06';
  18. expect(isHoliday(date1)).toBe(true);
  19. expect(isHoliday(date2)).toBe(false);
  20. });
  21. test('isWorkday should return correct boolean values', () => {
  22. const date1 = '2024-05-01';
  23. const date2 = '2024-05-06';
  24. expect(isWorkday(date1)).toBe(false);
  25. expect(isWorkday(date2)).toBe(true);
  26. });
  27. test('isInLieu should return correct boolean values', () => {
  28. const date1 = '2024-05-01';
  29. const date2 = '2024-05-03';
  30. expect(isInLieu(date1)).toBe(false);
  31. expect(isInLieu(date2)).toBe(true);
  32. });
  33. test('getDayDetail should return correct details', () => {
  34. const date = '2024-05-01';
  35. const detail = getDayDetail(date);
  36. expect(detail).toEqual({
  37. date: '2024-05-01',
  38. work: false,
  39. name: holidays['2024-05-01'],
  40. });
  41. });
  42. test('getHolidays should return correct holidays within a range', () => {
  43. const start = '2024-05-01';
  44. const end = '2024-05-31';
  45. const holidaysInRange = getHolidays(start, end, true);
  46. expect(holidaysInRange).toContain('2024-05-01');
  47. });
  48. test('getWorkdays should return correct workdays within a range', () => {
  49. const start = '2024-05-01';
  50. const end = '2024-05-31';
  51. const workdaysInRange = getWorkdays(start, end, true);
  52. expect(workdaysInRange).toContain('2024-05-06');
  53. });
  54. test('findWorkday should return correct workday', () => {
  55. const date = '2024-05-01';
  56. const nextWorkday = findWorkday(1, date);
  57. expect(nextWorkday).toBe('2024-05-06');
  58. });
  59. });
  60. describe('Arrangement Class', () => {
  61. let arrangement: Arrangement;
  62. beforeEach(() => {
  63. arrangement = new Arrangement();
  64. });
  65. test('should correctly handle 2023 holidays', () => {
  66. arrangement.yearAt(2024)
  67. .nyd().rest(1, 1)
  68. .sf().rest(2, 10).to(2, 17).work(2, 4).work(2, 18).inLieu(2, 15).to(2, 16)
  69. .tsd().rest(4, 4).to(4, 6).work(4, 7).inLieu(4, 5)
  70. .ld().rest(5, 1).to(5, 5).work(4, 28).work(5, 11).inLieu(5, 2).to(5, 3)
  71. .dbf().rest(6, 10)
  72. .maf().rest(9, 15).to(9, 17).work(9, 14).inLieu(9, 16)
  73. .nd().rest(10, 1).to(10, 7).work(9, 29).work(10, 12).inLieu(10, 4).inLieu(10, 7)
  74. expect(arrangement.holidays).toHaveProperty('2024-05-01');
  75. expect(arrangement.holidays).toHaveProperty('2024-05-02');
  76. expect(arrangement.holidays).toHaveProperty('2024-05-04');
  77. expect(arrangement.holidays).toHaveProperty('2024-05-05');
  78. expect(arrangement.workdays).toHaveProperty('2024-04-28');
  79. expect(arrangement.workdays).toHaveProperty('2024-05-11');
  80. });
  81. });