index.test.ts 2.7 KB

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