Files
TREK/client/src/components/Vacay/useVacayUserPicker.test.ts
T
Maurice 694e11c117 fix(vacay): a failed user load in the desktop invite and share dialogs no longer shows a stale list
When loading the users that can be invited to a fusion or given a calendar
share failed on desktop, the dialog kept showing the list from the last load,
so a user could pick someone who is no longer available and only learn it
from the failed send. The phone sheets already emptied the list and showed
their "no users available" state.

Cause: useVacayUserPicker only emptied the list on a failed load when its
clearOnLoadError option was true; the desktop VacayPersons and
VacaySharedCalendars dialogs passed false, a leftover of the old silent
catch in the desktop dialogs.

Fix: the hook always empties the list when the load fails. The option only
existed to keep the desktop behaviour, so it is removed from the hook and
from all four callers.

Red test: FE-COMP-VACAYPICK-002 (useVacayUserPicker.test.ts) failed on the
unfixed code with "AssertionError: expected [ { id: 2, username: 'bob' },
...(1) ] to deeply equal []".

Changed assertions:
- useVacayUserPicker.test.ts FE-COMP-VACAYPICK-002: was "a failed load keeps
  the last list on desktop" expecting available to equal USERS; now expects
  an empty list.
- FE-COMP-VACAYPICK-003: was "a failed load empties the list on the phone"
  through load() with clearOnLoadError true; now drives the same expectation
  (empty list) through a phone sheet reopening, since the option is gone.
2026-10-09 19:00:47 +02:00

156 lines
6.2 KiB
TypeScript

