17016: Added more unit tests 17016-delete-single-file-deletes-whole-collection
authorDaniel Kutyła <daniel.kutyla@contractors.roche.com>
Fri, 8 Jan 2021 16:37:32 +0000 (17:37 +0100)
committerDaniel Kutyła <daniel.kutyla@contractors.roche.com>
Fri, 8 Jan 2021 16:37:32 +0000 (17:37 +0100)
Arvados-DCO-1.1-Signed-off-by: Daniel Kutyła <daniel.kutyla@contractors.roche.com>

src/services/collection-service/collection-service.test.ts [new file with mode: 0644]

diff --git a/src/services/collection-service/collection-service.test.ts b/src/services/collection-service/collection-service.test.ts
new file mode 100644 (file)
index 0000000..19ac749
--- /dev/null
@@ -0,0 +1,69 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { AxiosInstance } from 'axios';
+import { WebDAV } from '~/common/webdav';
+import { ApiActions } from '../api/api-actions';
+import { AuthService } from '../auth-service/auth-service';
+import { CollectionService } from './collection-service';
+
+describe('collection-service', () => {
+    let collectionService: CollectionService;
+    let serverApi;
+    let webdavClient: any;
+    let authService;
+    let actions;
+
+    beforeEach(() => {
+        serverApi = {} as AxiosInstance;
+        webdavClient = {
+            delete: jest.fn(),
+        } as any;
+        authService = {} as AuthService;
+        actions = {} as ApiActions;
+        collectionService = new CollectionService(serverApi, webdavClient, authService, actions);
+    });
+
+    describe('deleteFiles', () => {
+        it('should remove no files', async () => {
+            // given
+            const filePaths: string[] = [];
+            const collectionUUID = '';
+
+            // when
+            await collectionService.deleteFiles(collectionUUID, filePaths);
+
+            // then
+            expect(webdavClient.delete).not.toHaveBeenCalled();
+        });
+
+        it('should remove only root files', async () => {
+            // given
+            const filePaths: string[] = ['/root/1', '/root/1/100', '/root/1/100/test.txt', '/root/2', '/root/2/200', '/root/3/300/test.txt'];
+            const collectionUUID = '';
+
+            // when
+            await collectionService.deleteFiles(collectionUUID, filePaths);
+
+            // then
+            expect(webdavClient.delete).toHaveBeenCalledTimes(3);
+            expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/3/300/test.txt");
+            expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/2");
+            expect(webdavClient.delete).toHaveBeenCalledWith("c=/root/1");
+        });
+
+        it('should remove files with uuid prefix', async () => {
+            // given
+            const filePaths: string[] = ['/root/1'];
+            const collectionUUID = 'zzzzz-tpzed-5o5tg0l9a57gxxx';
+
+            // when
+            await collectionService.deleteFiles(collectionUUID, filePaths);
+
+            // then
+            expect(webdavClient.delete).toHaveBeenCalledTimes(1);
+            expect(webdavClient.delete).toHaveBeenCalledWith("c=zzzzz-tpzed-5o5tg0l9a57gxxx/root/1");
+        });
+    });
+});
\ No newline at end of file