1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
11 describe('CollectionPanelFilesReducer', () => {
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' }),
25 const collectionFilesTree = files.reduce((tree, file) => setNode({
30 })(tree), createTree<CollectionFile | CollectionDirectory>());
32 const collectionPanelFilesTree = collectionPanelFilesReducer(
33 createTree<CollectionPanelFile | CollectionPanelDirectory>(),
34 collectionPanelFilesAction.SET_COLLECTION_FILES(collectionFilesTree));
36 it('SET_COLLECTION_FILES', () => {
37 expect(getNodeValue('Directory 1')(collectionPanelFilesTree)).toEqual({
38 ...createCollectionDirectory({ id: 'Directory 1', name: 'Directory 1', path: '' }),
44 it('TOGGLE_COLLECTION_FILE_COLLAPSE', () => {
45 const newTree = collectionPanelFilesReducer(
46 collectionPanelFilesTree,
47 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_COLLAPSE({ id: 'Directory 3' }));
49 const value = getNodeValue('Directory 3')(newTree)! as CollectionPanelDirectory;
50 expect(value.collapsed).toBe(false);
53 it('TOGGLE_COLLECTION_FILE_SELECTION', () => {
54 const newTree = collectionPanelFilesReducer(
55 collectionPanelFilesTree,
56 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 3' }));
58 const value = getNodeValue('Directory 3')(newTree);
59 expect(value!.selected).toBe(true);
62 it('TOGGLE_COLLECTION_FILE_SELECTION ancestors', () => {
63 const newTree = collectionPanelFilesReducer(
64 collectionPanelFilesTree,
65 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' }));
67 const value = getNodeValue('Directory 1')(newTree);
68 expect(value!.selected).toBe(true);
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);
79 it('TOGGLE_COLLECTION_FILE_SELECTION unselect ancestors', () => {
80 const [newTree] = [collectionPanelFilesTree]
81 .map(tree => collectionPanelFilesReducer(
83 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' })))
84 .map(tree => collectionPanelFilesReducer(
86 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'file1.txt' })));
88 expect(getNodeValue('Directory 2')(newTree)!.selected).toBe(false);
91 it('SELECT_ALL_COLLECTION_FILES', () => {
92 const newTree = collectionPanelFilesReducer(
93 collectionPanelFilesTree,
94 collectionPanelFilesAction.SELECT_ALL_COLLECTION_FILES());
96 mapTreeValues((v: CollectionPanelFile | CollectionPanelDirectory) => {
97 expect(v.selected).toEqual(true);
102 it('SELECT_ALL_COLLECTION_FILES', () => {
103 const [newTree] = [collectionPanelFilesTree]
104 .map(tree => collectionPanelFilesReducer(
106 collectionPanelFilesAction.SELECT_ALL_COLLECTION_FILES()))
107 .map(tree => collectionPanelFilesReducer(
109 collectionPanelFilesAction.UNSELECT_ALL_COLLECTION_FILES()));
111 mapTreeValues((v: CollectionPanelFile | CollectionPanelDirectory) => {
112 expect(v.selected).toEqual(false);