cr-changes
[arvados-workbench2.git] / 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, getNodeValue, getNodeChildren } from "~/models/tree";
6 import { TreePickerNode, createTreePickerNode } from "./tree-picker";
7 import { treePickerReducer } from "./tree-picker-reducer";
8 import { treePickerActions } from "./tree-picker-actions";
9 import { TreeItemStatus } from "~/components/tree/tree";
10
11 describe('TreePickerReducer', () => {
12     it('LOAD_TREE_PICKER_NODE - initial state', () => {
13         const tree = createTree<TreePickerNode>();
14         const newState = treePickerReducer({}, treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId: '1', pickerId: "projects" }));
15         expect(newState).toEqual({ 'projects': tree });
16     });
17
18     it('LOAD_TREE_PICKER_NODE', () => {
19         const node = createTreePickerNode({ nodeId: '1', value: '1' });
20         const [newState] = [{
21             projects: createTree<TreePickerNode>()
22         }]
23             .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ nodeId: '', nodes: [node], pickerId: "projects" })))
24             .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE({ nodeId: '1', pickerId: "projects" })));
25
26         expect(getNodeValue('1')(newState.projects)).toEqual({
27             ...createTreePickerNode({ nodeId: '1', value: '1' }),
28             status: TreeItemStatus.PENDING
29         });
30     });
31
32     it('LOAD_TREE_PICKER_NODE_SUCCESS - initial state', () => {
33         const subNode = createTreePickerNode({ nodeId: '1.1', value: '1.1' });
34         const newState = treePickerReducer({}, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ nodeId: '', nodes: [subNode], pickerId: "projects" }));
35         expect(getNodeChildren('')(newState.projects)).toEqual(['1.1']);
36     });
37
38     it('LOAD_TREE_PICKER_NODE_SUCCESS', () => {
39         const node = createTreePickerNode({ nodeId: '1', value: '1' });
40         const subNode = createTreePickerNode({ nodeId: '1.1', value: '1.1' });
41         const [newState] = [{
42             projects: createTree<TreePickerNode>()
43         }]
44             .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ nodeId: '', nodes: [node], pickerId: "projects" })))
45             .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ nodeId: '1', nodes: [subNode], pickerId: "projects" })));
46         expect(getNodeChildren('1')(newState.projects)).toEqual(['1.1']);
47         expect(getNodeValue('1')(newState.projects)).toEqual({
48             ...createTreePickerNode({ nodeId: '1', value: '1' }),
49             status: TreeItemStatus.LOADED
50         });
51     });
52
53     it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - collapsed', () => {
54         const node = createTreePickerNode({ nodeId: '1', value: '1' });
55         const [newState] = [{
56             projects: createTree<TreePickerNode>()
57         }]
58             .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ nodeId: '', nodes: [node], pickerId: "projects" })))
59             .map(state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId: '1', pickerId: "projects" })));
60         expect(getNodeValue('1')(newState.projects)).toEqual({
61             ...createTreePickerNode({ nodeId: '1', value: '1' }),
62             collapsed: false
63         });
64     });
65
66     it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - expanded', () => {
67         const node = createTreePickerNode({ nodeId: '1', value: '1' });
68         const [newState] = [{
69             projects: createTree<TreePickerNode>()
70         }]
71             .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ nodeId: '', nodes: [node], pickerId: "projects" })))
72             .map(state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId: '1', pickerId: "projects" })))
73             .map(state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ nodeId: '1', pickerId: "projects" })));
74         expect(getNodeValue('1')(newState.projects)).toEqual({
75             ...createTreePickerNode({ nodeId: '1', value: '1' }),
76             collapsed: true
77         });
78     });
79
80     it('TOGGLE_TREE_PICKER_NODE_SELECT - selected', () => {
81         const node = createTreePickerNode({ nodeId: '1', value: '1' });
82         const [newState] = [{
83             projects: createTree<TreePickerNode>()
84         }]
85             .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ nodeId: '', nodes: [node], pickerId: "projects" })))
86             .map(state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId: '1', pickerId: "projects" })));
87         expect(getNodeValue('1')(newState.projects)).toEqual({
88             ...createTreePickerNode({ nodeId: '1', value: '1' }),
89             selected: true
90         });
91     });
92
93     it('TOGGLE_TREE_PICKER_NODE_SELECT - not selected', () => {
94         const node = createTreePickerNode({ nodeId: '1', value: '1' });
95         const [newState] = [{
96             projects: createTree<TreePickerNode>()
97         }]
98             .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ nodeId: '', nodes: [node], pickerId: "projects" })))
99             .map(state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId: '1', pickerId: "projects" })))
100             .map(state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ nodeId: '1', pickerId: "projects" })));
101         expect(getNodeValue('1')(newState.projects)).toEqual({
102             ...createTreePickerNode({ nodeId: '1', value: '1' }),
103             selected: false
104         });
105     });
106 });