18966: Changes back collectionService.get() to use the GET HTTP method.
[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         } 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 request with default selected fields', 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/${uuid}`, {
43                     params: {
44                         select: JSON.stringify(defaultCollectionSelectedFields.map(snakeCase)),
45                     },
46                 }
47             );
48         });
49
50         it('should be able to request specific fields', async () => {
51             serverApi.get = jest.fn(() => Promise.resolve(
52                 { data: { items: [{}] } }
53             ));
54             const uuid = 'zzzzz-4zz18-0123456789abcde'
55             await collectionService.get(uuid, undefined, ['manifestText']);
56             expect(serverApi.get).toHaveBeenCalledWith(
57                 `/collections/${uuid}`, {
58                     params: {
59                         select: `["manifest_text"]`
60                     },
61                 }
62             );
63         });
64     });
65
66     describe('update', () => {
67         it('should call put selecting updated fields + others', async () => {
68             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
69             const data: Partial<CollectionResource> = {
70                 name: 'foo',
71             };
72             const expected = {
73                 collection: {
74                     ...data,
75                     preserve_version: true,
76                 },
77                 select: ['uuid', 'name', 'version', 'modified_at'],
78             }
79             collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
80             await collectionService.update('uuid', data);
81             expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
82         });
83     });
84
85     describe('deleteFiles', () => {
86         it('should remove no files', async () => {
87             // given
88             const filePaths: string[] = [];
89             const collectionUUID = '';
90
91             // when
92             await collectionService.deleteFiles(collectionUUID, filePaths);
93
94             // then
95             expect(webdavClient.delete).not.toHaveBeenCalled();
96         });
97
98         it('should remove only root files', async () => {
99             // given
100             const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
101             const collectionUUID = '';
102
103             // when
104             await collectionService.deleteFiles(collectionUUID, filePaths);
105
106             // then
107             expect(webdavClient.delete).toHaveBeenCalledTimes(3);
108             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
109             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
110             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
111         });
112
113         it('should remove files with uuid prefix', async () => {
114             // given
115             const filePaths: string[] = ['/root/1'];
116             const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
117
118             // when
119             await collectionService.deleteFiles(collectionUUID, filePaths);
120
121             // then
122             expect(webdavClient.delete).toHaveBeenCalledTimes(1);
123             expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");
124         });
125     });
126 });