refs #master Merge branch 'origin/master' into 14007-ts-paths
[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 newTree = treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE({ id: '1' }));
15         expect(newTree).toEqual(tree);
16     });
17
18     it('LOAD_TREE_PICKER_NODE', () => {
19         const tree = createTree<TreePickerNode>();
20         const node = createTreePickerNode({ id: '1', value: '1' });
21         const [newTree] = [tree]
22             .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
23             .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE({ id: '1' })));
24         expect(getNodeValue('1')(newTree)).toEqual({
25             ...createTreePickerNode({ id: '1', value: '1' }),
26             status: TreeItemStatus.PENDING
27         });
28     });
29
30     it('LOAD_TREE_PICKER_NODE_SUCCESS - initial state', () => {
31         const tree = createTree<TreePickerNode>();
32         const subNode = createTreePickerNode({ id: '1.1', value: '1.1' });
33         const newTree = treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [subNode] }));
34         expect(getNodeChildren('')(newTree)).toEqual(['1.1']);
35     });
36
37     it('LOAD_TREE_PICKER_NODE_SUCCESS', () => {
38         const tree = createTree<TreePickerNode>();
39         const node = createTreePickerNode({ id: '1', value: '1' });
40         const subNode = createTreePickerNode({ id: '1.1', value: '1.1' });
41         const [newTree] = [tree]
42             .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
43             .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '1', nodes: [subNode] })));
44         expect(getNodeChildren('1')(newTree)).toEqual(['1.1']);
45         expect(getNodeValue('1')(newTree)).toEqual({
46             ...createTreePickerNode({ id: '1', value: '1' }),
47             status: TreeItemStatus.LOADED
48         });
49     });
50
51     it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - collapsed', () => {
52         const tree = createTree<TreePickerNode>();
53         const node = createTreePickerNode({ id: '1', value: '1' });
54         const [newTree] = [tree]
55             .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
56             .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1' })));
57         expect(getNodeValue('1')(newTree)).toEqual({
58             ...createTreePickerNode({ id: '1', value: '1' }),
59             collapsed: false
60         });
61     });
62
63     it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - expanded', () => {
64         const tree = createTree<TreePickerNode>();
65         const node = createTreePickerNode({ id: '1', value: '1' });
66         const [newTree] = [tree]
67             .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
68             .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1' })))
69             .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1' })));
70         expect(getNodeValue('1')(newTree)).toEqual({
71             ...createTreePickerNode({ id: '1', value: '1' }),
72             collapsed: true
73         });
74     });
75
76     it('TOGGLE_TREE_PICKER_NODE_SELECT - selected', () => {
77         const tree = createTree<TreePickerNode>();
78         const node = createTreePickerNode({ id: '1', value: '1' });
79         const [newTree] = [tree]
80             .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
81             .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ id: '1' })));
82         expect(getNodeValue('1')(newTree)).toEqual({
83             ...createTreePickerNode({ id: '1', value: '1' }),
84             selected: true
85         });
86     });
87
88     it('TOGGLE_TREE_PICKER_NODE_SELECT - not selected', () => {
89         const tree = createTree<TreePickerNode>();
90         const node = createTreePickerNode({ id: '1', value: '1' });
91         const [newTree] = [tree]
92             .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
93             .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ id: '1' })))
94             .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ id: '1' })));
95         expect(getNodeValue('1')(newTree)).toEqual({
96             ...createTreePickerNode({ id: '1', value: '1' }),
97             selected: false
98         });
99     });
100 });