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, TreeNodeStatus } 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({
33 status: TreeNodeStatus.INITIAL,
34 })(tree), createTree<CollectionFile | CollectionDirectory>());
36 const collectionPanelFilesTree = collectionPanelFilesReducer(
37 createTree<CollectionPanelFile | CollectionPanelDirectory>(),
38 collectionPanelFilesAction.SET_COLLECTION_FILES(collectionFilesTree));
40 it('SET_COLLECTION_FILES', () => {
41 expect(getNodeValue('Directory 1')(collectionPanelFilesTree)).toEqual({
42 ...createCollectionDirectory({ id: 'Directory 1', name: 'Directory 1', path: '' }),
48 it('TOGGLE_COLLECTION_FILE_COLLAPSE', () => {
49 const newTree = collectionPanelFilesReducer(
50 collectionPanelFilesTree,
51 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_COLLAPSE({ id: 'Directory 3' }));
53 const value = getNodeValue('Directory 3')(newTree)! as CollectionPanelDirectory;
54 expect(value.collapsed).toBe(false);
57 it('TOGGLE_COLLECTION_FILE_SELECTION', () => {
58 const newTree = collectionPanelFilesReducer(
59 collectionPanelFilesTree,
60 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 3' }));
62 const value = getNodeValue('Directory 3')(newTree);
63 expect(value!.selected).toBe(true);
66 it('TOGGLE_COLLECTION_FILE_SELECTION ancestors', () => {
67 const newTree = collectionPanelFilesReducer(
68 collectionPanelFilesTree,
69 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' }));
71 const value = getNodeValue('Directory 1')(newTree);
72 expect(value!.selected).toBe(true);
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);
83 it('TOGGLE_COLLECTION_FILE_SELECTION unselect ancestors', () => {
84 const [newTree] = [collectionPanelFilesTree]
85 .map(tree => collectionPanelFilesReducer(
87 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'Directory 2' })))
88 .map(tree => collectionPanelFilesReducer(
90 collectionPanelFilesAction.TOGGLE_COLLECTION_FILE_SELECTION({ id: 'file1.txt' })));
92 expect(getNodeValue('Directory 2')(newTree)!.selected).toBe(false);
95 it('SELECT_ALL_COLLECTION_FILES', () => {
96 const newTree = collectionPanelFilesReducer(
97 collectionPanelFilesTree,
98 collectionPanelFilesAction.SELECT_ALL_COLLECTION_FILES());
100 mapTreeValues((v: CollectionPanelFile | CollectionPanelDirectory) => {
101 expect(v.selected).toEqual(true);
106 it('SELECT_ALL_COLLECTION_FILES', () => {
107 const [newTree] = [collectionPanelFilesTree]
108 .map(tree => collectionPanelFilesReducer(
110 collectionPanelFilesAction.SELECT_ALL_COLLECTION_FILES()))
111 .map(tree => collectionPanelFilesReducer(
113 collectionPanelFilesAction.UNSELECT_ALL_COLLECTION_FILES()));
115 mapTreeValues((v: CollectionPanelFile | CollectionPanelDirectory) => {
116 expect(v.selected).toEqual(false);