Merge branch '17016-delete-single-file-deletes-whole-collection'
[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 { AxiosInstance } from 'axios';
6 import { WebDAV } from '~/common/webdav';
7 import { ApiActions } from '../api/api-actions';
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;
14     let webdavClient: any;
15     let authService;
16     let actions;
17
18     beforeEach(() => {
19         serverApi = {} as AxiosInstance;
20         webdavClient = {
21             delete: jest.fn(),
22         } as any;
23         authService = {} as AuthService;
24         actions = {} as ApiActions;
25         collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
26     });
27
28     describe('deleteFiles', () => {
29         it('should remove no files', async () => {
30             // given
31             const filePaths: string[] = [];
32             const collectionUUID = '';
33
34             // when
35             await collectionService.deleteFiles(collectionUUID, filePaths);
36
37             // then
38             expect(webdavClient.delete).not.toHaveBeenCalled();
39         });
40
41         it('should remove only root files', async () => {
42             // given
43             const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
44             const collectionUUID = '';
45
46             // when
47             await collectionService.deleteFiles(collectionUUID, filePaths);
48
49             // then
50             expect(webdavClient.delete).toHaveBeenCalledTimes(3);
51             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
52             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
53             expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
54         });
55
56         it('should remove files with uuid prefix', async () => {
57             // given
58             const filePaths: string[] = ['/root/1'];
59             const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
60
61             // when
62             await collectionService.deleteFiles(collectionUUID, filePaths);
63
64             // then
65             expect(webdavClient.delete).toHaveBeenCalledTimes(1);
66             expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");
67         });
68     });
69 });