Welcome to mirror list, hosted at ThFree Co, Russian Federation.

downloader_spec.js « utils « lib « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c14cba3a62b46aa563dfaa2e67142cf70dbac98b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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);
    });
  });
});