arrangement.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.yearAt(2024);
  10. expect((arrangement as any).dayDetails.year).toBe(2024);
  11. });
  12. it('should mark holiday correctly', () => {
  13. arrangement.nyd();
  14. expect((arrangement as any).dayDetails.holiday).toBe(Holiday.NY);
  15. });
  16. it('should save holiday correctly', () => {
  17. arrangement.yearAt(2024).nyd().rest(1, 1);
  18. const date = dayjs('2024-01-01').format('YYYY-MM-DD');
  19. expect(arrangement.holidays[date]).toBe(Holiday.NY);
  20. });
  21. it('should save workday correctly', () => {
  22. arrangement.yearAt(2024).sf().work(2, 4);
  23. const date = dayjs('2024-02-04').format('YYYY-MM-DD');
  24. expect(arrangement.workdays[date]).toBe(Holiday.S);
  25. });
  26. it('should save in-lieu day correctly', () => {
  27. arrangement.yearAt(2024).maf().inLieu(9, 16);
  28. const date = dayjs('2024-09-16').format('YYYY-MM-DD');
  29. expect(arrangement.inLieuDays[date]).toBe(Holiday.M);
  30. });
  31. it('should save holiday range correctly', () => {
  32. arrangement.yearAt(2024).sf().rest(2, 10).to(2, 12);
  33. const dates = ['2024-02-10', '2024-02-11', '2024-02-12'].map(date =>
  34. dayjs(date).format('YYYY-MM-DD')
  35. );
  36. dates.forEach(date => {
  37. expect(arrangement.holidays[date]).toBe(Holiday.S);
  38. });
  39. });
  40. it('should throw error if year is not set before saving holiday', () => {
  41. expect(() => arrangement.nyd().rest(1, 1)).toThrow(
  42. 'should set year before saving holiday'
  43. );
  44. });
  45. it('should throw error if holiday is not set before saving holiday', () => {
  46. expect(() => arrangement.yearAt(2024).rest(1, 1)).toThrow(
  47. 'should set holiday before saving holiday'
  48. );
  49. });
  50. it('should throw error if end date is before start date in holiday range', () => {
  51. arrangement.yearAt(2024).sf().rest(2, 10);
  52. expect(() => arrangement.to(2, 9)).toThrow('end date should be after start date');
  53. });
  54. it('should throw error if year/month/day is not set before saving holiday range', () => {
  55. expect(() => arrangement.to(2, 10)).toThrow(
  56. 'should set year/month/day before saving holiday range'
  57. );
  58. });
  59. });