18484: Makes a list request on collectionService.get().
[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         } as any;
25         authService = {} as AuthService;
26         actions = {
27             progressFn: jest.fn(),
28         } as any;
29         collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
30         collectionService.update = jest.fn();
31     });
32
33     describe('get', () => {
34         it('should make a list request with uuid filtering', async () => {
35             serverApi.get = jest.fn(() => Promise.resolve(
36                 { data: { items: [{}] } }
37             ));
38             const uuid = 'zzzzz-4zz18-0123456789abcde'
39             await collectionService.get(uuid);
40             expect(serverApi.get).toHaveBeenCalledWith(
41                 '/collections', {
42                     params: {
43                         filters: `[["uuid","=","zzzzz-4zz18-0123456789abcde"]]`,
44                         order: undefined
45                     },
46                 }
47             );
48         });
49     });
50
51     describe('update', () => {
52         it('should call put selecting updated fields + others', async () => {
53             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
54             const data: Partial<CollectionResource> = {
55                 name: 'foo',
56             };
57             const expected = {
58                 collection: {
59                     ...data,
60                     preserve_version: true,
61                 },
62                 select: ['uuid', 'name', 'version', 'modified_at'],
63             }
64             collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
65             await collectionService.update('uuid', data);
66             expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
67         });
68     });
69
70     describe('deleteFiles', () => {
71         it('should remove no files', async () => {
72             // given
73             const filePaths: string[] = [];
74             const collectionUUID = '';
75
76             // when
77             await collectionService.deleteFiles(collectionUUID, filePaths);
78
79             // then
80             expect(webdavClient.delete).not.toHaveBeenCalled();
81         });
82
83         it('should remove only root files', async () => {
84             // given
85             const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
86             const collectionUUID = '';
87
88             // when
89             await collectionService.deleteFiles(collectionUUID, filePaths);
90
91             // then
92             expect(webdavClient.delete).toHaveBeenCalledTimes(3);
93             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
94             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
95             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
96         });
97
98         it('should remove files with uuid prefix', async () => {
99             // given
100             const filePaths: string[] = ['/root/1'];
101             const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
102
103             // when
104             await collectionService.deleteFiles(collectionUUID, filePaths);
105
106             // then
107             expect(webdavClient.delete).toHaveBeenCalledTimes(1);
108             expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");
109         });
110     });
111 });