diff --git a/components/settings/__tests__/calendar-management-settings.test.tsx b/components/settings/__tests__/calendar-management-settings.test.tsx index 7b8a25c5b..7cf86f45d 100644 --- a/components/settings/__tests__/calendar-management-settings.test.tsx +++ b/components/settings/__tests__/calendar-management-settings.test.tsx @@ -187,3 +187,35 @@ describe('CalendarManagementSettings with two accounts', () => { useAccountStore.setState({ accounts: [], activeAccountId: null }); }); }); + +// pim-p2-3: opened straight away (no visit to Calendar first), Settings +// loaded only the account on screen. +describe('CalendarManagementSettings opened with two accounts signed in', () => { + it("loads and lists both accounts' calendars", async () => { + const { useAccountStore } = await import('@/stores/account-store'); + const fake = (name: string) => ({ + supportsCalendars: () => true, + getAccountId: () => 'acct', + getAllCalendarsWithFailures: vi.fn(async () => ({ calendars: [{ ...appleColored, id: 'cal', name }], failedAccountIds: [] })), + }); + const clients: Record = { 'login-a': fake('Home'), 'login-b': fake('Office') }; + useAccountStore.setState({ + accounts: [ + { id: 'login-a', email: 'a@example.org', username: 'a@example.org', displayName: 'Sam', label: 'Sam', isConnected: true }, + { id: 'login-b', email: 'b@example.org', username: 'b@example.org', displayName: 'Sam', label: 'Sam', isConnected: true }, + ] as never, + activeAccountId: 'login-a', + }); + useAuthStore.setState({ + client: clients['login-a'], serverUrl: null, username: null, activeAccountId: 'login-a', + getClientForAccount: ((id: string) => clients[id]) as never, + } as never); + useCalendarStore.setState({ calendars: [], icalSubscriptions: [], updateCalendar: vi.fn() } as never); + + render(); + + await waitFor(() => expect(screen.getAllByTestId('calendar-settings-account').map((el) => el.textContent)).toEqual(['a@example.org', 'b@example.org'])); + useAccountStore.setState({ accounts: [], activeAccountId: null }); + useCalendarStore.getState().setAccountClients(null, null); + }); +}); diff --git a/components/settings/calendar-management-settings.tsx b/components/settings/calendar-management-settings.tsx index c274a7282..e0b068569 100644 --- a/components/settings/calendar-management-settings.tsx +++ b/components/settings/calendar-management-settings.tsx @@ -25,6 +25,7 @@ import { useSettingsStore } from '@/stores/settings-store'; import { useManagedAccountStore } from '@/stores/managed-account-store'; import { useAccountStore } from '@/stores/account-store'; import { groupCalendarsByAccount } from '@/lib/calendar-targets'; +import { useProMultiAccountCalendars } from '@/hooks/use-pro-multi-account-calendars'; import { apiFetch } from '@/lib/browser-navigation'; import { CALENDAR_COLORS, sharedCalendarColorKey } from '@/lib/shared-calendar-colors'; import { IS_LITE } from '@/lib/lite'; @@ -342,12 +343,14 @@ export function CalendarManagementSettings() { calendars.some(cal => !cal.isShared && Object.keys(cal.shareWith ?? {}).length > 0 && resolvesShares(cal)), ); - // Load calendars if not yet loaded + // With several accounts signed in, every account's calendars, as in + // Calendar (pim-p2-3); else load them if not yet loaded. + const { enabled: allAccounts } = useProMultiAccountCalendars(null, null); useEffect(() => { - if (client && calendars.length === 0) { + if (client && calendars.length === 0 && !allAccounts) { fetchCalendars(client); } - }, [client, calendars.length, fetchCalendars]); + }, [client, calendars.length, fetchCalendars, allAccounts]); useEffect(() => { // CalDAV discovery runs through /api/caldav/discover, absent in the static build.