fix(attachments): clear visible file context when application changes

This commit is contained in:
cesnimda
2026-11-17 07:52:22 +01:00
parent c354e38b59
commit 2affeaca68
2 changed files with 36 additions and 0 deletions
@@ -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;
}
});
@@ -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]);