Skip to content
This repository has been archived by the owner on Aug 21, 2023. It is now read-only.

Commit

Permalink
Fix issue with incorrectly computed isMonthInThePast result
Browse files Browse the repository at this point in the history
  • Loading branch information
Nickolas Kenyeres committed Jan 11, 2021
1 parent bf6c60b commit b33d2ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/components/Datepicker/Datepicker.utils.js
Expand Up @@ -47,11 +47,14 @@ export function isToday(someDate) {
*/
export function isMonthInThePast(someDate) {
const today = new Date()
const thisYear = today.getFullYear()
const someYear = someDate.getFullYear()

return (
someDate.getFullYear() <= today.getFullYear() &&
someDate.getMonth() < today.getMonth()
)
if (someYear === thisYear) {
return someDate.getMonth() < today.getMonth()
}

return someYear < thisYear
}

export function isInsideRange({ check, to, from }) {
Expand Down
23 changes: 12 additions & 11 deletions src/components/Datepicker/Datepicker.utils.test.js
Expand Up @@ -34,17 +34,18 @@ describe('Datepicker Utils', () => {
expect(isToday(new Date(1992, 3, 24))).toBeFalsy()
})

test('isMonthInThePast', () => {
const currentDate = new Date()
const pastDate = new Date(2020, 5, 29)
const futureDate = new Date(
currentDate.getFullYear,
currentDate.getMonth + 1,
4
)

expect(isMonthInThePast(pastDate)).toBeTruthy()
expect(isMonthInThePast(futureDate)).toBeFalsy()
test.each([
[new Date(2020, 10, 29), new Date(2020, 5, 29), true],
[new Date(2021, 1, 11), new Date(2020, 5, 29), true],
[new Date(2021, 1, 11), new Date(2021, 1, 11), false],
[new Date(2021, 1, 11), new Date(2021, 1, 12), false],
])('isMonthInThePast(%i)', (mockDate, someDate, expected) => {
const spyDateFn = jest
.spyOn(global, 'Date')
.mockImplementation(() => mockDate)
expect(isMonthInThePast(someDate)).toEqual(expected)
expect(spyDateFn).toHaveBeenCalled()
spyDateFn.mockRestore()
})

test('getValidDateTimeString', () => {
Expand Down

0 comments on commit b33d2ab

Please sign in to comment.