Merge branch 'master'
[arvados-workbench2.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 } 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     })(tree), createTree<CollectionFile | CollectionDirectory>());
31
32     const collectionPanelFilesTree = collectionPanelFilesReducer(
33         createTree<CollectionPanelFile | CollectionPanelDirectory>(),
34         collectionPanelFilesAction.SET_COLLECTION_FILES(collectionFilesTree));
35
36     it('SET_COLLECTION_FILES', () => {
37         expect(getNodeValue('Directory 1')(collectionPanelFilesTree)).toEqual({
38             ...createCollectionDirectory({ id: 'Directory 1', name: 'Directory 1', path: '' }),
39             collapsed: true,
40             selected: false
41         });
42     });
43
44     it('TOGGLE_COLLECTION_FILE_COLLAPSE', () => {
45         const newTree = collectionPanelFilesReducer(
46             collectionPanelFilesTree,
47             collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_COLLAPSE({ id: 'Directory 3' }));
48
49         const value = getNodeValue('Directory 3')(newTree)! as CollectionPanelDirectory;
50         expect(value.collapsed).toBe(false);
51     });
52
53     it('TOGGLE_COLLECTION_FILE_SELECTION', () => {
54         const newTree = collectionPanelFilesReducer(
55             collectionPanelFilesTree,
56             collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 3' }));
57
58         const value = getNodeValue('Directory 3')(newTree);
59         expect(value!.selected).toBe(true);
60     });
61
62     it('TOGGLE_COLLECTION_FILE_SELECTION ancestors', () => {
63         const newTree = collectionPanelFilesReducer(
64             collectionPanelFilesTree,
65             collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' }));
66
67         const value = getNodeValue('Directory 1')(newTree);
68         expect(value!.selected).toBe(true);
69     });
70
71     it('TOGGLE_COLLECTION_FILE_SELECTION descendants', () => {
72         const newTree = collectionPanelFilesReducer(
73             collectionPanelFilesTree,
74             collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' }));
75         expect(getNodeValue('file1.txt')(newTree)!.selected).toBe(true);
76         expect(getNodeValue('file2.txt')(newTree)!.selected).toBe(true);
77     });
78
79     it('TOGGLE_COLLECTION_FILE_SELECTION unselect ancestors', () => {
80         const [newTree] = [collectionPanelFilesTree]
81             .map(tree => collectionPanelFilesReducer(
82                 tree,
83                 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' })))
84             .map(tree => collectionPanelFilesReducer(
85                 tree,
86                 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'file1.txt' })));
87
88         expect(getNodeValue('Directory 2')(newTree)!.selected).toBe(false);
89     });
90
91     it('SELECT_ALL_COLLECTION_FILES', () => {
92         const newTree = collectionPanelFilesReducer(
93             collectionPanelFilesTree,
94             collectionPanelFilesAction.SELECT_ALL_COLLECTION_FILES());
95
96         mapTreeValues((v: CollectionPanelFile | CollectionPanelDirectory) => {
97             expect(v.selected).toEqual(true);
98             return v;
99         })(newTree);
100     });
101
102     it('SELECT_ALL_COLLECTION_FILES', () => {
103         const [newTree] = [collectionPanelFilesTree]
104             .map(tree => collectionPanelFilesReducer(
105                 tree,
106                 collectionPanelFilesAction.SELECT_ALL_COLLECTION_FILES()))
107             .map(tree => collectionPanelFilesReducer(
108                 tree,
109                 collectionPanelFilesAction.UNSELECT_ALL_COLLECTION_FILES()));
110
111         mapTreeValues((v: CollectionPanelFile | CollectionPanelDirectory) => {
112             expect(v.selected).toEqual(false);
113             return v;
114         })(newTree);
115     });
116 });