Skip to content

Scheduling infrastructure for absolutely everyone.

Test/get availability from schedule unit test (#1951)

Demian Caldelasd9d95ba17cce
diff --git a/apps/web/components/ui/modal/SetTimesModal.tsx b/apps/web/components/ui/modal/SetTimesModal.tsx
index 07e82c02cdb..b314d8542bb 100644
--- a/apps/web/components/ui/modal/SetTimesModal.tsx
+++ b/apps/web/components/ui/modal/SetTimesModal.tsx
@@ -59,8 +59,8 @@ export default function SetTimesModal(props: SetTimesModalProps) {
let endMinute = parseInt(endMinsRef.current.value);
//convert to dayjs object
- const startTime = dayjs(`${startHour}-${startMinute}`, "hh:mm");
- const endTime = dayjs(`${endHour}-${endMinute}`, "hh:mm");
+ const startTime = dayjs(`${startHour}:${startMinute}`, "hh:mm");
+ const endTime = dayjs(`${endHour}:${endMinute}`, "hh:mm");
//compute minimin and maximum allowed
const maximumStartTime = endTime.subtract(step, "minute");
diff --git a/apps/web/test/lib/getAvailabilityFromSchedule.test.ts b/apps/web/test/lib/getAvailabilityFromSchedule.test.ts
new file mode 100644
index 00000000000..d6e2861b479
--- /dev/null
+++ b/apps/web/test/lib/getAvailabilityFromSchedule.test.ts
@@ -0,0 +1,73 @@
+import { expect, it } from "@jest/globals";
+import dayjs from "dayjs";
+import customParseFormat from "dayjs/plugin/customParseFormat";
+import MockDate from "mockdate";
+
+import { Availability } from "@calcom/prisma/client";
+
+import { getAvailabilityFromSchedule } from "@lib/availability";
+
+dayjs.extend(customParseFormat);
+MockDate.set("2021-06-20T11:59:59Z");
+
+//parse "hh:mm-hh:mm" into <Availability> object
+const parseWorkingHours = (workingHours: string) => {
+ const times = workingHours.split("-").map((time) => dayjs(time, "hh:mm").toDate());
+ return { start: times[0], end: times[1] };
+};
+const p = parseWorkingHours;
+
+// mocked working hours
+const fulltimeWH = p("09:00-17:00");
+const morningWH = p("09:00-12:00");
+const afternoonWH = p("13:00-17:00");
+
+it("should return an empty availability array when received an empty schedule", async () => {
+ const schedule = [[]];
+ expect(getAvailabilityFromSchedule(schedule)).toStrictEqual([]);
+});
+
+it("should return availability for all workable days from 9:00 to 17:00", async () => {
+ const schedule = [[], [fulltimeWH], [fulltimeWH], [fulltimeWH], [fulltimeWH], [fulltimeWH], []];
+ const expected = [
+ {
+ days: [1, 2, 3, 4, 5],
+ startTime: fulltimeWH.start,
+ endTime: fulltimeWH.end,
+ },
+ ] as Availability[];
+
+ expect(getAvailabilityFromSchedule(schedule)).toStrictEqual(expected);
+});
+
+it("should return the available days grouped by the available time slots", async () => {
+ const schedule = [
+ [],
+ [afternoonWH],
+ [afternoonWH],
+ [morningWH, afternoonWH],
+ [fulltimeWH],
+ [morningWH],
+ [],
+ ];
+ const expected = [
+ {
+ days: [1, 2, 3],
+ startTime: afternoonWH.start,
+ endTime: afternoonWH.end,
+ },
+ {
+ days: [3, 5],
+ startTime: morningWH.start,
+ endTime: morningWH.end,
+ },
+
+ {
+ days: [4],
+ startTime: fulltimeWH.start,
+ endTime: fulltimeWH.end,
+ },
+ ] as Availability[];
+
+ expect(getAvailabilityFromSchedule(schedule)).toStrictEqual(expected);
+});
diff --git a/turbo.json b/turbo.json
index 7b1121aa195..c21bf24b9a8 100644
--- a/turbo.json
+++ b/turbo.json
@@ -81,11 +81,11 @@
},
"start": {},
"test": {
- "dependsOn": ["@calcom/web#build", "@calcom/prisma#db-reset"]
+ "dependsOn": []
},
"test-e2e": {
"cache": false,
- "dependsOn": ["^test"],
+ "dependsOn": ["^test", "@calcom/web#build", "@calcom/prisma#db-reset"],
"outputs": ["playwright", "test-results"]
},
"type-check": {