arrangement.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import dayjs from '../../src/utils/dayjs';
  2. import Arrangement, { Holiday} from '../../src/holidays/arrangement';
  3. describe('Arrangement class', () => {
  4. let arrangement: Arrangement;
  5. beforeEach(() => {
  6. arrangement = new Arrangement();
  7. });
  8. it('should set year correctly', () => {
  9. arrangement.y(2024);
  10. expect((arrangement as any).dayDetails.year).toBe(2024);
  11. });
  12. it('should mark holiday correctly', () => {
  13. arrangement.ny();
  14. expect((arrangement as any).dayDetails.holiday).toBe(Holiday.NY);
  15. });
  16. it('should mark holiday correctly', () => {
  17. arrangement.n();
  18. expect((arrangement as any).dayDetails.holiday).toBe(Holiday.N);
  19. });
  20. it('should save holiday correctly', () => {
  21. arrangement.y(2024).ny().r(1, 1);
  22. const date = dayjs('2024-01-01').format('YYYY-MM-DD');
  23. expect(arrangement.holidays[date]).toBe(Holiday.NY);
  24. });
  25. it('should save workday correctly', () => {
  26. arrangement.y(2024).s().w(2, 4);
  27. const date = dayjs('2024-02-04').format('YYYY-MM-DD');
  28. expect(arrangement.workdays[date]).toBe(Holiday.S);
  29. });
  30. it('should save in-lieu day correctly', () => {
  31. arrangement.y(2024).m().i(9, 16);
  32. const date = dayjs('2024-09-16').format('YYYY-MM-DD');
  33. expect(arrangement.inLieuDays[date]).toBe(Holiday.M);
  34. });
  35. it('should save holiday range correctly', () => {
  36. arrangement.y(2024).s().r(2, 10).to(2, 12);
  37. const dates = ['2024-02-10', '2024-02-11', '2024-02-12'].map(date =>
  38. dayjs(date).format('YYYY-MM-DD')
  39. );
  40. dates.forEach(date => {
  41. expect(arrangement.holidays[date]).toBe(Holiday.S);
  42. });
  43. });
  44. it('should throw error if year is not set before saving holiday', () => {
  45. expect(() => arrangement.ny().r(1, 1)).toThrow(
  46. 'should set year before saving holiday'
  47. );
  48. });
  49. it('should throw error if holiday is not set before saving holiday', () => {
  50. expect(() => arrangement.y(2024).r(1, 1)).toThrow(
  51. 'should set holiday before saving holiday'
  52. );
  53. });
  54. it('should throw error if end date is before start date in holiday range', () => {
  55. arrangement.y(2024).s().r(2, 10);
  56. expect(() => arrangement.to(2, 9)).toThrow('end date should be after start date');
  57. });
  58. it('should throw error if year/month/day is not set before saving holiday range', () => {
  59. expect(() => arrangement.to(2, 10)).toThrow(
  60. 'should set year/month/day before saving holiday range'
  61. );
  62. });
  63. });