index.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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('should throw an error for invalid date', () => {
  13. expect(() => isHoliday('invalid-date')).toThrow(
  14. 'unsupported type object, expected type is Date or Dayjs'
  15. );
  16. });
  17. test('isHoliday should return correct boolean values', () => {
  18. const date1 = '2024-05-01';
  19. const date2 = '2024-05-06';
  20. expect(isHoliday(date1)).toBe(true);
  21. expect(isHoliday(date2)).toBe(false);
  22. });
  23. test('isWorkday should return correct boolean values', () => {
  24. const date1 = '2024-05-01';
  25. const date2 = '2024-05-06';
  26. expect(isWorkday(date1)).toBe(false);
  27. expect(isWorkday(date2)).toBe(true);
  28. });
  29. test('isInLieu should return correct boolean values', () => {
  30. const date1 = '2024-05-01';
  31. const date2 = '2024-05-03';
  32. expect(isInLieu(date1)).toBe(false);
  33. expect(isInLieu(date2)).toBe(true);
  34. });
  35. test('getDayDetail should return correct details', () => {
  36. const date = '2024-04-29';
  37. const detail = getDayDetail(date);
  38. expect(detail).toEqual({
  39. date: '2024-04-29',
  40. work: true,
  41. name: "Monday",
  42. });
  43. });
  44. test('getDayDetail should return correct details', () => {
  45. const date = '2025-01-26';
  46. const detail = getDayDetail(date);
  47. expect(detail).toEqual({
  48. date: '2025-01-26',
  49. work: true,
  50. name: "Spring Festival,春节,4",
  51. });
  52. });
  53. test('getDayDetail should return correct details', () => {
  54. const date = '2024-05-01';
  55. const detail = getDayDetail(date);
  56. expect(detail).toEqual({
  57. date: '2024-05-01',
  58. work: false,
  59. name: "Labour Day,劳动节,1",
  60. });
  61. });
  62. test('getDayDetail should return correct details', () => {
  63. const date = '2025-05-01';
  64. const detail = getDayDetail(date);
  65. expect(detail).toEqual({
  66. date: '2025-05-01',
  67. work: false,
  68. name: "Labour Day,劳动节,2",
  69. });
  70. });
  71. test('getHolidaysInRange should return correct holidays within a range', () => {
  72. const start = '2024-05-01';
  73. const end = '2024-05-31';
  74. const holidaysInRange = getHolidaysInRange(start, end, false);
  75. expect(holidaysInRange).toContain('2024-05-01');
  76. });
  77. test('getHolidaysInRange should return correct holidays within a range', () => {
  78. const start = '2024-05-01';
  79. const end = '2024-05-31';
  80. const holidaysInRange = getHolidaysInRange(start, end, true);
  81. expect(holidaysInRange).toContain('2024-05-12');
  82. });
  83. test('getWorkdaysInRange should return correct workdays within a range', () => {
  84. const start = '2024-05-01';
  85. const end = '2024-05-31';
  86. const workdaysInRange = getWorkdaysInRange(start, end, false);
  87. expect(workdaysInRange).toContain('2024-05-06');
  88. });
  89. test('getWorkdaysInRange should return correct workdays within a range', () => {
  90. const start = '2024-05-01';
  91. const end = '2024-05-31';
  92. const workdaysInRange = getWorkdaysInRange(start, end, true);
  93. expect(workdaysInRange).toContain('2024-05-11');
  94. });
  95. test('findWorkday should return correct workday', () => {
  96. const date = '2024-05-01';
  97. const nextWorkday = findWorkday(1, date);
  98. expect(nextWorkday).toBe('2024-05-06');
  99. });
  100. test('findWorkday should return correct workday', () => {
  101. const date = '2024-05-11';
  102. const nextWorkday = findWorkday(0, date);
  103. expect(nextWorkday).toBe('2024-05-11');
  104. });
  105. test('findWorkday should return correct workday', () => {
  106. const date = '2024-05-12';
  107. const nextWorkday = findWorkday(0, date);
  108. expect(nextWorkday).toBe('2024-05-13');
  109. });
  110. });
  111. describe('Arrangement Class', () => {
  112. let arrangement: Arrangement;
  113. beforeEach(() => {
  114. arrangement = new Arrangement();
  115. });
  116. test('should correctly handle 2023 holidays', () => {
  117. arrangement.y(2024)
  118. .ny().r(1, 1)
  119. .s().r(2, 10).to(2, 17).w(2, 4).w(2, 18).i(2, 15).to(2, 16)
  120. .t().r(4, 4).to(4, 6).w(4, 7).i(4, 5)
  121. .l().r(5, 1).to(5, 5).w(4, 28).w(5, 11).i(5, 2).to(5, 3)
  122. .d().r(6, 10)
  123. .m().r(9, 15).to(9, 17).w(9, 14).i(9, 16)
  124. .n().r(10, 1).to(10, 7).w(9, 29).w(10, 12).i(10, 4).i(10, 7)
  125. expect(arrangement.holidays).toHaveProperty('2024-05-01');
  126. expect(arrangement.holidays).toHaveProperty('2024-05-02');
  127. expect(arrangement.holidays).toHaveProperty('2024-05-04');
  128. expect(arrangement.holidays).toHaveProperty('2024-05-05');
  129. expect(arrangement.workdays).toHaveProperty('2024-04-28');
  130. expect(arrangement.workdays).toHaveProperty('2024-05-11');
  131. });
  132. });