#669 display resource admin (#677)

Co-authored-by: lethemanh <lethemanh@lethemanhs-MacBook-Pro.local>
This commit is contained in:
lethemanh
2026-03-20 18:01:04 +07:00
committed by GitHub
parent 2ba92ed194
commit 5f26bef4dc
7 changed files with 252 additions and 52 deletions
@@ -87,7 +87,7 @@ const baseCalendar: Calendar = {
events: {},
invite: [],
owner: { emails: ["user1@example.com"] },
access: { write: true },
access: { write: true } as any,
delegated: true,
};
@@ -261,6 +261,82 @@ describe("CalendarAccessRights", () => {
expect(document.querySelector('[role="progressbar"]')).toBeInTheDocument();
});
it("displays the calendar owner information", () => {
renderWithProviders(
<CalendarAccessRights
calendar={baseCalendar}
value={[]}
onChange={mockOnChange}
onInvitesLoaded={mockOnInvitesLoaded}
/>,
{ ...userState }
);
expect(screen.getAllByText("user1@example.com").length).toBeGreaterThan(0);
expect(
screen.getByText("calendarPopover.access.owner")
).toBeInTheDocument();
});
it("fetches and displays resource administrators for resource calendars", async () => {
const resourceCalendar: any = {
...baseCalendar,
owner: {
_id: "resource1",
resource: true,
emails: ["resource1@example.com"],
administrators: [
{ id: "admin1" },
{ id: "admin2" },
{ id: "resource1" }, // Owner shouldn't be loaded as an admin
],
},
};
(getUserDetails as jest.Mock).mockImplementation((id: string) => {
if (id === "admin1") {
return Promise.resolve({
preferredEmail: "admin1@example.com",
firstname: "Admin",
lastname: "One",
});
}
if (id === "admin2") {
return Promise.resolve({
preferredEmail: "admin2@example.com",
firstname: "Admin",
lastname: "Two",
});
}
return Promise.resolve(null);
});
renderWithProviders(
<CalendarAccessRights
calendar={resourceCalendar}
value={[]}
onChange={mockOnChange}
onInvitesLoaded={mockOnInvitesLoaded}
/>,
{ ...userState }
);
await waitFor(() => {
expect(getUserDetails).toHaveBeenCalledWith("admin1");
expect(getUserDetails).toHaveBeenCalledWith("admin2");
});
expect(getUserDetails).not.toHaveBeenCalledWith("resource1");
expect(await screen.findByText("Admin One")).toBeInTheDocument();
expect(screen.getByText("admin1@example.com")).toBeInTheDocument();
expect(screen.getByText("Admin Two")).toBeInTheDocument();
expect(screen.getByText("admin2@example.com")).toBeInTheDocument();
expect(
screen.getAllByText("calendarPopover.access.administrator")
).toHaveLength(2);
});
});
const userState = {
@@ -1,16 +1,15 @@
import { addCalendarResourceAsync } from "@/features/Calendars/api/addCalendarResourceAsync";
import { addSharedCalendar } from "@/features/Calendars/CalendarApi";
import { getResourceDetails, getUserDetails } from "@/features/User/userAPI";
import { fetchOwnerOfResource } from "@/features/Calendars/services/helpers";
import { toRejectedError } from "@/utils/errorUtils";
import { configureStore } from "@reduxjs/toolkit";
jest.mock("@/features/Calendars/CalendarApi");
jest.mock("@/features/User/userAPI");
jest.mock("@/features/Calendars/services/helpers");
jest.mock("@/utils/errorUtils");
const mockedAddSharedCalendar = addSharedCalendar as jest.Mock;
const mockedGetResourceDetails = getResourceDetails as jest.Mock;
const mockedGetUserDetails = getUserDetails as jest.Mock;
const mockedFetchOwnerOfResource = fetchOwnerOfResource as jest.Mock;
const mockedToRejectedError = toRejectedError as jest.Mock;
describe("addCalendarResourceAsync thunk", () => {
@@ -55,8 +54,7 @@ describe("addCalendarResourceAsync thunk", () => {
};
it("should add shared calendar, fetch resource details, map userData", async () => {
mockedGetResourceDetails.mockResolvedValueOnce(mockResolvedResourceData);
mockedGetUserDetails.mockResolvedValueOnce({
mockedFetchOwnerOfResource.mockResolvedValueOnce({
firstname: "Creator",
lastname: "User",
emails: ["creator@example.com"],
@@ -72,8 +70,7 @@ describe("addCalendarResourceAsync thunk", () => {
mockPayload.calId,
mockPayload.cal
);
expect(mockedGetResourceDetails).toHaveBeenCalledWith("res-456");
expect(mockedGetUserDetails).toHaveBeenCalledWith("user-789");
expect(mockedFetchOwnerOfResource).toHaveBeenCalledWith("res-456");
expect(result.type).toBe("calendars/addCalendarResource/fulfilled");
expect(result.payload).toEqual({
@@ -94,9 +91,12 @@ describe("addCalendarResourceAsync thunk", () => {
it("should fallback to name if resource details fetch fails", async () => {
mockedAddSharedCalendar.mockResolvedValueOnce({});
const errorDetails = new Error("Fetch failed");
mockedGetResourceDetails.mockRejectedValueOnce(errorDetails);
const mockRejectedErrorResult = { message: "Fetch failed" };
mockedToRejectedError.mockReturnValueOnce(mockRejectedErrorResult);
mockedFetchOwnerOfResource.mockRejectedValueOnce(errorDetails);
// Silence expected console error in tests
const consoleSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});
const result = await addCalendarResourceAsync(
mockPayload as unknown as Parameters<typeof addCalendarResourceAsync>[0]
@@ -107,9 +107,9 @@ describe("addCalendarResourceAsync thunk", () => {
mockPayload.calId,
mockPayload.cal
);
expect(mockedGetResourceDetails).toHaveBeenCalledWith("res-456");
expect(mockedFetchOwnerOfResource).toHaveBeenCalledWith("res-456");
expect(mockedToRejectedError).toHaveBeenCalledWith(errorDetails);
consoleSpy.mockRestore();
expect(result.type).toBe("calendars/addCalendarResource/fulfilled");
expect(result.payload).toEqual({
@@ -142,7 +142,7 @@ describe("addCalendarResourceAsync thunk", () => {
mockPayload.calId,
mockPayload.cal
);
expect(mockedGetResourceDetails).not.toHaveBeenCalled();
expect(mockedFetchOwnerOfResource).not.toHaveBeenCalled();
expect(mockedToRejectedError).toHaveBeenCalledWith(errorAdd);