Merge branch 'master' into 14078-obtain-configuration-data-from-discovery-endpoint
[arvados.git] / src / store / project / project-reducer.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { projectsReducer, getTreePath } from "./project-reducer";
6 import { projectActions } from "./project-action";
7 import { TreeItem, TreeItemStatus } from "~/components/tree/tree";
8 import { mockProjectResource } from "~/models/test-utils";
9
10 describe('project-reducer', () => {
11
12     it('should load projects', () => {
13         const initialState = undefined;
14
15         const projects = [mockProjectResource({ uuid: "1" }), mockProjectResource({ uuid: "2" })];
16         const state = projectsReducer(initialState, projectActions.PROJECTS_SUCCESS({ projects, parentItemId: undefined }));
17         expect(state).toEqual({
18             items: [{
19                 active: false,
20                 open: false,
21                 id: "1",
22                 items: [],
23                 data: mockProjectResource({ uuid: "1" }),
24                 status: TreeItemStatus.INITIAL
25             }, {
26                 active: false,
27                 open: false,
28                 id: "2",
29                 items: [],
30                 data: mockProjectResource({ uuid: "2" }),
31                 status: TreeItemStatus.INITIAL
32             }
33             ],
34             currentItemId: "",
35             creator: {
36                 opened: false,
37                 ownerUuid: "",
38             },
39             updater: {
40                 opened: false,
41                 uuid: ''
42             }
43         });
44     });
45
46     it('should remove activity on projects list', () => {
47         const initialState = {
48             items: [{
49                 data: mockProjectResource(),
50                 id: "1",
51                 open: true,
52                 active: true,
53                 status: TreeItemStatus.PENDING
54             }],
55             currentItemId: "1",
56             creator: { opened: false, ownerUuid: "" },
57             updater: { opened: false, uuid: '' }
58         };
59         const project = {
60             items: [{
61                 data: mockProjectResource(),
62                 id: "1",
63                 open: true,
64                 active: false,
65                 status: TreeItemStatus.PENDING
66             }],
67             currentItemId: "",
68             creator: { opened: false, ownerUuid: "" },
69             updater: { opened: false, uuid: '' }
70         };
71
72         const state = projectsReducer(initialState, projectActions.RESET_PROJECT_TREE_ACTIVITY(initialState.items[0].id));
73         expect(state).toEqual(project);
74     });
75
76     it('should toggle project tree item activity', () => {
77         const initialState = {
78             items: [{
79                 data: mockProjectResource(),
80                 id: "1",
81                 open: true,
82                 active: false,
83                 status: TreeItemStatus.PENDING
84             }],
85             currentItemId: "1",
86             creator: { opened: false, ownerUuid: "" },
87             updater: { opened: false, uuid: '' }
88         };
89         const project = {
90             items: [{
91                 data: mockProjectResource(),
92                 id: "1",
93                 open: true,
94                 active: true,
95                 status: TreeItemStatus.PENDING,
96             }],
97             currentItemId: "1",
98             creator: { opened: false, ownerUuid: "" },
99             updater: { opened: false, uuid: '' }
100         };
101
102         const state = projectsReducer(initialState, projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(initialState.items[0].id));
103         expect(state).toEqual(project);
104     });
105
106
107     it('should close project tree item ', () => {
108         const initialState = {
109             items: [{
110                 data: mockProjectResource(),
111                 id: "1",
112                 open: true,
113                 active: false,
114                 status: TreeItemStatus.PENDING,
115             }],
116             currentItemId: "1",
117             creator: { opened: false, ownerUuid: "" },
118             updater: { opened: false, uuid: '' }
119         };
120         const project = {
121             items: [{
122                 data: mockProjectResource(),
123                 id: "1",
124                 open: false,
125                 active: false,
126                 status: TreeItemStatus.PENDING,
127             }],
128             currentItemId: "1",
129             creator: { opened: false, ownerUuid: "" },
130             updater: { opened: false, uuid: '' }
131
132         };
133
134         const state = projectsReducer(initialState, projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(initialState.items[0].id));
135         expect(state).toEqual(project);
136     });
137 });
138
139 describe("findTreeBranch", () => {
140     const createTreeItem = (id: string, items?: Array<TreeItem<string>>): TreeItem<string> => ({
141         id,
142         items,
143         active: false,
144         data: "",
145         open: false,
146         status: TreeItemStatus.INITIAL
147     });
148
149     it("should return an array that matches path to the given item", () => {
150         const tree: Array<TreeItem<string>> = [
151             createTreeItem("1", [
152                 createTreeItem("1.1", [
153                     createTreeItem("1.1.1"),
154                     createTreeItem("1.1.2")
155                 ])
156             ]),
157             createTreeItem("2", [
158                 createTreeItem("2.1", [
159                     createTreeItem("2.1.1"),
160                     createTreeItem("2.1.2")
161                 ])
162             ])
163         ];
164         const branch = getTreePath(tree, "2.1.1");
165         expect(branch.map(item => item.id)).toEqual(["2", "2.1", "2.1.1"]);
166     });
167
168     it("should return empty array if item is not found", () => {
169         const tree: Array<TreeItem<string>> = [
170             createTreeItem("1", [
171                 createTreeItem("1.1", [
172                     createTreeItem("1.1.1"),
173                     createTreeItem("1.1.2")
174                 ])
175             ]),
176             createTreeItem("2", [
177                 createTreeItem("2.1", [
178                     createTreeItem("2.1.1"),
179                     createTreeItem("2.1.2")
180                 ])
181             ])
182         ];
183         expect(getTreePath(tree, "3")).toHaveLength(0);
184     });
185
186 });