calcom/test/lib/getWorkingHours.test.ts
Alex van Andel ffdf0b9217
Fixes user availability to be contextual to the user timezone (#1166)
* WIP, WIP, WIP, WIP

* Adds missing types

* Type fixes for useSlots

* Type fixes

* Fixes periodType 500 error when updating

* Adds missing dayjs plugin and type fixes

* An attempt was made to fix tests

* Save work in progress

* Added UTC overflow to days

* Update lib/availability.ts

Co-authored-by: Alex Johansson <alexander@n1s.se>

* No more magic numbers

* Fixed slots.test & added getWorkingHours.test

* Tests pass, simpler logic, profit?

* Timezone shifting!

* Forgot to unskip tests

* Updated the user page

* Added American seed user, some fixes

* tmp fix so to continue testing availability

* Removed timeZone parameter, fix defaultValue auto-scroll

Co-authored-by: Omar López <zomars@me.com>
Co-authored-by: Alex Johansson <alexander@n1s.se>
2021-11-18 01:03:19 +00:00

159 lines
4.2 KiB
TypeScript

import { expect, it } from "@jest/globals";
import dayjs from "dayjs";
import timezone from "dayjs/plugin/timezone";
import utc from "dayjs/plugin/utc";
import MockDate from "mockdate";
import { getWorkingHours } from "@lib/availability";
dayjs.extend(utc);
dayjs.extend(timezone);
MockDate.set("2021-06-20T11:59:59Z");
it("correctly translates Availability (UTC+0) to UTC workingHours", async () => {
expect(
getWorkingHours({ timeZone: "GMT" }, [
{
days: [0],
startTime: new Date(Date.UTC(2021, 11, 16, 23)),
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
},
])
).toStrictEqual([
{
days: [0],
endTime: 1439,
startTime: 1380,
},
]);
});
it("correctly translates Availability in a positive UTC offset (Pacific/Auckland) to UTC workingHours", async () => {
// Take note that (Pacific/Auckland) is UTC+12 on 2021-06-20, NOT +13 like the other half of the year.
expect(
getWorkingHours({ timeZone: "Pacific/Auckland" }, [
{
days: [1],
startTime: new Date(Date.UTC(2021, 11, 16, 0)),
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
},
])
).toStrictEqual([
{
days: [1],
endTime: 719,
startTime: 0,
},
{
days: [0],
endTime: 1439,
startTime: 720, // 0 (midnight) - 12 * 60 (DST)
},
]);
});
it("correctly translates Availability in a negative UTC offset (Pacific/Midway) to UTC workingHours", async () => {
// Take note that (Pacific/Midway) is UTC-12 on 2021-06-20, NOT +13 like the other half of the year.
expect(
getWorkingHours({ timeZone: "Pacific/Midway" }, [
{
days: [1],
startTime: new Date(Date.UTC(2021, 11, 16, 0)),
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
},
])
).toStrictEqual([
{
days: [2],
endTime: 659,
startTime: 0,
},
{
days: [1],
endTime: 1439,
startTime: 660,
},
]);
});
it("can do the same with UTC offsets", async () => {
// Take note that (Pacific/Midway) is UTC-12 on 2021-06-20, NOT +13 like the other half of the year.
expect(
getWorkingHours({ utcOffset: dayjs().tz("Pacific/Midway").utcOffset() }, [
{
days: [1],
startTime: new Date(Date.UTC(2021, 11, 16, 0)),
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
},
])
).toStrictEqual([
{
days: [2],
endTime: 659,
startTime: 0,
},
{
days: [1],
endTime: 1439,
startTime: 660,
},
]);
});
it("can also shift UTC into other timeZones", async () => {
// UTC+0 time with 23:00 - 23:59 (Sunday) and 00:00 - 16:00 (Monday) when cast into UTC+1 should become 00:00 = 17:00 (Monday)
expect(
getWorkingHours({ utcOffset: -60 }, [
{
days: [0],
startTime: new Date(Date.UTC(2021, 11, 16, 23)),
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
},
{
days: [1],
startTime: new Date(Date.UTC(2021, 11, 17, 0)),
endTime: new Date(Date.UTC(2021, 11, 17, 16)),
},
])
).toStrictEqual([
// TODO: Maybe the desired result is 0-1020 as a single entry, but this requires some post-processing to merge. It may work as is so leaving this as now.
{
days: [1],
endTime: 59,
startTime: 0,
},
{
days: [1],
endTime: 1020,
startTime: 60,
},
]);
// And the other way around; UTC+0 time with 00:00 - 1:00 (Monday) and 21:00 - 24:00 (Sunday) when cast into UTC-1 should become 20:00 = 24:00 (Sunday)
expect(
getWorkingHours({ utcOffset: 60 }, [
{
days: [0],
startTime: new Date(Date.UTC(2021, 11, 16, 21)),
endTime: new Date(Date.UTC(2021, 11, 16, 23, 59)),
},
{
days: [1],
startTime: new Date(Date.UTC(2021, 11, 17, 0)),
endTime: new Date(Date.UTC(2021, 11, 17, 1)),
},
])
).toStrictEqual([
// TODO: Maybe the desired result is 1200-1439 as a single entry, but this requires some post-processing to merge. It may work as is so leaving this as now.
{
days: [0],
endTime: 1379,
startTime: 1200,
},
{
days: [0],
endTime: 1439,
startTime: 1380,
},
]);
});