Files
cal.diy/packages/features/host/repositories/HostRepository.test.ts
T
Benny Joo d81993892a refactor: create HostRepository in /features/host (#25842)
* mv test

* mv host repository

* update imports
2025-12-13 11:04:57 +00:00

77 lines
2.1 KiB
TypeScript

import { describe, expect, it, vi, beforeEach } from "vitest";
import { PrismaClient } from "@calcom/prisma";
import { HostRepository } from "./HostRepository";
vi.mock("@calcom/prisma", () => {
const mockPrisma = {
host: {
updateMany: vi.fn(),
findMany: vi.fn(),
},
};
return {
__esModule: true,
default: mockPrisma,
PrismaClient: vi.fn(() => mockPrisma),
};
});
describe("HostRepository", () => {
let prisma: PrismaClient;
let hostRepo: HostRepository;
beforeEach(() => {
prisma = new PrismaClient();
hostRepo = new HostRepository(prisma);
vi.clearAllMocks();
});
describe("updateHostsWithNewDefaultSchedule", () => {
it("should update hosts with new scheduleId", async () => {
const userId = 1;
const oldScheduleId = 123;
const newScheduleId = 456;
const updateResult = { count: 2 }; // 2 hosts updated
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(prisma.host.updateMany as any).mockResolvedValue(updateResult);
const result = await hostRepo.updateHostsSchedule(userId, oldScheduleId, newScheduleId);
expect(prisma.host.updateMany).toHaveBeenCalledWith({
where: {
userId,
scheduleId: oldScheduleId,
},
data: {
scheduleId: newScheduleId,
},
});
expect(result).toEqual(updateResult);
});
it("should handle case with no hosts to update", async () => {
const userId = 1;
const oldScheduleId = 123;
const newScheduleId = 456;
const updateResult = { count: 0 }; // No hosts updated
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(prisma.host.updateMany as any).mockResolvedValue(updateResult);
const result = await hostRepo.updateHostsSchedule(userId, oldScheduleId, newScheduleId);
expect(prisma.host.updateMany).toHaveBeenCalledWith({
where: {
userId,
scheduleId: oldScheduleId,
},
data: {
scheduleId: newScheduleId,
},
});
expect(result).toEqual(updateResult);
});
});
});