[#80] added selected calendars local persistance + tests (#281)

Co-authored-by: Camille Moussu <cmoussu@linagora.com>
This commit is contained in:
Camille Moussu
2025-11-04 16:00:08 +01:00
committed by GitHub
parent 838262fde3
commit 4f5308fe08
4 changed files with 75 additions and 6 deletions
+43
View File
@@ -20,6 +20,9 @@ function CalendarTestWrapper() {
} }
describe("CalendarSelection", () => { describe("CalendarSelection", () => {
beforeEach(() => {
localStorage.clear();
});
const today = new Date(); const today = new Date();
const start = new Date(today); const start = new Date(today);
start.setHours(10, 0, 0, 0); start.setHours(10, 0, 0, 0);
@@ -270,9 +273,49 @@ describe("CalendarSelection", () => {
) )
).toHaveClass("event-dot"); ).toHaveClass("event-dot");
}); });
it("renders calendars with local storage", async () => {
const mockCalendarRef = { current: null };
localStorage.setItem(
"selectedCalendars",
JSON.stringify(Object.keys(preloadedState.calendars.list))
);
await act(async () => {
renderWithProviders(
<CalendarApp calendarRef={mockCalendarRef} />,
preloadedState
);
});
expect(screen.getByLabelText("Calendar personnal")).toBeChecked();
expect(screen.getByLabelText("Calendar delegated")).toBeChecked();
expect(screen.getByLabelText("Calendar shared")).toBeChecked();
});
it("persist selected calendars in local storage", async () => {
const mockCalendarRef = { current: null };
await act(async () => {
renderWithProviders(
<CalendarApp calendarRef={mockCalendarRef} />,
preloadedState
);
});
expect(screen.getByLabelText("Calendar personnal")).toBeChecked();
expect(screen.getByLabelText("Calendar delegated")).not.toBeChecked();
expect(screen.getByLabelText("Calendar shared")).not.toBeChecked();
await act(async () => {
fireEvent.click(screen.getByLabelText("Calendar personnal"));
fireEvent.click(screen.getByLabelText("Calendar shared"));
});
expect(localStorage.getItem("selectedCalendars")).toBe('["user3/cal1"]');
});
}); });
describe("calendar Availability search", () => { describe("calendar Availability search", () => {
beforeEach(() => {
localStorage.clear();
});
const preloadedState = { const preloadedState = {
user: { user: {
userData: { userData: {
@@ -6,6 +6,9 @@ import * as calendarThunks from "../../src/features/Calendars/CalendarSlice";
import userEvent from "@testing-library/user-event"; import userEvent from "@testing-library/user-event";
describe("CalendarSelection", () => { describe("CalendarSelection", () => {
beforeEach(() => {
localStorage.clear();
});
const baseUser = { const baseUser = {
userData: { userData: {
sub: "test", sub: "test",
+21 -4
View File
@@ -102,14 +102,31 @@ export default function CalendarApp({
useEffect(() => { useEffect(() => {
if (initialLoadRef.current && Object.keys(calendars).length > 0 && userId) { if (initialLoadRef.current && Object.keys(calendars).length > 0 && userId) {
const personalCalendarIds = Object.keys(calendars).filter( const cached = localStorage.getItem("selectedCalendars");
(id) => id.split("/")[0] === userId if (cached && cached.length > 0) {
); const parsed = JSON.parse(cached) as string[];
setSelectedCalendars(personalCalendarIds); const valid = parsed.filter((id) => calendars[id]);
setSelectedCalendars(valid);
} else {
const personalCalendarIds = Object.keys(calendars).filter(
(id) => id.split("/")[0] === userId
);
setSelectedCalendars(personalCalendarIds);
}
initialLoadRef.current = false; initialLoadRef.current = false;
} }
}, [calendars, userId]); }, [calendars, userId]);
// Save selected cals to cache
useEffect(() => {
if (Object.keys(calendars).length > 0) {
localStorage.setItem(
"selectedCalendars",
JSON.stringify(selectedCalendars)
);
}
}, [selectedCalendars]);
useEffect(() => { useEffect(() => {
updateDarkColor(calendars, theme, dispatch); updateDarkColor(calendars, theme, dispatch);
}, [ }, [
@@ -4,7 +4,7 @@ import AccordionSummary from "@mui/material/AccordionSummary";
import Typography from "@mui/material/Typography"; import Typography from "@mui/material/Typography";
import { useAppDispatch, useAppSelector } from "../../app/hooks"; import { useAppDispatch, useAppSelector } from "../../app/hooks";
import AddIcon from "@mui/icons-material/Add"; import AddIcon from "@mui/icons-material/Add";
import { useState, useMemo } from "react"; import { useState, useMemo, useEffect } from "react";
import CalendarPopover from "./CalendarModal"; import CalendarPopover from "./CalendarModal";
import { Calendars } from "../../features/Calendars/CalendarTypes"; import { Calendars } from "../../features/Calendars/CalendarTypes";
import MoreVertIcon from "@mui/icons-material/MoreVert"; import MoreVertIcon from "@mui/icons-material/MoreVert";
@@ -40,7 +40,7 @@ function CalendarAccordion({
const [expended, setExpended] = useState(defaultExpanded); const [expended, setExpended] = useState(defaultExpanded);
if (calendars.length === 0 && !showAddButton) return null; if (calendars.length === 0 && !showAddButton) return null;
useEffect(() => setExpended(defaultExpanded), [defaultExpanded]);
return ( return (
<Accordion <Accordion
defaultExpanded={defaultExpanded} defaultExpanded={defaultExpanded}
@@ -149,6 +149,9 @@ export default function CalendarSelection({
setAnchorElCal(document.body); setAnchorElCal(document.body);
setSelectedCalId(id); setSelectedCalId(id);
}} }}
defaultExpanded={selectedCalendars.some((id) =>
delegatedCalendars.includes(id)
)}
/> />
<CalendarAccordion <CalendarAccordion
@@ -164,6 +167,9 @@ export default function CalendarSelection({
setAnchorElCal(document.body); setAnchorElCal(document.body);
setSelectedCalId(id); setSelectedCalId(id);
}} }}
defaultExpanded={selectedCalendars.some((id) =>
sharedCalendars.includes(id)
)}
/> />
</div> </div>
<CalendarPopover <CalendarPopover