From 4de142cb7c538b2bc6eba44e12cb8e5ec51c7064 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Wed, 1 Nov 2023 01:48:47 +1100 Subject: [PATCH] fix: Check for same day, not with <= (#12167) * fix: Check for same day, not with <= * Cover this situation with a test * Make the test timezone-independent --- .../getAvailableDatesInMonth.timezone.test.ts | 18 ++++++++++++++++++ .../calendars/lib/getAvailableDatesInMonth.ts | 5 ++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/features/calendars/lib/getAvailableDatesInMonth.timezone.test.ts b/packages/features/calendars/lib/getAvailableDatesInMonth.timezone.test.ts index c8475e3311..0d8c5d10aa 100644 --- a/packages/features/calendars/lib/getAvailableDatesInMonth.timezone.test.ts +++ b/packages/features/calendars/lib/getAvailableDatesInMonth.timezone.test.ts @@ -1,5 +1,6 @@ import { describe, expect, test, vi } from "vitest"; +import dayjs from "@calcom/dayjs"; import { getAvailableDatesInMonth } from "@calcom/features/calendars/lib/getAvailableDatesInMonth"; import { daysInMonth, yyyymmdd } from "@calcom/lib/date-fns"; @@ -63,5 +64,22 @@ describe("Test Suite: Date Picker", () => { vi.setSystemTime(vi.getRealSystemTime()); vi.useRealTimers(); }); + + test("it returns the correct responses end of month", () => { + // test a date at one minute past midnight, end of month. + // we use dayjs() as the system timezone can still modify the Date. + vi.useFakeTimers().setSystemTime(dayjs().endOf("month").startOf("day").add(1, "second").toDate()); + + const currentDate = new Date(); + const result = getAvailableDatesInMonth({ + browsingDate: currentDate, + }); + + expect(result).toHaveLength(1); + + // Undo the forced time we applied earlier, reset to system default. + vi.setSystemTime(vi.getRealSystemTime()); + vi.useRealTimers(); + }); }); }); diff --git a/packages/features/calendars/lib/getAvailableDatesInMonth.ts b/packages/features/calendars/lib/getAvailableDatesInMonth.ts index 8e50ef9793..a00f83d516 100644 --- a/packages/features/calendars/lib/getAvailableDatesInMonth.ts +++ b/packages/features/calendars/lib/getAvailableDatesInMonth.ts @@ -1,3 +1,4 @@ +import dayjs from "@calcom/dayjs"; import { daysInMonth, yyyymmdd } from "@calcom/lib/date-fns"; // calculate the available dates in the month: @@ -21,7 +22,9 @@ export function getAvailableDatesInMonth({ ); for ( let date = browsingDate > minDate ? browsingDate : minDate; - date <= lastDateOfMonth; + // Check if date is before the last date of the month + // or is the same day, in the same month, in the same year. + date < lastDateOfMonth || dayjs(date).isSame(lastDateOfMonth, "day"); date = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1) ) { // intersect included dates