From b33d2abd8f44fa0128b722030168c0f33dc5860b Mon Sep 17 00:00:00 2001 From: Nickolas Kenyeres Date: Mon, 11 Jan 2021 17:30:27 -0500 Subject: [PATCH] Fix issue with incorrectly computed isMonthInThePast result --- src/components/Datepicker/Datepicker.utils.js | 11 +++++---- .../Datepicker/Datepicker.utils.test.js | 23 ++++++++++--------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/components/Datepicker/Datepicker.utils.js b/src/components/Datepicker/Datepicker.utils.js index ade460702..b95e0a5a5 100644 --- a/src/components/Datepicker/Datepicker.utils.js +++ b/src/components/Datepicker/Datepicker.utils.js @@ -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 }) { diff --git a/src/components/Datepicker/Datepicker.utils.test.js b/src/components/Datepicker/Datepicker.utils.test.js index 034460533..6589cee96 100644 --- a/src/components/Datepicker/Datepicker.utils.test.js +++ b/src/components/Datepicker/Datepicker.utils.test.js @@ -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', () => {