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