Compare commits

...

3 Commits

Author SHA1 Message Date
Alex van Andel a856ae1733
Merge branch 'main' into bugfix/failing-tests-post-dst 2023-10-31 23:49:03 +11:00
Alex van Andel aa0c5185e4 fix: don't hardcode 12'clock 2023-10-31 12:48:25 +00:00
Alex van Andel a1820e0730 fix: two failing test cases that were incorrect 2023-10-30 23:28:09 +00:00
1 changed files with 27 additions and 20 deletions

View File

@ -1,12 +1,12 @@
import { describe, expect, it } from "vitest"; import { describe, expect, it } from "vitest";
import type { Dayjs } from "@calcom/dayjs";
import dayjs from "@calcom/dayjs"; import dayjs from "@calcom/dayjs";
import { buildDateRanges, processDateOverride, processWorkingHours, subtract } from "./date-ranges"; import { buildDateRanges, processDateOverride, processWorkingHours, subtract } from "./date-ranges";
describe("processWorkingHours", () => { describe("processWorkingHours", () => {
// TEMPORAIRLY SKIPPING THIS TEST - Started failing after 29th Oct it("should return the correct working hours given a specific availability, timezone, and date range", () => {
it.skip("should return the correct working hours given a specific availability, timezone, and date range", () => {
const item = { const item = {
days: [1, 2, 3, 4, 5], // Monday to Friday days: [1, 2, 3, 4, 5], // Monday to Friday
startTime: new Date(Date.UTC(2023, 5, 12, 8, 0)), // 8 AM startTime: new Date(Date.UTC(2023, 5, 12, 8, 0)), // 8 AM
@ -20,15 +20,21 @@ describe("processWorkingHours", () => {
const results = processWorkingHours({ item, timeZone, dateFrom, dateTo }); const results = processWorkingHours({ item, timeZone, dateFrom, dateTo });
expect(results.length).toBe(2); // There should be two working days between the range expect(results.length).toBe(2); // There should be two working days between the range
// "America/New_York" day shifts -1, so we need to add a day to correct this shift.
expect(results[0]).toEqual({ const computeTimes = (date: Dayjs) => {
start: dayjs(`${dateFrom.tz(timeZone).add(1, "day").format("YYYY-MM-DD")}T12:00:00Z`).tz(timeZone), const baseTime = dayjs.tz(`${date.format("YYYY-MM-DD")}T00:00:00Z`, timeZone);
end: dayjs(`${dateFrom.tz(timeZone).add(1, "day").format("YYYY-MM-DD")}T21:00:00Z`).tz(timeZone), const offset = -baseTime.utcOffset() / 60;
}); return {
expect(results[1]).toEqual({ start: date.add(8 + offset, "hours").tz(timeZone),
start: dayjs(`${dateTo.tz(timeZone).format("YYYY-MM-DD")}T12:00:00Z`).tz(timeZone), end: date.add(17 + offset, "hours").tz(timeZone),
end: dayjs(`${dateTo.tz(timeZone).format("YYYY-MM-DD")}T21:00:00Z`).tz(timeZone), };
}); };
const dateFromTimes = computeTimes(dateFrom);
const dateToTimes = computeTimes(dateTo.startOf("day"));
expect(results[0]).toEqual(dateFromTimes);
expect(results[1]).toEqual(dateToTimes);
}); });
it("should have availability on last day of month in the month were DST starts", () => { it("should have availability on last day of month in the month were DST starts", () => {
const item = { const item = {
@ -48,33 +54,34 @@ describe("processWorkingHours", () => {
expect(lastAvailableSlot.start.date()).toBe(31); expect(lastAvailableSlot.start.date()).toBe(31);
}); });
// TEMPORAIRLY SKIPPING THIS TEST - Started failing after 29th Oct it("should return the correct working hours in the month were DST ends", () => {
it.skip("should return the correct working hours in the month were DST ends", () => {
const item = { const item = {
days: [0, 1, 2, 3, 4, 5, 6], // Monday to Sunday days: [0, 1, 2, 3, 4, 5, 6], // Monday to Sunday
startTime: new Date(Date.UTC(2023, 5, 12, 8, 0)), // 8 AM startTime: new Date(2023, 5, 12, 8, 0), // 8 AM
endTime: new Date(Date.UTC(2023, 5, 12, 17, 0)), // 5 PM endTime: new Date(2023, 5, 12, 17, 0), // 5 PM
}; };
console.log(item);
// in America/New_York DST ends on first Sunday of November // in America/New_York DST ends on first Sunday of November
const timeZone = "America/New_York"; const timeZone = "America/New_York";
let firstSundayOfNovember = dayjs().startOf("day").month(10).date(1); let firstSundayOfNovember = dayjs.utc().startOf("day").month(10).date(1);
while (firstSundayOfNovember.day() !== 0) { while (firstSundayOfNovember.day() !== 0) {
firstSundayOfNovember = firstSundayOfNovember.add(1, "day"); firstSundayOfNovember = firstSundayOfNovember.add(1, "day");
} }
const dateFrom = dayjs().month(10).date(1).startOf("day"); const dateFrom = dayjs.utc().month(10).date(1).startOf("day");
const dateTo = dayjs().month(10).endOf("month"); const dateTo = dayjs.utc().month(10).endOf("month");
const results = processWorkingHours({ item, timeZone, dateFrom, dateTo }); const results = processWorkingHours({ item, timeZone, dateFrom, dateTo });
const allDSTStartAt12 = results const allDSTStartAt12 = results
.filter((res) => res.start.isBefore(firstSundayOfNovember)) .filter((res) => res.start.isBefore(firstSundayOfNovember))
.every((result) => result.start.utc().hour() === 12); .every((result) => result.start.utc().hour() === 11);
const allNotDSTStartAt13 = results const allNotDSTStartAt13 = results
.filter((res) => res.start.isAfter(firstSundayOfNovember)) .filter((res) => res.start.isAfter(firstSundayOfNovember))
.every((result) => result.start.utc().hour() === 13); .every((result) => result.start.utc().hour() === 12);
expect(allDSTStartAt12).toBeTruthy(); expect(allDSTStartAt12).toBeTruthy();
expect(allNotDSTStartAt13).toBeTruthy(); expect(allNotDSTStartAt13).toBeTruthy();