// FE-COMP-VACAYPICK-001 to -009: picking a user for the fusion invite and the calendar share.
import { act, renderHook } from '@testing-library/react';
import apiClient from '../../api/client';
import { useVacayUserPicker } from './useVacayUserPicker';
const toast = { success: vi.fn(), error: vi.fn() };
vi.mock('../shared/Toast', () => ({ useToast: () => toast }));
vi.mock('../../i18n', () => ({ useTranslation: () => ({ t: (k: string) => k }) }));
const USERS = [
{ id: 2, username: 'bob' },
{ id: 3, username: 'carol' },
];
const submit = vi.fn<(userId: number) => Promise<void>>();
function picker() {
return renderHook(() =>
useVacayUserPicker<{ id: number; username: string }>({
endpoint: '/addons/vacay/shares/available-users',
submit,
successKey: 'vacay.shareSent',
errorKey: 'vacay.shareFailed',
})
);
}
beforeEach(() => {
submit.mockReset().mockResolvedValue(undefined);
toast.success.mockReset();
toast.error.mockReset();
});
afterEach(() => vi.restoreAllMocks());
describe('useVacayUserPicker', () => {
it('FE-COMP-VACAYPICK-001: loads the users from the endpoint and resolves the picked one', async () => {
const get = vi.spyOn(apiClient, 'get').mockResolvedValue({ data: { users: USERS } });
const { result } = picker();
await act(() => result.current.load());
expect(get).toHaveBeenCalledWith('/addons/vacay/shares/available-users');
expect(result.current.available).toEqual(USERS);
act(() => result.current.setSelected(3));
expect(result.current.selectedUser).toEqual(USERS[1]);
});
it('FE-COMP-VACAYPICK-002: a failed load in a desktop dialog empties the list instead of showing the stale one', async () => {
const get = vi.spyOn(apiClient, 'get').mockResolvedValueOnce({ data: { users: USERS } });
const { result } = picker();
await act(() => result.current.load());
get.mockRejectedValueOnce(new Error('down'));
await act(() => result.current.load());
expect(result.current.available).toEqual([]);
});
it('FE-COMP-VACAYPICK-003: a failed load on reopening a phone sheet empties the list', async () => {
const get = vi.spyOn(apiClient, 'get').mockResolvedValueOnce({ data: { users: USERS } });
const { result, rerender } = renderHook(
({ open }: { open: boolean }) =>
useVacayUserPicker<{ id: number; username: string }>({
endpoint: '/addons/vacay/available-users',
submit,
successKey: 'vacay.inviteSent',
errorKey: 'vacay.inviteError',
sheetOpen: open,
}),
{ initialProps: { open: true } }
);
await act(async () => {});
expect(result.current.available).toEqual(USERS);
get.mockRejectedValueOnce(new Error('down'));
await act(async () => rerender({ open: false }));
await act(async () => rerender({ open: true }));
expect(result.current.available).toEqual([]);
});
it('FE-COMP-VACAYPICK-004: sending without a pick does nothing', async () => {
const onSent = vi.fn();
const { result } = picker();
await act(() => result.current.send(onSent));
expect(submit).not.toHaveBeenCalled();
expect(onSent).not.toHaveBeenCalled();
});
it('FE-COMP-VACAYPICK-005: a sent pick toasts success, then runs onSent', async () => {
const order: string[] = [];
toast.success.mockImplementation(() => order.push('toast'));
const { result } = picker();
act(() => result.current.setSelected(2));
await act(() => result.current.send(() => order.push('sent')));
expect(submit).toHaveBeenCalledWith(2);
expect(toast.success).toHaveBeenCalledWith('vacay.shareSent');
expect(order).toEqual(['toast', 'sent']);
expect(result.current.sending).toBe(false);
});
it('FE-COMP-VACAYPICK-006: a failed send toasts the server error and skips onSent', async () => {
submit.mockRejectedValue(new Error('already shared'));
const onSent = vi.fn();
const { result } = picker();
act(() => result.current.setSelected(2));
await act(() => result.current.send(onSent));
expect(toast.error).toHaveBeenCalledWith('already shared');
expect(onSent).not.toHaveBeenCalled();
expect(result.current.sending).toBe(false);
});
it('FE-COMP-VACAYPICK-007: the desktop dialogs (no sheetOpen) never load on their own', () => {
const get = vi.spyOn(apiClient, 'get').mockResolvedValue({ data: { users: USERS } });
picker();
expect(get).not.toHaveBeenCalled();
});
it('FE-COMP-VACAYPICK-008: every opening of a phone sheet clears the pick, folds the list and reloads', async () => {
const get = vi.spyOn(apiClient, 'get').mockResolvedValue({ data: { users: USERS } });
const { result, rerender } = renderHook(
({ open }: { open: boolean }) =>
useVacayUserPicker<{ id: number; username: string }>({
endpoint: '/addons/vacay/available-users',
submit,
successKey: 'vacay.inviteSent',
errorKey: 'vacay.inviteError',
sheetOpen: open,
}),
{ initialProps: { open: false } }
);
expect(get).not.toHaveBeenCalled();
await act(async () => rerender({ open: true }));
expect(get).toHaveBeenCalledTimes(1);
expect(result.current.available).toEqual(USERS);
act(() => result.current.togglePicker());
act(() => result.current.setSelected(2));
expect(result.current.pickerOpen).toBe(true);
await act(async () => rerender({ open: false }));
expect(get).toHaveBeenCalledTimes(1);
expect(result.current.selected).toBe(2);
await act(async () => rerender({ open: true }));
expect(get).toHaveBeenCalledTimes(2);
expect(result.current.selected).toBeNull();
expect(result.current.pickerOpen).toBe(false);
});
it('FE-COMP-VACAYPICK-009: toggling unfolds the inline list and picking a user folds it again', () => {
const { result } = picker();
expect(result.current.pickerOpen).toBe(false);
act(() => result.current.togglePicker());
expect(result.current.pickerOpen).toBe(true);
act(() => result.current.pick(3));
expect(result.current.selected).toBe(3);
expect(result.current.pickerOpen).toBe(false);
act(() => result.current.togglePicker());
act(() => result.current.togglePicker());
expect(result.current.pickerOpen).toBe(false);
});
});