From 2eac82c207ff55fce07fd997c314bc876c7a0ac8 Mon Sep 17 00:00:00 2001 From: Hamza Faidi <66977086+hamzafa1d1@users.noreply.github.com> Date: Thu, 12 Mar 2026 08:00:50 +0000 Subject: [PATCH] fix(front): stabilize downloadFile unit test and return promise chain (#18484) # Description ## What this PR fixes This PR fixes a flaky/skipped unit test for [downloadFile](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) and aligns the test with the actual implementation. ## Changes made Updated [downloadFile](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) test to validate file-saver behavior instead of DOM anchor creation. Mocked [saveAs](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) from file-saver and asserted it is called with the fetched blob and filename. Added proper async assertions for: successful file download failed fetch path ([status !== 200](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html)) rejecting with Failed downloading file Updated [downloadFile](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-browser/workbench/workbench.html) implementation to return the fetch promise chain so callers/tests can await it reliably. ## Related Issue Closes #18485 --- .../utils/__tests__/downloadFile.test.ts | 52 ++++++++++--------- .../activities/files/utils/downloadFile.ts | 2 +- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/packages/twenty-front/src/modules/activities/files/utils/__tests__/downloadFile.test.ts b/packages/twenty-front/src/modules/activities/files/utils/__tests__/downloadFile.test.ts index b1f398993e..f31398aefa 100644 --- a/packages/twenty-front/src/modules/activities/files/utils/__tests__/downloadFile.test.ts +++ b/packages/twenty-front/src/modules/activities/files/utils/__tests__/downloadFile.test.ts @@ -1,35 +1,39 @@ import { downloadFile } from '@/activities/files/utils/downloadFile'; +import { saveAs } from 'file-saver'; + +jest.mock('file-saver', () => ({ + saveAs: jest.fn(), +})); + +const mockBlob = new Blob(['test content'], { type: 'application/pdf' }); global.fetch = jest.fn(() => Promise.resolve({ status: 200, - blob: jest.fn(), + blob: () => Promise.resolve(mockBlob), } as unknown as Response), ); -window.URL.createObjectURL = jest.fn(() => 'mock-url'); -window.URL.revokeObjectURL = jest.fn(); - -// FIXME: jest is behaving weirdly here, it's not finding the element -// Also the document's innerHTML is empty -// `global.fetch` and `window.fetch` are also undefined -describe.skip('downloadFile', () => { - it('should download a file', () => { - downloadFile('url/to/file.pdf', 'file.pdf'); - - expect(fetch).toHaveBeenCalledWith('url/to/file.pdf'); - - const link = document.querySelector( - 'a[href="mock-url"][download="file.pdf"]', - ); - - expect(link).not.toBeNull(); - // oxlint-disable-next-line @typescripttypescript/ban-ts-comment - // @ts-ignore - expect(link?.style?.display).toBe('none'); - - expect(link).toHaveBeenCalledTimes(1); - +describe('downloadFile', () => { + afterEach(() => { jest.clearAllMocks(); }); + + it('should download a file', async () => { + await downloadFile('url/to/file.pdf', 'file.pdf'); + + expect(fetch).toHaveBeenCalledWith('url/to/file.pdf'); + expect(saveAs).toHaveBeenCalledWith(mockBlob, 'file.pdf'); + }); + + it('should reject when fetch fails', async () => { + (global.fetch as jest.Mock).mockResolvedValueOnce({ + status: 404, + blob: () => Promise.resolve(mockBlob), + }); + + await expect(downloadFile('url/to/file.pdf', 'file.pdf')).rejects.toBe( + 'Failed downloading file', + ); + }); }); diff --git a/packages/twenty-front/src/modules/activities/files/utils/downloadFile.ts b/packages/twenty-front/src/modules/activities/files/utils/downloadFile.ts index bdc5ed0fab..869aea4e7e 100644 --- a/packages/twenty-front/src/modules/activities/files/utils/downloadFile.ts +++ b/packages/twenty-front/src/modules/activities/files/utils/downloadFile.ts @@ -1,7 +1,7 @@ import { saveAs } from 'file-saver'; export const downloadFile = (fullPath: string, fileName: string) => { - fetch(fullPath) + return fetch(fullPath) .then((resp) => resp.status === 200 ? resp.blob()