Create colelction-panel-files-reducer tests
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 2 Aug 2018 11:41:31 +0000 (13:41 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 2 Aug 2018 11:41:31 +0000 (13:41 +0200)
Feature #13855

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/store/collection-panel/collection-panel-files/collection-panel-files-reducer.test.ts [new file with mode: 0644]
src/store/collection-panel/collection-panel-files/collections-panel-files-reducer.ts

diff --git a/src/store/collection-panel/collection-panel-files/collection-panel-files-reducer.test.ts b/src/store/collection-panel/collection-panel-files/collection-panel-files-reducer.test.ts
new file mode 100644 (file)
index 0000000..88b7fd0
--- /dev/null
@@ -0,0 +1,116 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { collectionPanelFilesReducer } from "./collections-panel-files-reducer";
+import { collectionPanelFilesAction } from "./collection-panel-files-actions";
+import { CollectionFile, CollectionDirectory, createFile, createDirectory } from "../../../models/collection-file";
+import { createTree, setNode, getNodeValue, mapTreeValues, Tree } from "../../../models/tree";
+import { CollectionPanelFile, CollectionPanelDirectory } from "./collection-panel-files-state";
+
+describe('CollectionPanelFilesReducer', () => {
+
+    const files: Array<CollectionFile | CollectionDirectory> = [
+        createDirectory({ id: 'Directory 1', name: 'Directory 1', parentId: '' }),
+        createDirectory({ id: 'Directory 2', name: 'Directory 2', parentId: 'Directory 1' }),
+        createDirectory({ id: 'Directory 3', name: 'Directory 3', parentId: '' }),
+        createDirectory({ id: 'Directory 4', name: 'Directory 4', parentId: 'Directory 3' }),
+        createFile({ id: 'file1.txt', name: 'file1.txt', parentId: 'Directory 2' }),
+        createFile({ id: 'file2.txt', name: 'file2.txt', parentId: 'Directory 2' }),
+        createFile({ id: 'file3.txt', name: 'file3.txt', parentId: 'Directory 3' }),
+        createFile({ id: 'file4.txt', name: 'file4.txt', parentId: 'Directory 3' }),
+        createFile({ id: 'file5.txt', name: 'file5.txt', parentId: 'Directory 4' }),
+    ];
+
+    const collectionFilesTree = files.reduce((tree, file) => setNode({
+        children: [],
+        id: file.id,
+        parent: file.parentId,
+        value: file
+    })(tree), createTree<CollectionFile | CollectionDirectory>());
+
+    const collectionPanelFilesTree = collectionPanelFilesReducer(
+        createTree<CollectionPanelFile | CollectionPanelDirectory>(),
+        collectionPanelFilesAction.SET_COLLECTION_FILES({ files: collectionFilesTree }));
+
+    it('SET_COLLECTION_FILES', () => {
+        expect(getNodeValue('Directory 1')(collectionPanelFilesTree)).toEqual({
+            ...createDirectory({ id: 'Directory 1', name: 'Directory 1', parentId: '' }),
+            collapsed: true,
+            selected: false
+        });
+    });
+
+    it('TOGGLE_COLLECTION_FILE_COLLAPSE', () => {
+        const newTree = collectionPanelFilesReducer(
+            collectionPanelFilesTree,
+            collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_COLLAPSE({ id: 'Directory 3' }));
+
+        const value = getNodeValue('Directory 3')(newTree)! as CollectionPanelDirectory;
+        expect(value.collapsed).toBe(false);
+    });
+
+    it('TOGGLE_COLLECTION_FILE_SELECTION', () => {
+        const newTree = collectionPanelFilesReducer(
+            collectionPanelFilesTree,
+            collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 3' }));
+
+        const value = getNodeValue('Directory 3')(newTree);
+        expect(value!.selected).toBe(true);
+    });
+
+    it('TOGGLE_COLLECTION_FILE_SELECTION ancestors', () => {
+        const newTree = collectionPanelFilesReducer(
+            collectionPanelFilesTree,
+            collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' }));
+
+        const value = getNodeValue('Directory 1')(newTree);
+        expect(value!.selected).toBe(true);
+    });
+
+    it('TOGGLE_COLLECTION_FILE_SELECTION descendants', () => {
+        const newTree = collectionPanelFilesReducer(
+            collectionPanelFilesTree,
+            collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' }));
+        expect(getNodeValue('file1.txt')(newTree)!.selected).toBe(true);
+        expect(getNodeValue('file2.txt')(newTree)!.selected).toBe(true);
+    });
+
+    it('TOGGLE_COLLECTION_FILE_SELECTION unselect ancestors', () => {
+        const [newTree] = [collectionPanelFilesTree]
+            .map(tree => collectionPanelFilesReducer(
+                tree,
+                collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' })))
+            .map(tree => collectionPanelFilesReducer(
+                tree,
+                collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'file1.txt' })));
+
+        expect(getNodeValue('Directory 2')(newTree)!.selected).toBe(false);
+    });
+
+    it('SELECT_ALL_COLLECTION_FILES', () => {
+        const newTree = collectionPanelFilesReducer(
+            collectionPanelFilesTree,
+            collectionPanelFilesAction.SELECT_ALL_COLLECTION_FILES());
+
+        mapTreeValues((v: CollectionPanelFile | CollectionPanelDirectory) => {
+            expect(v.selected).toEqual(true);
+            return v;
+        })(newTree);
+    });
+
+    it('SELECT_ALL_COLLECTION_FILES', () => {
+        const [newTree] = [collectionPanelFilesTree]
+            .map(tree => collectionPanelFilesReducer(
+                tree,
+                collectionPanelFilesAction.SELECT_ALL_COLLECTION_FILES()))
+            .map(tree => collectionPanelFilesReducer(
+                tree,
+                collectionPanelFilesAction.UNSELECT_ALL_COLLECTION_FILES()));
+
+        mapTreeValues((v: CollectionPanelFile | CollectionPanelDirectory) => {
+            expect(v.selected).toEqual(false);
+            return v;
+        })(newTree);
+    });
+});
index f6503ccb569ebb704fa9379a524e88817bf62a3c..c27000e2227de7f941991b2ca7aaf77f25fdc3ac 100644 (file)
@@ -27,7 +27,7 @@ export const collectionPanelFilesReducer = (state: CollectionPanelFilesState = c
             mapTreeValues(v => ({ ...v, selected: false }))(state),
 
         default: () => state
-    });
+    }) as CollectionPanelFilesState;
 };
 
 const toggleCollapse = (id: string) => (tree: CollectionPanelFilesState) =>