diff --git a/job-tracker-ui/src/attachment-action-lifetime.test.tsx b/job-tracker-ui/src/attachment-action-lifetime.test.tsx index 30cb52ea..f8f3e8c4 100644 --- a/job-tracker-ui/src/attachment-action-lifetime.test.tsx +++ b/job-tracker-ui/src/attachment-action-lifetime.test.tsx @@ -39,6 +39,36 @@ beforeEach(() => { mockedApi.delete.mockResolvedValue({ data: {} } as any); }); afterEach(() => localStorage.removeItem("uiLanguage")); + +test.each(['en', 'nb'] as const)('preview download keeps the attachment identity and filename (%s)', async language => { + localStorage.setItem('uiLanguage', language); + const copy = translations[language]; + const originalCreate = URL.createObjectURL; + const originalRevoke = URL.revokeObjectURL; + URL.createObjectURL = jest.fn(() => 'blob:preview-download'); + URL.revokeObjectURL = jest.fn(); + const timers = jest.spyOn(window, 'setTimeout'); + const downloads: string[] = []; + const click = jest.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(function (this: HTMLAnchorElement) { downloads.push(this.download); }); + mockedApi.get.mockImplementation((url: any) => Promise.resolve({ data: String(url).includes('/download/') ? new Blob(['synthetic preview'], { type: 'application/pdf' }) : [file] })); + const view = render(page()); + try { + fireEvent.click(await screen.findByRole('button', { name: `${copy.attachmentsPreview}: ${file.fileName}` })); + const dialog = await screen.findByRole('dialog'); + fireEvent.click(within(dialog).getByRole('button', { name: copy.attachmentsDownload })); + await waitFor(() => expect(downloads).toEqual([file.fileName])); + expect(mockedApi.get.mock.calls.filter(([url]) => String(url).includes('/download/')).map(([url]) => url)).toEqual(['/attachments/download/7', '/attachments/download/7']); + expect(mockedApi.post).not.toHaveBeenCalled(); + expect(mockedApi.patch).not.toHaveBeenCalled(); + expect(mockedApi.delete).not.toHaveBeenCalled(); + } finally { + view.unmount(); click.mockRestore(); + timers.mock.calls.forEach((call, index) => { if (call[1] === 5000) window.clearTimeout(timers.mock.results[index].value); }); + timers.mockRestore(); + URL.createObjectURL = originalCreate; URL.revokeObjectURL = originalRevoke; + } +}); + async function open(action: "rename" | "delete") { const view = render(page()); fireEvent.click(await screen.findByRole("button", { name: `${action === "rename" ? "Rename" : "Delete"}: ${file.fileName}` })); diff --git a/job-tracker-ui/src/components/Attachments.tsx b/job-tracker-ui/src/components/Attachments.tsx index 9bf98c23..e447c34c 100644 --- a/job-tracker-ui/src/components/Attachments.tsx +++ b/job-tracker-ui/src/components/Attachments.tsx @@ -96,7 +96,7 @@ export default function Attachments({ jobId }: { jobId: number }) { const [loadError, setLoadError] = useState(null); const [loading, setLoading] = useState(true); const [previewOpen, setPreviewOpen] = useState(false); - const [preview, setPreview] = useState<{ url: string; type: string; name: string } | null>(null); + const [preview, setPreview] = useState<{ id: number; url: string; type: string; name: string } | null>(null); const [dragActive, setDragActive] = useState(false); const [uploading, setUploading] = useState(false); const fileInputRef = useRef(null); @@ -263,7 +263,7 @@ export default function Attachments({ jobId }: { jobId: number }) { if (preview?.url) URL.revokeObjectURL(preview.url); const url = await getBlobUrl(a.id, active); if (!url) return; - setPreview({ url, type: a.fileType || "", name: a.fileName }); + setPreview({ id: a.id, url, type: a.fileType || "", name: a.fileName }); setPreviewOpen(true); } catch (error) { if (active()) toast(getApiErrorMessage(error, currentT.current("attachmentsPreviewFailed")), "error"); @@ -419,7 +419,7 @@ export default function Attachments({ jobId }: { jobId: number }) { )} - {preview ? : null} + {preview ? : null}