forked from verdnatura/salix-front
37 lines
1.4 KiB
JavaScript
37 lines
1.4 KiB
JavaScript
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 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(axios.get).toHaveBeenCalledWith(
|
|
`${baseUrl}/dms/1/downloadFile?access_token=${token}`,
|
|
{ responseType: 'blob' }
|
|
);
|
|
});
|
|
});
|