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
pull/12168/head^2
Alex van Andel 2023-11-01 01:48:47 +11:00 committed by GitHub
parent b4d27a9326
commit 4de142cb7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import { describe, expect, test, vi } from "vitest"; import { describe, expect, test, vi } from "vitest";
import dayjs from "@calcom/dayjs";
import { getAvailableDatesInMonth } from "@calcom/features/calendars/lib/getAvailableDatesInMonth"; import { getAvailableDatesInMonth } from "@calcom/features/calendars/lib/getAvailableDatesInMonth";
import { daysInMonth, yyyymmdd } from "@calcom/lib/date-fns"; import { daysInMonth, yyyymmdd } from "@calcom/lib/date-fns";
@ -63,5 +64,22 @@ describe("Test Suite: Date Picker", () => {
vi.setSystemTime(vi.getRealSystemTime()); vi.setSystemTime(vi.getRealSystemTime());
vi.useRealTimers(); 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();
});
}); });
}); });

View File

@ -1,3 +1,4 @@
import dayjs from "@calcom/dayjs";
import { daysInMonth, yyyymmdd } from "@calcom/lib/date-fns"; import { daysInMonth, yyyymmdd } from "@calcom/lib/date-fns";
// calculate the available dates in the month: // calculate the available dates in the month:
@ -21,7 +22,9 @@ export function getAvailableDatesInMonth({
); );
for ( for (
let date = browsingDate > minDate ? browsingDate : minDate; 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) date = new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1)
) { ) {
// intersect included dates // intersect included dates