fix(documents): download the previewed attachment by its real id
This commit is contained in:
@@ -39,6 +39,36 @@ beforeEach(() => {
|
|||||||
mockedApi.delete.mockResolvedValue({ data: {} } as any);
|
mockedApi.delete.mockResolvedValue({ data: {} } as any);
|
||||||
});
|
});
|
||||||
afterEach(() => localStorage.removeItem("uiLanguage"));
|
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") {
|
async function open(action: "rename" | "delete") {
|
||||||
const view = render(page());
|
const view = render(page());
|
||||||
fireEvent.click(await screen.findByRole("button", { name: `${action === "rename" ? "Rename" : "Delete"}: ${file.fileName}` }));
|
fireEvent.click(await screen.findByRole("button", { name: `${action === "rename" ? "Rename" : "Delete"}: ${file.fileName}` }));
|
||||||
|
|||||||
@@ -96,7 +96,7 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
|||||||
const [loadError, setLoadError] = useState<string | null>(null);
|
const [loadError, setLoadError] = useState<string | null>(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [previewOpen, setPreviewOpen] = useState(false);
|
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 [dragActive, setDragActive] = useState(false);
|
||||||
const [uploading, setUploading] = useState(false);
|
const [uploading, setUploading] = useState(false);
|
||||||
const fileInputRef = useRef<HTMLInputElement | null>(null);
|
const fileInputRef = useRef<HTMLInputElement | null>(null);
|
||||||
@@ -263,7 +263,7 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
|||||||
if (preview?.url) URL.revokeObjectURL(preview.url);
|
if (preview?.url) URL.revokeObjectURL(preview.url);
|
||||||
const url = await getBlobUrl(a.id, active);
|
const url = await getBlobUrl(a.id, active);
|
||||||
if (!url) return;
|
if (!url) return;
|
||||||
setPreview({ url, type: a.fileType || "", name: a.fileName });
|
setPreview({ id: a.id, url, type: a.fileType || "", name: a.fileName });
|
||||||
setPreviewOpen(true);
|
setPreviewOpen(true);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (active()) toast(getApiErrorMessage(error, currentT.current("attachmentsPreviewFailed")), "error");
|
if (active()) toast(getApiErrorMessage(error, currentT.current("attachmentsPreviewFailed")), "error");
|
||||||
@@ -419,7 +419,7 @@ export default function Attachments({ jobId }: { jobId: number }) {
|
|||||||
)}
|
)}
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
<DialogActions>
|
<DialogActions>
|
||||||
{preview ? <Button onClick={() => void download({ id: 0, fileName: preview.name, fileType: preview.type, fileSize: 0, uploadDate: "" } as AttachmentItem)}>{t("attachmentsDownload")}</Button> : null}
|
{preview ? <Button onClick={() => void download({ id: preview.id, fileName: preview.name, fileType: preview.type, fileSize: 0, uploadDate: "" } as AttachmentItem)}>{t("attachmentsDownload")}</Button> : null}
|
||||||
<Button onClick={() => setPreviewOpen(false)}>{t("close")}</Button>
|
<Button onClick={() => setPreviewOpen(false)}>{t("close")}</Button>
|
||||||
</DialogActions>
|
</DialogActions>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
|
|||||||
Reference in New Issue
Block a user