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 <alannnc@gmail.com>
This commit is contained in:
Keith Williams 2023-06-23 00:19:50 +02:00 committed by GitHub
parent 7413ba6aa0
commit cfa6a5b996
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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