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

* Fix typo in SetTimesModal

* Remove db dependency to run unit tests

* Add unit test for getAvailabilityFromSchedule
This commit is contained in:
Demian Caldelas 2022-02-23 13:16:04 -03:00 committed by GitHub
parent 3bca9687d0
commit d9d95ba17c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 4 deletions

View file

@ -59,8 +59,8 @@ export default function SetTimesModal(props: SetTimesModalProps) {
let endMinute = parseInt(endMinsRef.current.value); let endMinute = parseInt(endMinsRef.current.value);
//convert to dayjs object //convert to dayjs object
const startTime = dayjs(`${startHour}-${startMinute}`, "hh:mm"); const startTime = dayjs(`${startHour}:${startMinute}`, "hh:mm");
const endTime = dayjs(`${endHour}-${endMinute}`, "hh:mm"); const endTime = dayjs(`${endHour}:${endMinute}`, "hh:mm");
//compute minimin and maximum allowed //compute minimin and maximum allowed
const maximumStartTime = endTime.subtract(step, "minute"); const maximumStartTime = endTime.subtract(step, "minute");

View file

@ -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);
});

View file

@ -81,11 +81,11 @@
}, },
"start": {}, "start": {},
"test": { "test": {
"dependsOn": ["@calcom/web#build", "@calcom/prisma#db-reset"] "dependsOn": []
}, },
"test-e2e": { "test-e2e": {
"cache": false, "cache": false,
"dependsOn": ["^test"], "dependsOn": ["^test", "@calcom/web#build", "@calcom/prisma#db-reset"],
"outputs": ["playwright", "test-results"] "outputs": ["playwright", "test-results"]
}, },
"type-check": { "type-check": {