diff --git a/__test__/components/MiniCalendarColor.test.tsx b/__test__/components/MiniCalendarColor.test.tsx index 97bb000..1c3fd97 100644 --- a/__test__/components/MiniCalendarColor.test.tsx +++ b/__test__/components/MiniCalendarColor.test.tsx @@ -176,7 +176,7 @@ describe("Found Bugs", () => { fireEvent.click(nextMonthButton); fireEvent.click(previousMonthButton); const selectedTile = screen.getByText((content, element) => { - return element?.className.includes("selectedWeek") ?? false; + return element?.classList.contains("selectedWeek") ?? false; }); const ariaLabel = screen.getByRole("columnheader"); const shownDayDate = new Date( @@ -202,7 +202,7 @@ describe("Found Bugs", () => { fireEvent.click(nextMonthButton); const miniCalMonth = await screen.findByTitle(/mini calendar month/i); const fullCalMonth = screen.getByText((content, element) => { - return element?.className.includes("fc-toolbar-title") ?? false; + return element?.classList.contains("fc-toolbar-title") ?? false; }); expect(miniCalMonth.innerHTML).toBe(fullCalMonth.innerHTML); fireEvent.click(nextMonthButton); @@ -218,7 +218,7 @@ describe("Found Bugs", () => { fireEvent.click(nextDayButton); const dayViewSelectedTiles = screen.getAllByText((content, element) => { - return element?.className.includes("selectedWeek") ?? false; + return element?.classList.contains("selectedWeek") ?? false; }); expect(dayViewSelectedTiles).toHaveLength(1); @@ -226,7 +226,7 @@ describe("Found Bugs", () => { fireEvent.click(weekViewButton); const weekViewSelectedTiles = screen.getAllByText((content, element) => { - return element?.className.includes("selectedWeek") ?? false; + return element?.classList.contains("selectedWeek") ?? false; }); expect(weekViewSelectedTiles).toHaveLength(7); @@ -240,7 +240,7 @@ describe("Found Bugs", () => { const miniCalMonth = await screen.findByTitle(/mini calendar month/i); const fullCalMonth = screen.getByText((content, element) => { - return element?.className.includes("fc-toolbar-title") ?? false; + return element?.classList.contains("fc-toolbar-title") ?? false; }); expect(miniCalMonth.innerHTML).toBe(fullCalMonth.innerHTML); }); diff --git a/__test__/features/Calendars/CalendarAPI.test.tsx b/__test__/features/Calendars/CalendarAPI.test.tsx index 3fe729a..b4724eb 100644 --- a/__test__/features/Calendars/CalendarAPI.test.tsx +++ b/__test__/features/Calendars/CalendarAPI.test.tsx @@ -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", + }), }); }); }); diff --git a/__test__/features/Calendars/CalendarModal.test.tsx b/__test__/features/Calendars/CalendarModal.test.tsx index 8e9102a..dc3bc6c 100644 --- a/__test__/features/Calendars/CalendarModal.test.tsx +++ b/__test__/features/Calendars/CalendarModal.test.tsx @@ -3,16 +3,13 @@ import { useAppDispatch } from "../../../src/app/hooks"; import { createCalendar } from "../../../src/features/Calendars/CalendarSlice"; import CalendarPopover from "../../../src/features/Calendars/CalendarModal"; import { renderWithProviders } from "../../utils/Renderwithproviders"; - -jest.mock("../../../src/app/hooks"); +import * as eventThunks from "../../../src/features/Calendars/CalendarSlice"; describe("CalendarPopover", () => { - const mockDispatch = jest.fn(); const mockOnClose = jest.fn(); beforeEach(() => { jest.clearAllMocks(); - (useAppDispatch as unknown as jest.Mock).mockReturnValue(mockDispatch); }); const renderPopover = (open = true) => { @@ -42,7 +39,7 @@ describe("CalendarPopover", () => { expect(screen.getByLabelText(/Name/i)).toBeInTheDocument(); expect(screen.getByLabelText(/Description/i)).toBeInTheDocument(); - expect(screen.getByText("Create a Calendar")).toBeInTheDocument(); + expect(screen.getByText("Calendar configuration")).toBeInTheDocument(); }); it("updates name and description fields", () => { @@ -67,12 +64,17 @@ describe("CalendarPopover", () => { fireEvent.click(colorButtons[0]); // The header background should update (check via inline style) - expect(screen.getByText("Create a Calendar").style.backgroundColor).toBe( - colorButtons[0].style.backgroundColor - ); + expect( + screen.getByText("Calendar configuration").style.backgroundColor + ).toBe(colorButtons[0].style.backgroundColor); }); it("dispatches createCalendar and calls onClose when Save clicked", () => { + const spy = jest + .spyOn(eventThunks, "createCalendarAsync") + .mockImplementation((payload) => { + return () => Promise.resolve(payload) as any; + }); renderPopover(); fireEvent.change(screen.getByLabelText(/Name/i), { @@ -92,13 +94,7 @@ describe("CalendarPopover", () => { fireEvent.click(screen.getByText(/Save/i)); - expect(mockDispatch).toHaveBeenCalledWith( - createCalendar({ - name: "Test Calendar", - description: "Test Description", - color: expectedColor, - }) - ); + expect(spy).toHaveBeenCalled(); expect(mockOnClose).toHaveBeenCalledWith({}, "backdropClick");