From cfa6a5b9963ef6cdc150b38d58243f4dcc9a0182 Mon Sep 17 00:00:00 2001 From: Keith Williams Date: Fri, 23 Jun 2023 00:19:50 +0200 Subject: [PATCH] chore: Added unit tests for converting availability to string (#9617) * Added unit tests for availability to string * Testing to see if extra toString() removes unicode * Replacing unicode space * Updated to regex * Reverted files that were not supposed to change * Reverted schema changes * Added comment * Type check * Change function call structure --------- Co-authored-by: alannnc --- .../web/test/lib/availabilityAsString.test.ts | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 apps/web/test/lib/availabilityAsString.test.ts diff --git a/apps/web/test/lib/availabilityAsString.test.ts b/apps/web/test/lib/availabilityAsString.test.ts new file mode 100644 index 0000000000..144a621d71 --- /dev/null +++ b/apps/web/test/lib/availabilityAsString.test.ts @@ -0,0 +1,111 @@ +import { expect, it } from "vitest"; + +import { availabilityAsString } from "@calcom/lib/availability"; + +it("correctly handles 1 day", async () => { + const availability = { + id: 1, + userId: 2, + eventTypeId: 3, + days: [1], + startTime: new Date(Date.UTC(1970, 1, 1, 9, 0, 0, 0)), + endTime: new Date(Date.UTC(1970, 1, 1, 17, 0, 0, 0)), + date: null, + scheduleId: 1 + }; + + const result = availabilityAsString(availability, { + locale: 'en', + hour12: true + }); + + expect(replaceUnicodeSpace(result)).toBe("Mon, 9:00 AM - 5:00 PM"); +}); + + +it("correctly handles all days", async () => { + const availability = { + id: 1, + userId: 2, + eventTypeId: 3, + days: [1, 2, 3, 4, 5, 6, 7], + startTime: new Date(Date.UTC(1970, 1, 1, 9, 0, 0, 0)), + endTime: new Date(Date.UTC(1970, 1, 1, 17, 0, 0, 0)), + date: null, + scheduleId: 1 + }; + + const result = availabilityAsString(availability, { + locale: 'en', + hour12: true + }); + + expect(replaceUnicodeSpace(result)).toBe("Mon - Sun, 9:00 AM - 5:00 PM"); +}); + +it("correctly handles staggered days", async () => { + const availability = { + id: 1, + userId: 2, + eventTypeId: 3, + days: [1, 3, 5, 7], + startTime: new Date(Date.UTC(1970, 1, 1, 9, 0, 0, 0)), + endTime: new Date(Date.UTC(1970, 1, 1, 17, 0, 0, 0)), + date: null, + scheduleId: 1 + }; + + const result = availabilityAsString(availability, { + locale: 'en', + hour12: true + }); + + expect(replaceUnicodeSpace(result)).toBe("Mon, Wed, Fri, Sun, 9:00 AM - 5:00 PM"); +}); + +it("correctly produces days and times - 12 hours", async () => { + const availability = { + id: 1, + userId: 2, + eventTypeId: 3, + days: [1, 2, 3], + startTime: new Date(Date.UTC(1970, 1, 1, 9, 0, 0, 0)), + endTime: new Date(Date.UTC(1970, 1, 1, 17, 0, 0, 0)), + date: null, + scheduleId: 1 + }; + + const result = availabilityAsString(availability, { + locale: 'en', + hour12: true + }); + + expect(replaceUnicodeSpace(result)).toBe("Mon - Wed, 9:00 AM - 5:00 PM"); +}); + +it("correctly produces days and times - 24 hours", async () => { + const availability = { + id: 1, + userId: 2, + eventTypeId: 3, + days: [1, 2, 3], + startTime: new Date(Date.UTC(1970, 1, 1, 9, 0, 0, 0)), + endTime: new Date(Date.UTC(1970, 1, 1, 17, 0, 0, 0)), + date: null, + scheduleId: 1 + }; + + const result = availabilityAsString(availability, { + locale: 'en', + hour12: false + }); + + expect(replaceUnicodeSpace(result)).toBe("Mon - Wed, 09:00 - 17:00"); +}); + +// INFO: This is because on GitHub, the international date formatting +// produces Unicode characters. Instead of using line for line code from the +// availability.ts file, opted for this instead. +const replaceUnicodeSpace = (string: string) => { + return string.replace(/\u202f/g, ' '); +}