Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / store / tree-picker / tree-picker-reducer.test.ts
index 56a3ba5d29f602815484efb01a66d2a76dda6ee5..2a5229ca5b567707665c9c189452370fcdda221e 100644 (file)
@@ -2,11 +2,12 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { createTree, getNodeValue, getNodeChildrenIds } from "~/models/tree";
+import { createTree, getNodeChildrenIds, getNode, TreeNodeStatus } from 'models/tree';
+import { pipe } from 'lodash/fp';
 import { treePickerReducer } from "./tree-picker-reducer";
 import { treePickerActions } from "./tree-picker-actions";
-import { TreeItemStatus } from "~/components/tree/tree";
-import { initTreeNode } from '~/models/tree';
+import { TreePicker } from './tree-picker';
+import { initTreeNode } from 'models/tree';
 
 describe('TreePickerReducer', () => {
     it('LOAD_TREE_PICKER_NODE - initial state', () => {
@@ -17,15 +18,14 @@ describe('TreePickerReducer', () => {
 
     it('LOAD_TREE_PICKER_NODE', () => {
         const node = initTreeNode({ id: '1', value: '1' });
-        const [newState] = [{
-            projects: createTree<{}>()
-        }]
-            .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })))
-            .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE({ id: '1', pickerId: "projects" })));
+        const newState = pipe(
+            (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
+            state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE({ id: '1', pickerId: "projects" }))
+        )({ projects: createTree<{}>() });
 
-        expect(getNodeValue('1')(newState.projects)).toEqual({
+        expect(getNode('1')(newState.projects)).toEqual({
             ...initTreeNode({ id: '1', value: '1' }),
-            status: TreeItemStatus.PENDING
+            status: TreeNodeStatus.PENDING
         });
     });
 
@@ -38,69 +38,68 @@ describe('TreePickerReducer', () => {
     it('LOAD_TREE_PICKER_NODE_SUCCESS', () => {
         const node = initTreeNode({ id: '1', value: '1' });
         const subNode = initTreeNode({ id: '1.1', value: '1.1' });
-        const [newState] = [{
-            projects: createTree<{}>()
-        }]
-            .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })))
-            .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '1', nodes: [subNode], pickerId: "projects" })));
+        const newState = pipe(
+            (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
+            state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '1', nodes: [subNode], pickerId: "projects" }))
+        )({ projects: createTree<{}>() });
         expect(getNodeChildrenIds('1')(newState.projects)).toEqual(['1.1']);
-        expect(getNodeValue('1')(newState.projects)).toEqual({
+        expect(getNode('1')(newState.projects)).toEqual({
             ...initTreeNode({ id: '1', value: '1' }),
-            status: TreeItemStatus.LOADED
+            children: ['1.1'],
+            status: TreeNodeStatus.LOADED
         });
     });
 
-    it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - collapsed', () => {
+    it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - expanded', () => {
         const node = initTreeNode({ id: '1', value: '1' });
-        const [newState] = [{
-            projects: createTree<{}>()
-        }]
-            .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })))
-            .map(state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1', pickerId: "projects" })));
-        expect(getNodeValue('1')(newState.projects)).toEqual({
+        const newState = pipe(
+            (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
+            state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1', pickerId: "projects" }))
+        )({ projects: createTree<{}>() });
+        expect(getNode('1')(newState.projects)).toEqual({
             ...initTreeNode({ id: '1', value: '1' }),
-            collapsed: false
+            expanded: true
         });
     });
 
     it('TOGGLE_TREE_PICKER_NODE_COLLAPSE - expanded', () => {
         const node = initTreeNode({ id: '1', value: '1' });
-        const [newState] = [{
-            projects: createTree<{}>()
-        }]
-            .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })))
-            .map(state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1', pickerId: "projects" })))
-            .map(state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1', pickerId: "projects" })));
-        expect(getNodeValue('1')(newState.projects)).toEqual({
+        const newState = pipe(
+            (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
+            state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1', pickerId: "projects" })),
+            state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id: '1', pickerId: "projects" })),
+        )({ projects: createTree<{}>() });
+        expect(getNode('1')(newState.projects)).toEqual({
             ...initTreeNode({ id: '1', value: '1' }),
-            collapsed: true
+            expanded: false
         });
     });
 
-    it('TOGGLE_TREE_PICKER_NODE_SELECT - selected', () => {
+    it('ACTIVATE_TREE_PICKER_NODE', () => {
         const node = initTreeNode({ id: '1', value: '1' });
-        const [newState] = [{
-            projects: createTree<{}>()
-        }]
-            .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })))
-            .map(state => treePickerReducer(state, treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: '1', pickerId: "projects" })));
-        expect(getNodeValue('1')(newState.projects)).toEqual({
+        const newState = pipe(
+            (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
+            state => treePickerReducer(state, treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: '1', pickerId: "projects" })),
+        )({ projects: createTree<{}>() });
+        expect(getNode('1')(newState.projects)).toEqual({
             ...initTreeNode({ id: '1', value: '1' }),
-            selected: true
+            active: true
         });
     });
 
-    it('TOGGLE_TREE_PICKER_NODE_SELECT - not selected', () => {
+    it('TOGGLE_TREE_PICKER_NODE_SELECTION', () => {
         const node = initTreeNode({ id: '1', value: '1' });
-        const [newState] = [{
-            projects: createTree<{}>()
-        }]
-            .map(state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })))
-            .map(state => treePickerReducer(state, treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: '1', pickerId: "projects" })))
-            .map(state => treePickerReducer(state, treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id: '1', pickerId: "projects" })));
-        expect(getNodeValue('1')(newState.projects)).toEqual({
+        const subNode = initTreeNode({ id: '1.1', value: '1.1' });
+        const newState = pipe(
+            (state: TreePicker) => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '', nodes: [node], pickerId: "projects" })),
+            state => treePickerReducer(state, treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({ id: '1', nodes: [subNode], pickerId: "projects" })),
+            state => treePickerReducer(state, treePickerActions.TOGGLE_TREE_PICKER_NODE_SELECTION({ id: '1.1', pickerId: "projects", cascade: true })),
+        )({ projects: createTree<{}>() });
+        expect(getNode('1')(newState.projects)).toEqual({
             ...initTreeNode({ id: '1', value: '1' }),
-            selected: false
+            selected: true,
+            children: ['1.1'],
+            status: TreeNodeStatus.LOADED,
         });
     });
 });