cal.pub0.org/packages/app-store/closecom/test/lib/CalendarService.test.ts

276 lines
7.0 KiB
TypeScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-explicit-any */
import { expect, vi, afterEach, test } from "vitest";
import CloseCom from "@calcom/lib/CloseCom";
import {
getCloseComContactIds,
getCustomActivityTypeInstanceData,
getCloseComCustomActivityTypeFieldsIds,
getCloseComLeadId,
} from "@calcom/lib/CloseComeUtils";
import type { CalendarEvent } from "@calcom/types/Calendar";
vi.mock("@calcom/lib/CloseCom", () => ({
default: class {
constructor() {
/* Mock */
}
},
}));
afterEach(() => {
vi.resetAllMocks();
});
2022-08-26 23:21:12 +00:00
// getCloseComLeadId
test("check generic lead generator: already exists", async () => {
CloseCom.prototype.lead = {
list: () => ({
data: [{ name: "From Cal.com", id: "abc" }],
}),
} as any;
const closeCom = new CloseCom("someKey");
const id = await getCloseComLeadId(closeCom);
expect(id).toEqual("abc");
});
2022-08-26 23:21:12 +00:00
// getCloseComLeadId
test("check generic lead generator: doesn't exist", async () => {
CloseCom.prototype.lead = {
list: () => ({
data: [],
}),
create: () => ({ id: "def" }),
} as any;
const closeCom = new CloseCom("someKey");
const id = await getCloseComLeadId(closeCom);
expect(id).toEqual("def");
});
// getCloseComContactIds
test("retrieve contact IDs: all exist", async () => {
const attendees = [
{ email: "test1@example.com", id: "test1" },
{ email: "test2@example.com", id: "test2" },
];
const event = {
attendees,
} as { attendees: { email: string; name: string | null; id: string }[] };
CloseCom.prototype.contact = {
search: () => ({ data: attendees }),
} as any;
const closeCom = new CloseCom("someKey");
const contactIds = await getCloseComContactIds(event.attendees, closeCom, "leadId");
expect(contactIds).toEqual(["test1", "test2"]);
});
// getCloseComContactIds
test("retrieve contact IDs: some don't exist", async () => {
const attendees = [{ email: "test1@example.com", id: "test1" }, { email: "test2@example.com" }];
const event = {
attendees,
} as CalendarEvent;
CloseCom.prototype.contact = {
search: () => ({ data: [{ emails: [{ email: "test1@example.com" }], id: "test1" }] }),
create: () => ({ id: "test3" }),
} as any;
const closeCom = new CloseCom("someKey");
const contactIds = await getCloseComContactIds(event.attendees, closeCom, "leadId");
expect(contactIds).toEqual(["test1", "test3"]);
});
// getCloseComCustomActivityTypeFieldsIds
test("retrieve custom fields for custom activity type: type doesn't exist, no field created", async () => {
CloseCom.prototype.activity = {
type: {
get: () => [],
},
} as any;
CloseCom.prototype.customActivity = {
type: {
get: () => ({ data: [] }),
create: () => ({ id: "type1" }),
},
} as any;
CloseCom.prototype.customField = {
activity: {
create: (data: { name: string }) => ({ id: `field${data.name.length}${data.name[0]}` }),
},
} as any;
const closeCom = new CloseCom("someKey");
const contactIds = await getCloseComCustomActivityTypeFieldsIds(
[
["Attendees", "", true, true],
["Date & Time", "", true, true],
["Time Zone", "", true, true],
],
closeCom
);
expect(contactIds).toEqual({
activityType: "type1",
fields: ["field9A", "field11D", "field9T"],
});
});
// getCloseComCustomActivityTypeFieldsIds
test("retrieve custom fields for custom activity type: type exists, no field created", async () => {
CloseCom.prototype.activity = {
type: {
get: () => [],
},
} as any;
CloseCom.prototype.customActivity = {
type: {
get: () => ({ data: [{ id: "typeX", name: "Cal.com Activity" }] }),
},
} as any;
CloseCom.prototype.customField = {
activity: {
get: () => ({ data: [{ id: "fieldY", custom_activity_type_id: "typeX", name: "Attendees" }] }),
create: (data: { name: string }) => ({ id: `field${data.name.length}${data.name[0]}` }),
},
} as any;
const closeCom = new CloseCom("someKey");
const contactIds = await getCloseComCustomActivityTypeFieldsIds(
[
["Attendees", "", true, true],
["Date & Time", "", true, true],
["Time Zone", "", true, true],
],
closeCom
);
expect(contactIds).toEqual({
activityType: "typeX",
fields: ["fieldY", "field11D", "field9T"],
});
});
// getCustomActivityTypeInstanceData
test("prepare data to create custom activity type instance: two attendees, no additional notes", async () => {
const attendees = [
{ email: "test1@example.com", id: "test1", timeZone: "America/Montevideo" },
{ email: "test2@example.com" },
];
const now = new Date();
const event = {
attendees,
startTime: now.toISOString(),
Bringing back sendgrid app to review (#5501) * Sendgrid app and code simplification * Applying app-store-cli + impl * Fixing types * Adding features to readme * Fixing unit tests * A few last tweaks regarding UX and env vars * Applying feedback * Using calcom icons * Renaming and applying feedback * Testing user/type page fix * Standarizing Sendgrid client usage * Removing types * Reverting CloseCom changes * Stop relying on sendgrid client pkg * Fixing button and more reverting closecom changes * Revert "Stop relying on sendgrid client pkg" This reverts commit dd61851572a17a1e4051b133683af85c934bc2d0. * Revert "Removing types" This reverts commit 1ec5ed8de2f3139bbe84f867f229bc5759256806. * Is this it? * Standardizing apis * Fixing path * Fixing throwing errors the standard way * Stop relying on getInstalledAppPath * Removing seemingly troubling code * Returning error and avoiding any outer reference * Revert "Returning error and avoiding any outer reference" This reverts commit 7d32e30154423c95f54ebae81a76ab16a1c7bc94. * Revert "Removing seemingly troubling code" This reverts commit eaae772abcd04c8f046e4960116f42c5aaf87adf. * Revert "Stop relying on getInstalledAppPath" This reverts commit bcc70fc337bbe7fb5e74609abaeee7cd3ede90a3. * Revert "Fixing throwing errors the standard way" This reverts commit bb1bb410fac6f8c6ad14c3163a8433d125f7a885. * Revert "Fixing path" This reverts commit a7bd83c4fb7597594d0470cb530378c826b45481. * Revert "Standardizing apis" This reverts commit 0258a182298af3ebad321854ef4f34a65f4c700a. * Revert "Is this it?" This reverts commit 70b3f7b98e3003dfa225dc539e02a1e17abdd840. * Converting APIs to legacy style * Missing reverted CloseCom test mock * Needed for the renaming * Reverting Closecom and yarn unneeded changes * Ununsed type * Testing rearranging exports * Update apps/web/components/apps/OmniInstallAppButton.tsx Co-authored-by: Omar López <zomars@me.com> * Standardizing APIs * Fixing wrong toast message on app page Co-authored-by: Peer Richelsen <peeroke@gmail.com> Co-authored-by: Omar López <zomars@me.com>
2022-11-17 21:38:34 +00:00
} as unknown as CalendarEvent;
CloseCom.prototype.activity = {
type: {
get: () => [],
},
} as any;
CloseCom.prototype.customActivity = {
type: {
get: () => ({ data: [] }),
create: () => ({ id: "type1" }),
},
} as any;
CloseCom.prototype.customField = {
activity: {
create: (data: { name: string }) => ({ id: `field${data.name.length}${data.name[0]}` }),
},
} as any;
CloseCom.prototype.lead = {
list: () => ({
data: [],
}),
create: () => ({ id: "def" }),
} as any;
const closeCom = new CloseCom("someKey");
const data = await getCustomActivityTypeInstanceData(
event,
[
["Attendees", "", true, true],
["Date & Time", "", true, true],
["Time Zone", "", true, true],
],
closeCom
);
expect(data).toEqual({
custom_activity_type_id: "type1",
lead_id: "def",
"custom.field9A": ["test3"],
"custom.field11D": now.toISOString(),
"custom.field9T": "America/Montevideo",
});
});
// getCustomActivityTypeInstanceData
test("prepare data to create custom activity type instance: one attendees, with additional notes", async () => {
const attendees = [{ email: "test1@example.com", id: "test1", timeZone: "America/Montevideo" }];
const now = new Date();
const event = {
attendees,
startTime: now.toISOString(),
additionalNotes: "Some comment!",
} as any;
CloseCom.prototype.activity = {
type: {
get: () => [],
},
} as any;
CloseCom.prototype.customActivity = {
type: {
get: () => ({ data: [] }),
create: () => ({ id: "type1" }),
},
} as any;
CloseCom.prototype.customField = {
activity: {
create: (data: { name: string }) => ({ id: `field${data.name.length}${data.name[0]}` }),
},
} as any;
CloseCom.prototype.lead = {
list: () => ({
data: [{ name: "From Cal.com", id: "abc" }],
}),
} as any;
const closeCom = new CloseCom("someKey");
const data = await getCustomActivityTypeInstanceData(
event,
[
["Attendees", "", true, true],
["Date & Time", "", true, true],
["Time Zone", "", true, true],
],
closeCom
);
expect(data).toEqual({
custom_activity_type_id: "type1",
lead_id: "abc",
"custom.field9A": null,
"custom.field11D": now.toISOString(),
"custom.field9T": "America/Montevideo",
});
});