[#64] added tests and fixed broken ones

This commit is contained in:
Camille Moussu
2025-09-11 17:29:39 +02:00
committed by Benoit TELLIER
parent 9d43c15ee0
commit 06984ded1a
3 changed files with 68 additions and 54 deletions
@@ -3,6 +3,7 @@
import {
getCalendar,
getCalendars,
postCalendar,
} from "../../../src/features/Calendars/CalendarApi";
import { clientConfig } from "../../../src/features/User/oidcAuth";
import { api } from "../../../src/utils/apiUtils";
@@ -15,48 +16,65 @@ describe("Calendar API", () => {
jest.clearAllMocks();
});
describe("getCalendars", () => {
it("fetches calendar list for a user", async () => {
const mockUserId = "user123";
const mockResponse = [{ id: "calendar1" }, { id: "calendar2" }];
it("fetches calendar list for a user", async () => {
const mockUserId = "user123";
const mockResponse = [{ id: "calendar1" }, { id: "calendar2" }];
(api.get as jest.Mock).mockReturnValue({
json: jest.fn().mockResolvedValue(mockResponse),
});
const calendars = await getCalendars(mockUserId);
expect(api.get).toHaveBeenCalledWith(
`dav/calendars/${mockUserId}.json?personal=true&sharedDelegationStatus=accepted&sharedPublicSubscription=true&withRights=true`,
{
headers: { Accept: "application/calendar+json" },
}
);
expect(calendars).toEqual(mockResponse);
(api.get as jest.Mock).mockReturnValue({
json: jest.fn().mockResolvedValue(mockResponse),
});
const calendars = await getCalendars(mockUserId);
expect(api.get).toHaveBeenCalledWith(
`dav/calendars/${mockUserId}.json?personal=true&sharedDelegationStatus=accepted&sharedPublicSubscription=true&withRights=true`,
{
headers: { Accept: "application/calendar+json" },
}
);
expect(calendars).toEqual(mockResponse);
});
describe("getCalendar", () => {
it("fetches calendar events for a given ID and match window", async () => {
const calendarId = "calendar1";
const match = { start: "2025-07-01", end: "2025-07-31" };
const mockCalendarData = { events: ["event1", "event2"] };
it("fetches calendar events for a given ID and match window", async () => {
const calendarId = "calendar1";
const match = { start: "2025-07-01", end: "2025-07-31" };
const mockCalendarData = { events: ["event1", "event2"] };
(api as unknown as jest.Mock).mockReturnValue({
json: jest.fn().mockResolvedValue(mockCalendarData),
});
(api as unknown as jest.Mock).mockReturnValue({
json: jest.fn().mockResolvedValue(mockCalendarData),
});
const result = await getCalendar(calendarId, match);
const result = await getCalendar(calendarId, match);
expect(api).toHaveBeenCalledWith(`dav/calendars/${calendarId}.json`, {
method: "REPORT",
headers: {
Accept: "application/json, text/plain, */*",
},
body: JSON.stringify({ match }),
});
expect(api).toHaveBeenCalledWith(`dav/calendars/${calendarId}.json`, {
method: "REPORT",
headers: {
Accept: "application/json, text/plain, */*",
},
body: JSON.stringify({ match }),
});
expect(result).toEqual(mockCalendarData);
expect(result).toEqual(mockCalendarData);
});
it("postCalendar", async () => {
const calId = "calId";
const userId = "userId";
const color = "calId";
const name = "new cal";
const desc = "desc";
const result = await postCalendar(userId, calId, color, name, desc);
expect(api.post).toHaveBeenCalledWith(`dav/calendars/${userId}.json`, {
headers: {
Accept: "application/json, text/plain, */*",
},
body: JSON.stringify({
id: "calId",
"dav:name": "new cal",
"apple:color": "calId",
"caldav:description": "desc",
}),
});
});
});