Files
cal.diy/packages/app-store/salesforce/lib/CrmService.test.ts
T

266 lines
7.3 KiB
TypeScript

import { setupAndTeardown } from "@calcom/web/test/utils/bookingScenario/setupAndTeardown";
import { describe, expect, it, beforeEach, vi } from "vitest";
import type { CredentialPayload } from "@calcom/types/Credential";
import SalesforceCRMService from "./CrmService";
import { SalesforceRecordEnum } from "./enums";
describe("SalesforceCRMService", () => {
let service: SalesforceCRMService;
let mockConnection: { query: any };
setupAndTeardown();
beforeEach(() => {
mockConnection = {
query: vi.fn(),
};
const mockCredential: CredentialPayload = {
id: 1,
type: "salesforce_crm",
key: {
access_token: "mock_token",
refresh_token: "mock_refresh",
instance_url: "https://mock.salesforce.com",
},
userId: 1,
appId: "salesforce",
teamId: null,
invalid: false,
user: {
email: "[email protected]",
},
};
service = new SalesforceCRMService(mockCredential, {}, true);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
service.conn = Promise.resolve(mockConnection);
});
describe("getContacts", () => {
it("should return empty array when no records found", async () => {
// Setup mock response
mockConnection.query.mockResolvedValueOnce({ records: [] });
const result = await service.getContacts({ emails: "[email protected]" });
expect(result).toEqual([]);
expect(mockConnection.query).toHaveBeenCalledWith(
"SELECT Id, Email, OwnerId FROM Contact WHERE Email IN ('[email protected]')"
);
});
it("should handle single email string input", async () => {
// Setup mock response with descriptive name
const contactQueryResponse = {
records: [
{
Id: "001",
Email: "[email protected]",
OwnerId: "owner001",
attributes: { type: "Contact" },
},
],
};
// Setup query spy
const querySpy = vi.spyOn(mockConnection, "query");
querySpy.mockResolvedValueOnce(contactQueryResponse);
const result = await service.getContacts({ emails: "[email protected]" });
expect(result).toEqual([
{
id: "001",
email: "[email protected]",
recordType: "Contact",
},
]);
// Verify the exact query made
expect(querySpy).toHaveBeenCalledWith(
"SELECT Id, Email, OwnerId FROM Contact WHERE Email IN ('[email protected]')"
);
});
it("should handle array of emails input", async () => {
const contactsQueryResponse = {
records: [
{
Id: "001",
Email: "[email protected]",
OwnerId: "owner001",
attributes: { type: "Contact" },
},
{
Id: "002",
Email: "[email protected]",
OwnerId: "owner002",
attributes: { type: "Contact" },
},
],
};
const querySpy = vi.spyOn(mockConnection, "query");
querySpy.mockResolvedValueOnce(contactsQueryResponse);
const result = await service.getContacts({
emails: ["[email protected]", "[email protected]"],
});
expect(result).toEqual([
{
id: "001",
email: "[email protected]",
recordType: "Contact",
},
{
id: "002",
email: "[email protected]",
recordType: "Contact",
},
]);
expect(querySpy).toHaveBeenCalledWith(
"SELECT Id, Email, OwnerId FROM Contact WHERE Email IN ('[email protected]','[email protected]')"
);
});
it("should include owner information when includeOwner is true", async () => {
const contactQueryResponse = {
records: [
{
Id: "001",
Email: "[email protected]",
OwnerId: "owner001",
attributes: { type: "Contact" },
},
],
};
const ownerQueryResponse = {
records: [
{
Id: "owner001",
Email: "[email protected]",
Name: "Test Owner",
},
],
};
// Setup query spy with specific responses
const querySpy = vi.spyOn(mockConnection, "query");
querySpy
.mockResolvedValueOnce(contactQueryResponse) // Contact query
.mockResolvedValueOnce(ownerQueryResponse); // Owner query
const result = await service.getContacts({
emails: "[email protected]",
includeOwner: true,
});
expect(result).toEqual([
{
id: "001",
email: "[email protected]",
ownerId: "owner001",
ownerEmail: "[email protected]",
recordType: "Contact",
},
]);
expect(querySpy).toHaveBeenNthCalledWith(
1,
"SELECT Id, Email, OwnerId FROM Contact WHERE Email IN ('[email protected]')"
);
expect(querySpy).toHaveBeenNthCalledWith(
2,
expect.stringContaining("SELECT Id, Email, Name FROM User WHERE Id = 'owner001'")
);
});
it("should handle account record type with round robin skip", async () => {
service = new SalesforceCRMService(
{} as CredentialPayload,
{
createEventOn: SalesforceRecordEnum.ACCOUNT,
roundRobinSkipCheckRecordOn: SalesforceRecordEnum.ACCOUNT,
},
true
);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
service.conn = Promise.resolve(mockConnection);
const initialContactQueryResponse = {
records: [
{
Id: "001",
Email: "[email protected]",
AccountId: "acc001",
OwnerId: "owner001",
attributes: { type: "Contact" },
},
],
};
const accountQueryResponse = {
records: [
{
Id: "acc001",
OwnerId: "owner001",
Email: "[email protected]",
attributes: { type: "Account" },
},
],
};
const ownerQueryResponse = {
records: [
{
Id: "owner001",
Email: "[email protected]",
Name: "Test Owner",
},
],
};
const querySpy = vi.spyOn(mockConnection, "query");
querySpy
.mockResolvedValueOnce(initialContactQueryResponse)
.mockResolvedValueOnce(accountQueryResponse)
.mockResolvedValueOnce(ownerQueryResponse);
const result = await service.getContacts({
emails: "[email protected]",
forRoundRobinSkip: true,
includeOwner: true,
});
expect(result).toEqual([
{
id: "acc001",
email: "[email protected]",
ownerId: "owner001",
ownerEmail: "[email protected]",
recordType: "Account",
},
]);
expect(querySpy).toHaveBeenNthCalledWith(
1,
"SELECT Id, Email, OwnerId, AccountId FROM Contact WHERE Email = '[email protected]' AND AccountId != null"
);
expect(querySpy).toHaveBeenNthCalledWith(2, "SELECT Id, OwnerId FROM Account WHERE Id = 'acc001'");
expect(querySpy).toHaveBeenNthCalledWith(
3,
expect.stringContaining("SELECT Id, Email, Name FROM User WHERE Id = 'owner001'")
);
});
});
});