cal.pub0.org/apps/web/test/lib/getAvailabilityFromSchedule...

73 lines
1.9 KiB
TypeScript
Raw Normal View History

import type { Availability } from "@prisma/client";
import { expect, it, beforeAll, vi } from "vitest";
2022-06-28 20:40:58 +00:00
import dayjs from "@calcom/dayjs";
import { getAvailabilityFromSchedule } from "@calcom/lib/availability";
beforeAll(() => {
vi.setSystemTime(new Date("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);
});