index.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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: "Labour Day,劳动节,1",
  37. });
  38. });
  39. test('getDayDetail should return correct details', () => {
  40. const date = '2025-05-01';
  41. const detail = getDayDetail(date);
  42. expect(detail).toEqual({
  43. date: '2025-05-01',
  44. work: false,
  45. name: "Labour Day,劳动节,2",
  46. });
  47. });
  48. test('getHolidaysInRange should return correct holidays within a range', () => {
  49. const start = '2024-05-01';
  50. const end = '2024-05-31';
  51. const holidaysInRange = getHolidaysInRange(start, end, true);
  52. expect(holidaysInRange).toContain('2024-05-01');
  53. });
  54. test('getWorkdaysInRange should return correct workdays within a range', () => {
  55. const start = '2024-05-01';
  56. const end = '2024-05-31';
  57. const workdaysInRange = getWorkdaysInRange(start, end, true);
  58. expect(workdaysInRange).toContain('2024-05-06');
  59. });
  60. test('findWorkday should return correct workday', () => {
  61. const date = '2024-05-01';
  62. const nextWorkday = findWorkday(1, date);
  63. expect(nextWorkday).toBe('2024-05-06');
  64. });
  65. });
  66. describe('Arrangement Class', () => {
  67. let arrangement: Arrangement;
  68. beforeEach(() => {
  69. arrangement = new Arrangement();
  70. });
  71. test('should correctly handle 2023 holidays', () => {
  72. arrangement.y(2024)
  73. .ny().r(1, 1)
  74. .s().r(2, 10).to(2, 17).w(2, 4).w(2, 18).i(2, 15).to(2, 16)
  75. .t().r(4, 4).to(4, 6).w(4, 7).i(4, 5)
  76. .l().r(5, 1).to(5, 5).w(4, 28).w(5, 11).i(5, 2).to(5, 3)
  77. .d().r(6, 10)
  78. .m().r(9, 15).to(9, 17).w(9, 14).i(9, 16)
  79. .n().r(10, 1).to(10, 7).w(9, 29).w(10, 12).i(10, 4).i(10, 7)
  80. expect(arrangement.holidays).toHaveProperty('2024-05-01');
  81. expect(arrangement.holidays).toHaveProperty('2024-05-02');
  82. expect(arrangement.holidays).toHaveProperty('2024-05-04');
  83. expect(arrangement.holidays).toHaveProperty('2024-05-05');
  84. expect(arrangement.workdays).toHaveProperty('2024-04-28');
  85. expect(arrangement.workdays).toHaveProperty('2024-05-11');
  86. });
  87. });