Code Snippet Server

by ngeojiajun
Verified
import { jest } from "@jest/globals"; import { google } from "googleapis"; import { CalendarService } from "../calendar-service"; import { GoogleAuthService } from "../google-auth-service"; // Create a test class that exposes waitForInit class TestCalendarService extends CalendarService { public async testInit(): Promise<void> { await this.waitForInit(); } } jest.mock("googleapis"); jest.mock("../google-auth-service"); describe("CalendarService", () => { let mockCalendarAPI: any; let mockAuth: jest.Mocked<GoogleAuthService>; let service: TestCalendarService; beforeEach(async () => { jest.clearAllMocks(); // Mock calendar API methods mockCalendarAPI = { events: { list: jest.fn(), get: jest.fn(), }, calendarList: { list: jest.fn(), }, }; (google.calendar as jest.Mock).mockReturnValue(mockCalendarAPI); // Mock auth service mockAuth = { initialize: jest.fn().mockImplementation(() => Promise.resolve()), authenticate: jest.fn().mockImplementation(() => Promise.resolve()), getAuth: jest.fn(), saveToken: jest.fn().mockImplementation(() => Promise.resolve()), oAuth2Client: undefined, authUrl: "", } as unknown as jest.Mocked<GoogleAuthService>; (GoogleAuthService.getInstance as jest.Mock).mockReturnValue(mockAuth); // Create service instance and wait for initialization service = new TestCalendarService(); await service.testInit(); }); describe("listEvents", () => { it("should list events successfully", async () => { const mockEvents = { data: { items: [ { id: "1", summary: "Test Event 1" }, { id: "2", summary: "Test Event 2" }, ], }, }; mockCalendarAPI.events.list.mockResolvedValue(mockEvents); const result = await service.listEvents(); expect(result).toEqual(mockEvents.data.items); expect(mockCalendarAPI.events.list).toHaveBeenCalled(); }); it("should handle empty event list", async () => { mockCalendarAPI.events.list.mockResolvedValue({ data: { items: [] } }); const result = await service.listEvents(); expect(result).toEqual([]); expect(mockCalendarAPI.events.list).toHaveBeenCalled(); }); it("should handle errors", async () => { const error = new Error("API error"); mockCalendarAPI.events.list.mockRejectedValue(error); await expect(service.listEvents()).rejects.toThrow(error); }); }); describe("getEvent", () => { it("should get event details successfully", async () => { const mockEvent = { data: { id: "1", summary: "Test Event", start: { dateTime: "2024-01-01T10:00:00Z" }, end: { dateTime: "2024-01-01T11:00:00Z" }, }, }; mockCalendarAPI.events.get.mockResolvedValue(mockEvent); const result = await service.getEvent("1"); expect(result).toEqual(mockEvent.data); expect(mockCalendarAPI.events.get).toHaveBeenCalledWith({ calendarId: "primary", eventId: "1", }); }); it("should handle errors", async () => { const error = new Error("API error"); mockCalendarAPI.events.get.mockRejectedValue(error); await expect(service.getEvent("1")).rejects.toThrow(error); }); }); describe("listCalendars", () => { it("should list calendars successfully", async () => { const mockCalendars = { data: { items: [ { id: "1", summary: "Calendar 1" }, { id: "2", summary: "Calendar 2" }, ], }, }; mockCalendarAPI.calendarList.list.mockResolvedValue(mockCalendars); const result = await service.listCalendars(); expect(result).toEqual(mockCalendars.data.items); expect(mockCalendarAPI.calendarList.list).toHaveBeenCalled(); }); it("should handle empty calendar list", async () => { mockCalendarAPI.calendarList.list.mockResolvedValue({ data: { items: [] }, }); const result = await service.listCalendars(); expect(result).toEqual([]); expect(mockCalendarAPI.calendarList.list).toHaveBeenCalled(); }); it("should handle errors", async () => { const error = new Error("API error"); mockCalendarAPI.calendarList.list.mockRejectedValue(error); await expect(service.listCalendars()).rejects.toThrow(error); }); }); });