From 43f94ede6410b4e8ce4de1a1ec0ffa1cf79a8cbb Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 20 Nov 2024 16:43:59 +0100 Subject: [PATCH] fix: refs #7229 url + test --- src/composables/downloadFile.js | 2 +- .../composables/downloadFile.spec.js | 33 ++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/composables/downloadFile.js b/src/composables/downloadFile.js index 4588265a2..ef1a1093e 100644 --- a/src/composables/downloadFile.js +++ b/src/composables/downloadFile.js @@ -9,7 +9,7 @@ const token = getTokenMultimedia(); export async function downloadFile(id, model = 'dms', urlPath = '/downloadFile', url) { const appUrl = (await getUrl('', 'lilium')).replace('/#/', ''); const response = await axios.get( - url ?? `${appUrl}/api/${model}/${id}${urlPath}?access_token=${token}`, + url ?? `${appUrl}/${model}/${id}${urlPath}?access_token=${token}`, { responseType: 'blob' } ); diff --git a/test/vitest/__tests__/composables/downloadFile.spec.js b/test/vitest/__tests__/composables/downloadFile.spec.js index f611479bf..8d480fcef 100644 --- a/test/vitest/__tests__/composables/downloadFile.spec.js +++ b/test/vitest/__tests__/composables/downloadFile.spec.js @@ -1,25 +1,36 @@ -import { vi, describe, expect, it } from 'vitest'; +import { vi, describe, expect, it, beforeAll, afterAll } from 'vitest'; import { axios } from 'app/test/vitest/helper'; import { downloadFile } from 'src/composables/downloadFile'; import { useSession } from 'src/composables/useSession'; - const session = useSession(); const token = session.getToken(); describe('downloadFile', () => { + const baseUrl = 'http://localhost:9000'; + let defaulCreateObjectURL; + + beforeAll(() => { + defaulCreateObjectURL = window.URL.createObjectURL; + window.URL.createObjectURL = vi.fn(() => 'blob:http://localhost:9000/blob-id'); + }); + + afterAll(() => (window.URL.createObjectURL = defaulCreateObjectURL)); + it('should open a new window to download the file', async () => { - const url = 'http://localhost:9000'; - - vi.spyOn(axios, 'get').mockResolvedValueOnce({ data: url }); - - const mockWindowOpen = vi.spyOn(window, 'open'); + const res = { + data: new Blob(['file content'], { type: 'application/octet-stream' }), + headers: { 'content-disposition': 'attachment; filename="test-file.txt"' }, + }; + vi.spyOn(axios, 'get').mockImplementation((url) => { + if (url == 'Urls/getUrl') return Promise.resolve({ data: baseUrl }); + else if (url.includes('downloadFile')) return Promise.resolve(res); + }); await downloadFile(1); - expect(mockWindowOpen).toHaveBeenCalledWith( - `${url}/api/dms/1/downloadFile?access_token=${token}` + expect(axios.get).toHaveBeenCalledWith( + `${baseUrl}/dms/1/downloadFile?access_token=${token}`, + { responseType: 'blob' } ); - - mockWindowOpen.mockRestore(); }); });