Skip to content

Commit

Permalink
fix(parental-leave): If startDate is the 31st then the endDate should…
Browse files Browse the repository at this point in the history
… be 1 day later (#16814)

* fix(parental-leave): If startDate is the 31st then the endDate should be 1 day later

* fix period length in months calculation and tests

---------

Co-authored-by: hfhelgason <[email protected]>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 13, 2024
1 parent f27b1ba commit 5baabc6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1434,17 +1434,23 @@ describe('calculateEndDateForPeriodWithStartAndLength', () => {
calculateEndDateForPeriodWithStartAndLength('2021-06-15', 1),
).toEqual(new Date(2021, 6, 14))

expect(
calculateEndDateForPeriodWithStartAndLength('2021-02-28', 1),
).toEqual(new Date(2021, 2, 27))
})

it('should calculate month + n for whole months when startDate is 31', () => {
expect(
calculateEndDateForPeriodWithStartAndLength('2021-01-31', 1),
).toEqual(new Date(2021, 1, 27))
).toEqual(new Date(2021, 1, 28))

expect(
calculateEndDateForPeriodWithStartAndLength('2021-03-31', 1),
).toEqual(new Date(2021, 3, 29))
).toEqual(new Date(2021, 3, 30))

expect(
calculateEndDateForPeriodWithStartAndLength('2021-02-28', 1),
).toEqual(new Date(2021, 2, 27))
calculateEndDateForPeriodWithStartAndLength('2020-12-31', 1),
).toEqual(new Date(2021, 0, 31))
})

it('should calculate month + n and multiply remainder with 28', () => {
Expand Down Expand Up @@ -1507,11 +1513,11 @@ describe('calculatePeriodLengthInMonths', () => {
})

it('should calculate half months correctly', () => {
expect(calculatePeriodLengthInMonths('2021-01-01', '2021-01-14')).toBe(0.5)
expect(calculatePeriodLengthInMonths('2021-01-01', '2021-01-15')).toBe(0.5)
expect(calculatePeriodLengthInMonths('2021-01-14', '2021-01-28')).toBe(0.5)
expect(calculatePeriodLengthInMonths('2021-01-31', '2021-02-13')).toBe(0.5)
expect(calculatePeriodLengthInMonths('2021-02-28', '2021-03-13')).toBe(0.5)
expect(calculatePeriodLengthInMonths('2021-02-28', '2021-03-27')).toBe(1)
expect(calculatePeriodLengthInMonths('2021-02-28', '2021-03-14')).toBe(0.5)
expect(calculatePeriodLengthInMonths('2021-02-28', '2021-03-28')).toBe(1)
expect(calculatePeriodLengthInMonths('2021-02-28', '2021-04-13')).toBe(1.5)
expect(calculatePeriodLengthInMonths('2021-02-28', '2021-05-13')).toBe(2.5)
expect(calculatePeriodLengthInMonths('2021-02-28', '2021-07-13')).toBe(4.5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1431,6 +1431,9 @@ export const calculateEndDateForPeriodWithStartAndLength = (

// If startDay is first day of the month and daysToAdd = 0
if (daysToAdd === 0) {
if (start.getDate() === 31) {
endDate = addDays(endDate, 1)
}
return endDate
}

Expand Down Expand Up @@ -1467,9 +1470,10 @@ export const calculatePeriodLengthInMonths = (
const end = parseISO(endDate)

const diffMonths = differenceInMonths(end, start)
const diffDays = differenceInDays(addMonths(end, -diffMonths), start)
const diffDays = differenceInDays(end, addMonths(start, diffMonths))

const roundedDays = Math.min((diffDays / 28) * 100, 100) / 100
const roundedDays =
Math.min((diffDays / getDaysInMonth(end)) * 100, 100) / 100

return round(diffMonths + roundedDays, 1)
}
Expand Down

0 comments on commit 5baabc6

Please sign in to comment.