18834: Unit tests added
[arvados-workbench2.git] / src / services / collection-service / collection-service.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import axios, { AxiosInstance } from 'axios';
6 import MockAdapter from 'axios-mock-adapter';
7 import { CollectionResource } from 'models/collection';
8 import { AuthService } from '../auth-service/auth-service';
9 import { CollectionService } from './collection-service';
10
11 describe('collection-service', () => {
12     let collectionService: CollectionService;
13     let serverApi: AxiosInstance;
14     let axiosMock: MockAdapter;
15     let webdavClient: any;
16     let authService;
17     let actions;
18
19     beforeEach(() => {
20         serverApi = axios.create();
21         axiosMock = new MockAdapter(serverApi);
22         webdavClient = {
23             delete: jest.fn(),
24             upload: jest.fn(),
25         } as any;
26         authService = {} as AuthService;
27         actions = {
28             progressFn: jest.fn(),
29         } as any;
30         collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
31         collectionService.update = jest.fn();
32     });
33
34     describe('get', () => {
35         it('should make a list request with uuid filtering', async () => {
36             serverApi.get = jest.fn(() => Promise.resolve(
37                 { data: { items: [{}] } }
38             ));
39             const uuid = 'zzzzz-4zz18-0123456789abcde'
40             await collectionService.get(uuid);
41             expect(serverApi.get).toHaveBeenCalledWith(
42                 '/collections', {
43                     params: {
44                         filters: `[["uuid","=","zzzzz-4zz18-0123456789abcde"]]`,
45                         include_old_versions: true,
46                     },
47                 }
48             );
49         });
50
51         it('should be able to request specific fields', async () => {
52             serverApi.get = jest.fn(() => Promise.resolve(
53                 { data: { items: [{}] } }
54             ));
55             const uuid = 'zzzzz-4zz18-0123456789abcde'
56             await collectionService.get(uuid, undefined, ['manifestText']);
57             expect(serverApi.get).toHaveBeenCalledWith(
58                 '/collections', {
59                     params: {
60                         filters: `[["uuid","=","zzzzz-4zz18-0123456789abcde"]]`,
61                         include_old_versions: true,
62                         select: `["manifest_text"]`
63                     },
64                 }
65             );
66         });
67     });
68
69     describe('update', () => {
70         it('should call put selecting updated fields + others', async () => {
71             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
72             const data: Partial<CollectionResource> = {
73                 name: 'foo',
74             };
75             const expected = {
76                 collection: {
77                     ...data,
78                     preserve_version: true,
79                 },
80                 select: ['uuid', 'name', 'version', 'modified_at'],
81             }
82             collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
83             await collectionService.update('uuid', data);
84             expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
85         });
86     });
87
88     describe('uploadFiles', () => {
89         it('should skip if no files to upload files', async () => {
90             // given
91             const files: File[] = [];
92             const collectionUUID = '';
93
94             // when
95             await collectionService.uploadFiles(collectionUUID, files);
96
97             // then
98             expect(webdavClient.upload).not.toHaveBeenCalled();
99         });
100
101         it('should upload files', async () => {
102             // given
103             const files: File[] = [{name: 'test-file1'} as File];
104             const collectionUUID = 'zzzzz-4zz18-0123456789abcde';
105
106             // when
107             await collectionService.uploadFiles(collectionUUID, files);
108
109             // then
110             expect(webdavClient.upload).toHaveBeenCalledTimes(1);
111             expect(webdavClient.upload.mock.calls[0][0]).toEqual("c=zzzzz-4zz18-0123456789abcde/test-file1");
112         });
113
114         it('should upload files with custom uplaod target', async () => {
115             // given
116             const files: File[] = [{name: 'test-file1'} as File];
117             const collectionUUID = 'zzzzz-4zz18-0123456789abcde';
118             const customTarget = 'zzzzz-4zz18-0123456789adddd/test-path/'
119
120             // when
121             await collectionService.uploadFiles(collectionUUID, files, undefined, customTarget);
122
123             // then
124             expect(webdavClient.upload).toHaveBeenCalledTimes(1);
125             expect(webdavClient.upload.mock.calls[0][0]).toEqual("c=zzzzz-4zz18-0123456789adddd/test-path//test-file1");
126         });
127     });
128
129     describe('deleteFiles', () => {
130         it('should remove no files', async () => {
131             // given
132             const filePaths: string[] = [];
133             const collectionUUID = '';
134
135             // when
136             await collectionService.deleteFiles(collectionUUID, filePaths);
137
138             // then
139             expect(webdavClient.delete).not.toHaveBeenCalled();
140         });
141
142         it('should remove only root files', async () => {
143             // given
144             const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
145             const collectionUUID = '';
146
147             // when
148             await collectionService.deleteFiles(collectionUUID, filePaths);
149
150             // then
151             expect(webdavClient.delete).toHaveBeenCalledTimes(3);
152             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
153             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
154             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
155         });
156
157         it('should remove files with uuid prefix', async () => {
158             // given
159             const filePaths: string[] = ['/root/1'];
160             const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
161
162             // when
163             await collectionService.deleteFiles(collectionUUID, filePaths);
164
165             // then
166             expect(webdavClient.delete).toHaveBeenCalledTimes(1);
167             expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");
168         });
169     });
170 });