From 2affeaca68fa0cf191f1c57edb7cce2b0ac36e5b Mon Sep 17 00:00:00 2001 From: cesnimda Date: Tue, 17 Nov 2026 07:52:22 +0100 Subject: [PATCH] fix(attachments): clear visible file context when application changes --- .../src/attachment-action-lifetime.test.tsx | 32 +++++++++++++++++++ job-tracker-ui/src/components/Attachments.tsx | 4 +++ 2 files changed, 36 insertions(+) diff --git a/job-tracker-ui/src/attachment-action-lifetime.test.tsx b/job-tracker-ui/src/attachment-action-lifetime.test.tsx index a47dab4d..8297ac35 100644 --- a/job-tracker-ui/src/attachment-action-lifetime.test.tsx +++ b/job-tracker-ui/src/attachment-action-lifetime.test.tsx @@ -212,3 +212,35 @@ test.each(['resolve', 'reject'] as const)('old upload %s cannot clear a new appl await act(async () => current.resolve({ data: {} })); await waitFor(() => expect(screen.getByRole('button', { name: translations.en.attachmentsUpload })).toBeEnabled()); }); + +test('switching application clears old attachment actions while the next list is pending', async () => { + const view = render(page()); + await screen.findByText(file.fileName); + const next = deferred(); + mockedApi.get.mockReturnValueOnce(next.promise); + await act(async () => { view.rerender(page(77)); }); + expect(screen.queryByText(file.fileName)).not.toBeInTheDocument(); + expect(screen.queryByRole('button', { name: `Delete: ${file.fileName}` })).not.toBeInTheDocument(); + await act(async () => next.resolve({ data: [{ ...file, id: 77, fileName: 'Next.pdf' }] })); + expect(await screen.findByText('Next.pdf')).toBeVisible(); + expect(mockedApi.delete).not.toHaveBeenCalled(); +}); + +test('switching application closes and releases its already-open attachment preview', async () => { + const originalCreate = URL.createObjectURL; + const originalRevoke = URL.revokeObjectURL; + URL.createObjectURL = jest.fn(() => 'blob:previous-application'); + URL.revokeObjectURL = jest.fn(); + mockedApi.get.mockImplementation((url: any) => Promise.resolve({ data: url === '/attachments/download/7' ? new Blob(['preview'], { type: 'application/pdf' }) : [file] })); + const view = render(page()); + try { + fireEvent.click(await screen.findByRole('button', { name: `Preview: ${file.fileName}` })); + expect(await screen.findByTitle(`Preview: ${file.fileName}`)).toHaveAttribute('src', 'blob:previous-application'); + await act(async () => { view.rerender(page(77)); }); + await waitFor(() => expect(screen.queryByRole('dialog')).not.toBeInTheDocument()); + expect(URL.revokeObjectURL).toHaveBeenCalledWith('blob:previous-application'); + } finally { + view.unmount(); + URL.createObjectURL = originalCreate; URL.revokeObjectURL = originalRevoke; + } +}); diff --git a/job-tracker-ui/src/components/Attachments.tsx b/job-tracker-ui/src/components/Attachments.tsx index d2e0392c..8d7ab7bc 100644 --- a/job-tracker-ui/src/components/Attachments.tsx +++ b/job-tracker-ui/src/components/Attachments.tsx @@ -108,6 +108,10 @@ export default function Attachments({ jobId }: { jobId: number }) { owner.jobId = jobId; owner.generation += 1; setUploading(false); + setItems([]); + setLoadError(null); + setPreviewOpen(false); + setPreview(null); return () => { owner.mounted = false; owner.generation += 1; }; }, [jobId]);