Merge branch '14841-link-workbench1' refs #14841 refs #14720
[arvados.git] / src / store / collection-panel / collection-panel-files / collection-panel-files-reducer.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { collectionPanelFilesReducer } from "./collection-panel-files-reducer";
6 import { collectionPanelFilesAction } from "./collection-panel-files-actions";
7 import { CollectionFile, CollectionDirectory, createCollectionFile, createCollectionDirectory } from "~/models/collection-file";
8 import { createTree, setNode, getNodeValue, mapTreeValues, TreeNodeStatus } from "~/models/tree";
9 import { CollectionPanelFile, CollectionPanelDirectory } from "./collection-panel-files-state";
10
11 describe('CollectionPanelFilesReducer', () => {
12
13     const files: Array<CollectionFile | CollectionDirectory> = [
14         createCollectionDirectory({ id: 'Directory 1', name: 'Directory 1', path: '' }),
15         createCollectionDirectory({ id: 'Directory 2', name: 'Directory 2', path: 'Directory 1' }),
16         createCollectionDirectory({ id: 'Directory 3', name: 'Directory 3', path: '' }),
17         createCollectionDirectory({ id: 'Directory 4', name: 'Directory 4', path: 'Directory 3' }),
18         createCollectionFile({ id: 'file1.txt', name: 'file1.txt', path: 'Directory 2' }),
19         createCollectionFile({ id: 'file2.txt', name: 'file2.txt', path: 'Directory 2' }),
20         createCollectionFile({ id: 'file3.txt', name: 'file3.txt', path: 'Directory 3' }),
21         createCollectionFile({ id: 'file4.txt', name: 'file4.txt', path: 'Directory 3' }),
22         createCollectionFile({ id: 'file5.txt', name: 'file5.txt', path: 'Directory 4' }),
23     ];
24
25     const collectionFilesTree = files.reduce((tree, file) => setNode({
26         children: [],
27         id: file.id,
28         parent: file.path,
29         value: file,
30         active: false,
31         selected: false,
32         expanded: false,
33         status: TreeNodeStatus.INITIAL,
34     })(tree), createTree<CollectionFile | CollectionDirectory>());
35
36     const collectionPanelFilesTree = collectionPanelFilesReducer(
37         createTree<CollectionPanelFile | CollectionPanelDirectory>(),
38         collectionPanelFilesAction.SET_COLLECTION_FILES(collectionFilesTree));
39
40     it('SET_COLLECTION_FILES', () => {
41         expect(getNodeValue('Directory 1')(collectionPanelFilesTree)).toEqual({
42             ...createCollectionDirectory({ id: 'Directory 1', name: 'Directory 1', path: '' }),
43             collapsed: true,
44             selected: false
45         });
46     });
47
48     it('TOGGLE_COLLECTION_FILE_COLLAPSE', () => {
49         const newTree = collectionPanelFilesReducer(
50             collectionPanelFilesTree,
51             collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_COLLAPSE({ id: 'Directory 3' }));
52
53         const value = getNodeValue('Directory 3')(newTree)! as CollectionPanelDirectory;
54         expect(value.collapsed).toBe(false);
55     });
56
57     it('TOGGLE_COLLECTION_FILE_SELECTION', () => {
58         const newTree = collectionPanelFilesReducer(
59             collectionPanelFilesTree,
60             collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 3' }));
61
62         const value = getNodeValue('Directory 3')(newTree);
63         expect(value!.selected).toBe(true);
64     });
65
66     it('TOGGLE_COLLECTION_FILE_SELECTION ancestors', () => {
67         const newTree = collectionPanelFilesReducer(
68             collectionPanelFilesTree,
69             collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' }));
70
71         const value = getNodeValue('Directory 1')(newTree);
72         expect(value!.selected).toBe(true);
73     });
74
75     it('TOGGLE_COLLECTION_FILE_SELECTION descendants', () => {
76         const newTree = collectionPanelFilesReducer(
77             collectionPanelFilesTree,
78             collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' }));
79         expect(getNodeValue('file1.txt')(newTree)!.selected).toBe(true);
80         expect(getNodeValue('file2.txt')(newTree)!.selected).toBe(true);
81     });
82
83     it('TOGGLE_COLLECTION_FILE_SELECTION unselect ancestors', () => {
84         const [newTree] = [collectionPanelFilesTree]
85             .map(tree => collectionPanelFilesReducer(
86                 tree,
87                 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' })))
88             .map(tree => collectionPanelFilesReducer(
89                 tree,
90                 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'file1.txt' })));
91
92         expect(getNodeValue('Directory 2')(newTree)!.selected).toBe(false);
93     });
94
95     it('SELECT_ALL_COLLECTION_FILES', () => {
96         const newTree = collectionPanelFilesReducer(
97             collectionPanelFilesTree,
98             collectionPanelFilesAction.SELECT_ALL_COLLECTION_FILES());
99
100         mapTreeValues((v: CollectionPanelFile | CollectionPanelDirectory) => {
101             expect(v.selected).toEqual(true);
102             return v;
103         })(newTree);
104     });
105
106     it('SELECT_ALL_COLLECTION_FILES', () => {
107         const [newTree] = [collectionPanelFilesTree]
108             .map(tree => collectionPanelFilesReducer(
109                 tree,
110                 collectionPanelFilesAction.SELECT_ALL_COLLECTION_FILES()))
111             .map(tree => collectionPanelFilesReducer(
112                 tree,
113                 collectionPanelFilesAction.UNSELECT_ALL_COLLECTION_FILES()));
114
115         mapTreeValues((v: CollectionPanelFile | CollectionPanelDirectory) => {
116             expect(v.selected).toEqual(false);
117             return v;
118         })(newTree);
119     });
120 });