1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
12 describe('TreePickerReducer', () => {
13 it('LOAD_TREE_PICKER_NODE - initial state', () => {
14 const tree = createTree<TreePickerNode>();
15 const newTree = treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE({ id: '1' }));
16 expect(newTree).toEqual(tree);
19 it('LOAD_TREE_PICKER_NODE', () => {
20 const tree = createTree<TreePickerNode>();
21 const node = createTreePickerNode({ id: '1', value: '1' });
22 const [newTree] = [tree]
23 .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
24 .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE({ id: '1' })));
25 expect(getNodeValue('1')(newTree)).toEqual({
26 ...createTreePickerNode({ id: '1', value: '1' }),
27 status: TreeItemStatus.PENDING
31 it('LOAD_TREE_PICKER_NODE_SUCCESS - initial state', () => {
32 const tree = createTree<TreePickerNode>();
33 const subNode = createTreePickerNode({ id: '1.1', value: '1.1' });
34 const newTree = treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [subNode] }));
35 expect(getNodeChildren('')(newTree)).toEqual(['1.1']);
38 it('LOAD_TREE_PICKER_NODE_SUCCESS', () => {
39 const tree = createTree<TreePickerNode>();
40 const node = createTreePickerNode({ id: '1', value: '1' });
41 const subNode = createTreePickerNode({ id: '1.1', value: '1.1' });
42 const [newTree] = [tree]
43 .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
44 .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '1', nodes: [subNode] })));
45 expect(getNodeChildren('1')(newTree)).toEqual(['1.1']);
46 expect(getNodeValue('1')(newTree)).toEqual({
47 ...createTreePickerNode({ id: '1', value: '1' }),
48 status: TreeItemStatus.LOADED
52 it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - collapsed', () => {
53 const tree = createTree<TreePickerNode>();
54 const node = createTreePickerNode({ id: '1', value: '1' });
55 const [newTree] = [tree]
56 .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
57 .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1' })));
58 expect(getNodeValue('1')(newTree)).toEqual({
59 ...createTreePickerNode({ id: '1', value: '1' }),
64 it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - expanded', () => {
65 const tree = createTree<TreePickerNode>();
66 const node = createTreePickerNode({ id: '1', value: '1' });
67 const [newTree] = [tree]
68 .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
69 .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1' })))
70 .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1' })));
71 expect(getNodeValue('1')(newTree)).toEqual({
72 ...createTreePickerNode({ id: '1', value: '1' }),
77 it('TOGGLE_TREE_PICKER_NODE_SELECT - selected', () => {
78 const tree = createTree<TreePickerNode>();
79 const node = createTreePickerNode({ id: '1', value: '1' });
80 const [newTree] = [tree]
81 .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
82 .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ id: '1' })));
83 expect(getNodeValue('1')(newTree)).toEqual({
84 ...createTreePickerNode({ id: '1', value: '1' }),
89 it('TOGGLE_TREE_PICKER_NODE_SELECT - not selected', () => {
90 const tree = createTree<TreePickerNode>();
91 const node = createTreePickerNode({ id: '1', value: '1' });
92 const [newTree] = [tree]
93 .map(tree => treePickerReducer(tree, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node] })))
94 .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ id: '1' })))
95 .map(tree => treePickerReducer(tree, treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECT({ id: '1' })));
96 expect(getNodeValue('1')(newTree)).toEqual({
97 ...createTreePickerNode({ id: '1', value: '1' }),