2024-11-20 15:43:59 +00:00
|
|
|
import { vi, describe, expect, it, beforeAll, afterAll } from 'vitest';
|
2023-10-31 07:56:51 +00:00
|
|
|
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', () => {
|
2024-11-20 15:43:59 +00:00
|
|
|
const baseUrl = 'http://localhost:9000';
|
|
|
|
let defaulCreateObjectURL;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
defaulCreateObjectURL = window.URL.createObjectURL;
|
|
|
|
window.URL.createObjectURL = vi.fn(() => 'blob:http://localhost:9000/blob-id');
|
|
|
|
});
|
2023-10-31 07:56:51 +00:00
|
|
|
|
2024-11-20 15:43:59 +00:00
|
|
|
afterAll(() => (window.URL.createObjectURL = defaulCreateObjectURL));
|
2023-10-31 07:56:51 +00:00
|
|
|
|
2024-11-20 15:43:59 +00:00
|
|
|
it('should open a new window to download the file', async () => {
|
|
|
|
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);
|
|
|
|
});
|
2023-10-31 07:56:51 +00:00
|
|
|
|
|
|
|
await downloadFile(1);
|
|
|
|
|
2024-11-20 15:43:59 +00:00
|
|
|
expect(axios.get).toHaveBeenCalledWith(
|
2024-11-22 10:17:47 +00:00
|
|
|
`${baseUrl}/api/dms/1/downloadFile?access_token=${token}`,
|
2024-11-20 15:43:59 +00:00
|
|
|
{ responseType: 'blob' }
|
2023-10-31 07:56:51 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|