fix(mail-ingest): open the prefilled form again for an entry asked for twice in the same trip

The guard kept the last handled entry after the link left the address, so a second Fill in yourself for it in the open trip did nothing.
This commit is contained in:
Maurice
2026-11-23 14:55:18 +01:00
parent a3caa9ca1a
commit 755ca865a4
2 changed files with 64 additions and 1 deletions
@@ -0,0 +1,59 @@
// FE-MAIL-PREFILL-001 to FE-MAIL-PREFILL-002: "Fill in yourself" opens the booking form in the trip.
import { act, renderHook, waitFor } from '@testing-library/react';
import type { BookingImportPreviewItem } from '@trek/shared';
import { createElement, type ReactNode } from 'react';
import { MemoryRouter, useSearchParams } from 'react-router';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { TranslationProvider } from '../../i18n/TranslationContext';
const repo = vi.hoisted(() => ({ prefill: vi.fn() }));
const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn() }));
vi.mock('../../repo/mailIngestRepo', () => ({ mailIngestRepo: repo }));
vi.mock('../shared/Toast', () => ({ useToast: () => toast }));
const { useMailPrefillLink } = await import('./useMailPrefillLink');
const ITEM = { type: 'hotel', title: 'Hotel Roma' } as BookingImportPreviewItem;
const wrapper = ({ children }: { children: ReactNode }) =>
createElement(
MemoryRouter,
{ initialEntries: ['/trips/5?mailPrefill=12'] },
createElement(TranslationProvider, null, children)
);
function setup() {
const startImportReview = vi.fn();
const view = renderHook(
() => {
const [searchParams, setSearchParams] = useSearchParams();
useMailPrefillLink({ searchParams, setSearchParams, loaded: true, startImportReview });
return { searchParams, setSearchParams };
},
{ wrapper }
);
return { ...view, startImportReview };
}
beforeEach(() => {
vi.clearAllMocks();
repo.prefill.mockResolvedValue({ trip_id: 5, item: ITEM });
});
describe('useMailPrefillLink', () => {
it('FE-MAIL-PREFILL-001: opens the form once and takes the parameter out of the address', async () => {
const { result, startImportReview } = setup();
await waitFor(() => expect(startImportReview).toHaveBeenCalledTimes(1));
expect(startImportReview.mock.calls[0].slice(0, 3)).toEqual([[ITEM], [], 'bookings']);
expect(repo.prefill).toHaveBeenCalledWith(12);
expect(result.current.searchParams.get('mailPrefill')).toBeNull();
});
it('FE-MAIL-PREFILL-002: the same entry asked for again in the open trip opens the form again', async () => {
const { result, startImportReview } = setup();
await waitFor(() => expect(startImportReview).toHaveBeenCalledTimes(1));
act(() => result.current.setSearchParams({ mailPrefill: '12' }));
await waitFor(() => expect(startImportReview).toHaveBeenCalledTimes(2));
expect(result.current.searchParams.get('mailPrefill')).toBeNull();
});
});
@@ -29,7 +29,11 @@ export function useMailPrefillLink({
const handled = useRef<string | null>(null);
useEffect(() => {
if (!requested || !loaded || handled.current === requested) return;
if (!requested) {
handled.current = null;
return;
}
if (!loaded || handled.current === requested) return;
handled.current = requested;
setSearchParams(
(prev) => {