21720:
[arvados.git] / services / workbench2 / src / store / tree-picker / tree-picker-reducer.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { createTree, getNodeChildrenIds, getNode, TreeNodeStatus } from 'models/tree';
6 import { pipe } from 'lodash/fp';
7 import { treePickerReducer } from "./tree-picker-reducer";
8 import { treePickerActions } from "./tree-picker-actions";
9 import { TreePicker } from './tree-picker';
10 import { initTreeNode } from 'models/tree';
11
12 describe('TreePickerReducer', () => {
13     it('LOAD_TREE_PICKER_NODE - initial state', () => {
14         const tree = createTree<{}>();
15         const newState = treePickerReducer({}, treePickerActions.LOAD_TREE_PICKER_NODE({ id: '1', pickerId: "projects" }));
16         expect(newState).toEqual({ 'projects': tree });
17     });
18
19     it('LOAD_TREE_PICKER_NODE', () => {
20         const node = initTreeNode({ id: '1', value: '1' });
21         const newState = pipe(
22             (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
23             state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE({ id: '1', pickerId: "projects" }))
24         )({ projects: createTree<{}>() });
25
26         expect(getNode('1')(newState.projects)).toEqual({
27             ...initTreeNode({ id: '1', value: '1' }),
28             status: TreeNodeStatus.PENDING
29         });
30     });
31
32     it('LOAD_TREE_PICKER_NODE_SUCCESS - initial state', () => {
33         const subNode = initTreeNode({ id: '1.1', value: '1.1' });
34         const newState = treePickerReducer({}, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [subNode], pickerId: "projects" }));
35         expect(getNodeChildrenIds('')(newState.projects)).toEqual(['1.1']);
36     });
37
38     it('LOAD_TREE_PICKER_NODE_SUCCESS', () => {
39         const node = initTreeNode({ id: '1', value: '1' });
40         const subNode = initTreeNode({ id: '1.1', value: '1.1' });
41         const newState = pipe(
42             (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
43             state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '1', nodes: [subNode], pickerId: "projects" }))
44         )({ projects: createTree<{}>() });
45         expect(getNodeChildrenIds('1')(newState.projects)).toEqual(['1.1']);
46         expect(getNode('1')(newState.projects)).toEqual({
47             ...initTreeNode({ id: '1', value: '1' }),
48             children: ['1.1'],
49             status: TreeNodeStatus.LOADED
50         });
51     });
52
53     it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - expanded', () => {
54         const node = initTreeNode({ id: '1', value: '1' });
55         const newState = pipe(
56             (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
57             state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1', pickerId: "projects" }))
58         )({ projects: createTree<{}>() });
59         expect(getNode('1')(newState.projects)).toEqual({
60             ...initTreeNode({ id: '1', value: '1' }),
61             expanded: true
62         });
63     });
64
65     it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - expanded', () => {
66         const node = initTreeNode({ id: '1', value: '1' });
67         const newState = pipe(
68             (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
69             state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1', pickerId: "projects" })),
70             state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1', pickerId: "projects" })),
71         )({ projects: createTree<{}>() });
72         expect(getNode('1')(newState.projects)).toEqual({
73             ...initTreeNode({ id: '1', value: '1' }),
74             expanded: false
75         });
76     });
77
78     it('ACTIVATE_TREE_PICKER_NODE', () => {
79         const node = initTreeNode({ id: '1', value: '1' });
80         const newState = pipe(
81             (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
82             state => treePickerReducer(state, treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: '1', pickerId: "projects" })),
83         )({ projects: createTree<{}>() });
84         expect(getNode('1')(newState.projects)).toEqual({
85             ...initTreeNode({ id: '1', value: '1' }),
86             active: true
87         });
88     });
89
90     it('TOGGLE_TREE_PICKER_NODE_SELECTION', () => {
91         const node = initTreeNode({ id: '1', value: '1' });
92         const subNode = initTreeNode({ id: '1.1', value: '1.1' });
93         const newState = pipe(
94             (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
95             state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '1', nodes: [subNode], pickerId: "projects" })),
96             state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id: '1.1', pickerId: "projects", cascade: true })),
97         )({ projects: createTree<{}>() });
98         expect(getNode('1')(newState.projects)).toEqual({
99             ...initTreeNode({ id: '1', value: '1' }),
100             selected: true,
101             children: ['1.1'],
102             status: TreeNodeStatus.LOADED,
103         });
104     });
105 });