1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { createTree, getNodeValue, getNodeChildrenIds } 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";
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 });
18 it('LOAD_TREE_PICKER_NODE', () => {
19 const node = createTreePickerNode({ nodeId: '1', value: '1' });
21 projects: createTree<TreePickerNode>()
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" })));
26 expect(getNodeValue('1')(newState.projects)).toEqual({
27 ...createTreePickerNode({ nodeId: '1', value: '1' }),
28 status: TreeItemStatus.PENDING
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(getNodeChildrenIds('')(newState.projects)).toEqual(['1.1']);
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' });
42 projects: createTree<TreePickerNode>()
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(getNodeChildrenIds('1')(newState.projects)).toEqual(['1.1']);
47 expect(getNodeValue('1')(newState.projects)).toEqual({
48 ...createTreePickerNode({ nodeId: '1', value: '1' }),
49 status: TreeItemStatus.LOADED
53 it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - collapsed', () => {
54 const node = createTreePickerNode({ nodeId: '1', value: '1' });
56 projects: createTree<TreePickerNode>()
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' }),
66 it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - expanded', () => {
67 const node = createTreePickerNode({ nodeId: '1', value: '1' });
69 projects: createTree<TreePickerNode>()
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' }),
80 it('TOGGLE_TREE_PICKER_NODE_SELECT - selected', () => {
81 const node = createTreePickerNode({ nodeId: '1', value: '1' });
83 projects: createTree<TreePickerNode>()
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' }),
93 it('TOGGLE_TREE_PICKER_NODE_SELECT - not selected', () => {
94 const node = createTreePickerNode({ nodeId: '1', value: '1' });
96 projects: createTree<TreePickerNode>()
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' }),