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
This commit is contained in:
Hamza Faidi
2026-03-12 08:00:50 +00:00
committed by GitHub
parent f262437da6
commit 2eac82c207
2 changed files with 29 additions and 25 deletions
@@ -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',
);
});
});
@@ -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()