index.test.ts 2.9 KB

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