c0aa85f1d3ab3e8df9912b993c978ef67d8c5834
[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('update', () => {
34         it('should call put selecting updated fields + others', async () => {
35             serverApi.put = jest.fn(() => Promise.resolve({ data: {} }));
36             const data: Partial<CollectionResource> = {
37                 name: 'foo',
38             };
39             const expected = {
40                 collection: {
41                     ...data,
42                     preserve_version: true,
43                 },
44                 select: ['uuid', 'name', 'version', 'modified_at'],
45             }
46             collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
47             await collectionService.update('uuid', data);
48             expect(serverApi.put).toHaveBeenCalledWith('/collections/uuid', expected);
49         });
50     });
51
52     describe('deleteFiles', () => {
53         it('should remove no files', async () => {
54             // given
55             const filePaths: string[] = [];
56             const collectionUUID = '';
57
58             // when
59             await collectionService.deleteFiles(collectionUUID, filePaths);
60
61             // then
62             expect(webdavClient.delete).not.toHaveBeenCalled();
63         });
64
65         it('should remove only root files', async () => {
66             // given
67             const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
68             const collectionUUID = '';
69
70             // when
71             await collectionService.deleteFiles(collectionUUID, filePaths);
72
73             // then
74             expect(webdavClient.delete).toHaveBeenCalledTimes(3);
75             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
76             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
77             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
78         });
79
80         it('should remove files with uuid prefix', async () => {
81             // given
82             const filePaths: string[] = ['/root/1'];
83             const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
84
85             // when
86             await collectionService.deleteFiles(collectionUUID, filePaths);
87
88             // then
89             expect(webdavClient.delete).toHaveBeenCalledTimes(1);
90             expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");
91         });
92     });
93 });