test: add unit tests for getWhereForTeamOrAllTeams function (#21152)

* test: add unit tests for getWhereForTeamOrAllTeams function

Co-Authored-By: [email protected] <[email protected]>

* test: remove implementation details and add tests for invalid parameter combinations

Co-Authored-By: [email protected] <[email protected]>

* test: update mock values to use more distinct numbers

Co-Authored-By: [email protected] <[email protected]>

* test: update mock values to use completely distinct numbers

Co-Authored-By: [email protected] <[email protected]>

* test: update mock values to maintain data integrity

Co-Authored-By: [email protected] <[email protected]>

* test: convert to integration tests for getWhereForTeamOrAllTeams function

Co-Authored-By: [email protected] <[email protected]>

* chore: remove integration test file

Co-Authored-By: [email protected] <[email protected]>

* test: reorganize tests with proper describe blocks

Co-Authored-By: [email protected] <[email protected]>

* test: move test file to __tests__ directory

Co-Authored-By: [email protected] <[email protected]>

* test: update import path for routing-events in test file

Co-Authored-By: [email protected] <[email protected]>

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: [email protected] <[email protected]>
Co-authored-by: Eunjae Lee <[email protected]>
This commit is contained in:
devin-ai-integration[bot]
2025-05-16 08:19:17 +00:00
committed by GitHub
co-authored by Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> [email protected] <[email protected]> Eunjae Lee
parent 8889e8566c
commit a5b684af1f
@@ -0,0 +1,212 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
import { readonlyPrisma } from "@calcom/prisma";
import { RoutingEventsInsights } from "../routing-events";
vi.mock("@calcom/prisma", () => ({
readonlyPrisma: {
team: {
findMany: vi.fn(),
},
membership: {
findMany: vi.fn(),
},
},
}));
describe("RoutingEventsInsights", () => {
class TestRoutingEventsInsights extends RoutingEventsInsights {
static async testGetWhereForTeamOrAllTeams(params: {
userId?: number | null;
teamId?: number | null;
isAll: boolean;
organizationId?: number | null;
routingFormId?: string | null;
}) {
return super.getWhereForTeamOrAllTeams(params);
}
}
beforeEach(() => {
vi.resetAllMocks();
});
describe("getWhereForTeamOrAllTeams", () => {
describe("Basic filtering scenarios", () => {
it("should return correct where condition when isAll is true with organizationId", async () => {
vi.mocked(readonlyPrisma.team.findMany).mockResolvedValue([{ id: 1 }, { id: 2 }]);
vi.mocked(readonlyPrisma.membership.findMany).mockResolvedValue([{ teamId: 1 }, { teamId: 2 }]);
const result = await TestRoutingEventsInsights.testGetWhereForTeamOrAllTeams({
userId: 1,
isAll: true,
organizationId: 1,
});
expect(result).toEqual({
teamId: {
in: [1, 2],
},
});
});
it("should return correct where condition when teamId is provided", async () => {
vi.mocked(readonlyPrisma.membership.findMany).mockResolvedValue([{ teamId: 5 }]);
const result = await TestRoutingEventsInsights.testGetWhereForTeamOrAllTeams({
userId: 1,
teamId: 5,
isAll: false,
});
expect(result).toEqual({
teamId: {
in: [5],
},
});
});
it("should return correct where condition when neither teamId nor organizationId is provided", async () => {
const result = await TestRoutingEventsInsights.testGetWhereForTeamOrAllTeams({
userId: 1,
isAll: false,
});
expect(result).toEqual({
userId: 1,
teamId: null,
});
});
});
describe("Edge cases", () => {
it("should return correct where condition when user has no access to teams", async () => {
vi.mocked(readonlyPrisma.team.findMany).mockResolvedValue([{ id: 2 }, { id: 3 }]);
vi.mocked(readonlyPrisma.membership.findMany).mockResolvedValue([]);
const result = await TestRoutingEventsInsights.testGetWhereForTeamOrAllTeams({
userId: 1,
isAll: true,
organizationId: 1,
});
expect(result).toEqual({
userId: 1,
teamId: null,
});
});
it("should return correct where condition when routingFormId is provided", async () => {
vi.mocked(readonlyPrisma.membership.findMany).mockResolvedValue([{ teamId: 5 }]);
const result = await TestRoutingEventsInsights.testGetWhereForTeamOrAllTeams({
userId: 1,
teamId: 5,
isAll: false,
routingFormId: "form-123",
});
expect(result).toEqual({
teamId: {
in: [5],
},
id: "form-123",
});
});
it("should handle null userId by using default value", async () => {
vi.mocked(readonlyPrisma.membership.findMany).mockResolvedValue([{ teamId: 5 }]);
const result = await TestRoutingEventsInsights.testGetWhereForTeamOrAllTeams({
userId: null,
teamId: 5,
isAll: false,
});
expect(result).toEqual({
teamId: {
in: [5],
},
});
});
it("should handle empty parameters with only isAll provided", async () => {
const result = await TestRoutingEventsInsights.testGetWhereForTeamOrAllTeams({
isAll: false,
});
expect(result).toEqual({
userId: -1,
teamId: null,
});
});
});
describe("Invalid parameter combinations", () => {
it("should handle when isAll is true but no organizationId is provided", async () => {
const result = await TestRoutingEventsInsights.testGetWhereForTeamOrAllTeams({
userId: 1,
isAll: true,
});
expect(result).toEqual({
userId: 1,
teamId: null,
});
});
it("should handle when both teamId and organizationId are provided", async () => {
vi.mocked(readonlyPrisma.membership.findMany).mockResolvedValue([{ teamId: 5 }]);
const result = await TestRoutingEventsInsights.testGetWhereForTeamOrAllTeams({
userId: 1,
teamId: 5,
isAll: false,
organizationId: 10,
});
expect(result).toEqual({
teamId: {
in: [5],
},
});
});
it("should handle when both teamId and organizationId are provided with isAll true", async () => {
vi.mocked(readonlyPrisma.team.findMany).mockResolvedValue([{ id: 1 }, { id: 2 }]);
vi.mocked(readonlyPrisma.membership.findMany).mockResolvedValue([{ teamId: 1 }, { teamId: 2 }]);
const result = await TestRoutingEventsInsights.testGetWhereForTeamOrAllTeams({
userId: 1,
teamId: 5,
isAll: true,
organizationId: 1,
});
expect(result).toEqual({
teamId: {
in: [1, 2],
},
});
});
it("should handle undefined teamId with isAll true", async () => {
vi.mocked(readonlyPrisma.team.findMany).mockResolvedValue([]);
vi.mocked(readonlyPrisma.membership.findMany).mockResolvedValue([]);
const result = await TestRoutingEventsInsights.testGetWhereForTeamOrAllTeams({
userId: 1,
teamId: undefined,
isAll: true,
organizationId: 1,
});
expect(result).toEqual({
userId: 1,
teamId: null,
});
});
});
});
});