88791d4b64bb33f3a6fb7fc7d4e66dcb7a5811d3
[arvados-workbench2.git] / src / views-components / context-menu / actions / download-action.test.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import axios from 'axios';
7 import { configure, shallow } from "enzyme";
8 import * as Adapter from 'enzyme-adapter-react-16';
9 import { ListItem } from '@material-ui/core';
10 import * as JSZip from 'jszip';
11 import { DownloadAction } from './download-action';
12
13 configure({ adapter: new Adapter() });
14
15 jest.mock('axios');
16
17 jest.mock('file-saver', () => ({
18     saveAs: jest.fn(),
19 }));
20
21 const mock = {
22     file: jest.fn(),
23     generateAsync: jest.fn().mockImplementation(() => Promise.resolve('test')),
24 };
25
26 jest.mock('jszip', () => jest.fn().mockImplementation(() => mock));
27
28 describe('<DownloadAction />', () => {
29     let props;
30     let zip;
31
32     beforeEach(() => {
33         props = {};
34         zip = new JSZip();
35         (axios as any).get.mockImplementationOnce(() => Promise.resolve({ data: '1234' }));
36     });
37
38     it('should return null if missing href or kind of file in props', () => {
39         // when
40         const wrapper = shallow(<DownloadAction {...props} />);
41
42         // then
43         expect(wrapper.html()).toBeNull();
44     });
45
46     it('should return a element', () => {
47         // setup
48         props.href = '#';
49
50         // when
51         const wrapper = shallow(<DownloadAction {...props} />);
52
53         // then
54         expect(wrapper.html()).not.toBeNull();
55     });
56
57     it('should handle download', () => {
58         // setup
59         props = {
60             href: ['file1'],
61             kind: 'files',
62             download: [],
63             currentCollectionUuid: '123412-123123'
64         };
65         const wrapper = shallow(<DownloadAction {...props} />);
66
67         // when
68         wrapper.find(ListItem).simulate('click');
69
70         // then
71         expect(axios.get).toHaveBeenCalledWith(props.href[0]);
72     });
73 });