debian-mirror-gitlab/spec/frontend/lib/utils/downloader_spec.js

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import downloader from '~/lib/utils/downloader';
describe('Downloader', () => {
let a;
beforeEach(() => {
a = { click: jest.fn() };
jest.spyOn(document, 'createElement').mockImplementation(() => a);
});
afterEach(() => {
jest.clearAllMocks();
});
describe('when inline file content is provided', () => {
const fileData = 'inline content';
const fileName = 'test.csv';
it('uses the data urls to download the file', () => {
downloader({ fileName, fileData });
expect(document.createElement).toHaveBeenCalledWith('a');
expect(a.download).toBe(fileName);
expect(a.href).toBe(`data:text/plain;base64,${fileData}`);
expect(a.click).toHaveBeenCalledTimes(1);
});
});
describe('when an endpoint is provided', () => {
const url = 'https://gitlab.com/test.csv';
const fileName = 'test.csv';
it('uses the endpoint to download the file', () => {
downloader({ fileName, url });
expect(document.createElement).toHaveBeenCalledWith('a');
expect(a.download).toBe(fileName);
expect(a.href).toBe(url);
expect(a.click).toHaveBeenCalledTimes(1);
});
});
});