Merge branch '21891-output-copying-performance'
[arvados.git] / services / workbench2 / src / store / context-menu / context-menu-actions.test.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ContextMenuKind } from 'views-components/context-menu/menu-item-sort';
6 import { resourceUuidToContextMenuKind } from './context-menu-actions';
7 import configureStore from 'redux-mock-store';
8 import thunk from 'redux-thunk';
9 import { PROJECT_PANEL_CURRENT_UUID } from '../project-panel/project-panel-action';
10 import { GroupClass } from 'models/group';
11
12 describe('context-menu-actions', () => {
13     describe('resourceUuidToContextMenuKind', () => {
14         const middlewares = [thunk];
15         const mockStore = configureStore(middlewares);
16         const userUuid = 'zzzzz-tpzed-bbbbbbbbbbbbbbb';
17         const otherUserUuid = 'zzzzz-tpzed-bbbbbbbbbbbbbbc';
18         const headCollectionUuid = 'zzzzz-4zz18-aaaaaaaaaaaaaaa';
19         const oldCollectionUuid = 'zzzzz-4zz18-aaaaaaaaaaaaaab';
20         const projectUuid = 'zzzzz-j7d0g-ccccccccccccccc';
21         const filterGroupUuid = 'zzzzz-j7d0g-ccccccccccccccd';
22         const linkUuid = 'zzzzz-o0j2j-0123456789abcde';
23         const containerRequestUuid = 'zzzzz-xvhdp-0123456789abcde';
24
25         it('should return the correct menu kind', () => {
26             const cases = [
27                 // resourceUuid, isAdminUser, isEditable, isTrashed, forceReadonly, expected
28                 [headCollectionUuid, false, true, true, false, ContextMenuKind.TRASHED_COLLECTION],
29                 [headCollectionUuid, false, true, false, false, ContextMenuKind.COLLECTION],
30                 [headCollectionUuid, false, true, false, true, ContextMenuKind.READONLY_COLLECTION],
31                 [headCollectionUuid, false, false, true, false, ContextMenuKind.READONLY_COLLECTION],
32                 [headCollectionUuid, false, false, false, false, ContextMenuKind.READONLY_COLLECTION],
33                 [headCollectionUuid, true, true, true, false, ContextMenuKind.TRASHED_COLLECTION],
34                 [headCollectionUuid, true, true, false, false, ContextMenuKind.COLLECTION_ADMIN],
35                 [headCollectionUuid, true, false, true, false, ContextMenuKind.TRASHED_COLLECTION],
36                 [headCollectionUuid, true, false, false, false, ContextMenuKind.COLLECTION_ADMIN],
37                 [headCollectionUuid, true, false, false, true, ContextMenuKind.READONLY_COLLECTION],
38
39                 [oldCollectionUuid, false, true, true, false, ContextMenuKind.OLD_VERSION_COLLECTION],
40                 [oldCollectionUuid, false, true, false, false, ContextMenuKind.OLD_VERSION_COLLECTION],
41                 [oldCollectionUuid, false, false, true, false, ContextMenuKind.OLD_VERSION_COLLECTION],
42                 [oldCollectionUuid, false, false, false, false, ContextMenuKind.OLD_VERSION_COLLECTION],
43                 [oldCollectionUuid, true, true, true, false, ContextMenuKind.OLD_VERSION_COLLECTION],
44                 [oldCollectionUuid, true, true, false, false, ContextMenuKind.OLD_VERSION_COLLECTION],
45                 [oldCollectionUuid, true, false, true, false, ContextMenuKind.OLD_VERSION_COLLECTION],
46                 [oldCollectionUuid, true, false, false, false, ContextMenuKind.OLD_VERSION_COLLECTION],
47
48                 // FIXME: WB2 doesn't currently have context menu for trashed projects
49                 // [projectUuid, false, true, true, false, ContextMenuKind.TRASHED_PROJECT],
50                 [projectUuid, false, true, false, false, ContextMenuKind.PROJECT],
51                 [projectUuid, false, true, false, true, ContextMenuKind.READONLY_PROJECT],
52                 [projectUuid, false, false, true, false, ContextMenuKind.READONLY_PROJECT],
53                 [projectUuid, false, false, false, false, ContextMenuKind.READONLY_PROJECT],
54                 // [projectUuid, true, true, true, false, ContextMenuKind.TRASHED_PROJECT],
55                 [projectUuid, true, true, false, false, ContextMenuKind.PROJECT_ADMIN],
56                 // [projectUuid, true, false, true, false, ContextMenuKind.TRASHED_PROJECT],
57                 [projectUuid, true, false, false, false, ContextMenuKind.PROJECT_ADMIN],
58                 [projectUuid, true, false, false, true, ContextMenuKind.READONLY_PROJECT],
59
60                 [linkUuid, false, true, true, false, ContextMenuKind.LINK],
61                 [linkUuid, false, true, false, false, ContextMenuKind.LINK],
62                 [linkUuid, false, false, true, false, ContextMenuKind.LINK],
63                 [linkUuid, false, false, false, false, ContextMenuKind.LINK],
64                 [linkUuid, true, true, true, false, ContextMenuKind.LINK],
65                 [linkUuid, true, true, false, false, ContextMenuKind.LINK],
66                 [linkUuid, true, false, true, false, ContextMenuKind.LINK],
67                 [linkUuid, true, false, false, false, ContextMenuKind.LINK],
68
69                 [userUuid, false, true, true, false, ContextMenuKind.ROOT_PROJECT],
70                 [userUuid, false, true, false, false, ContextMenuKind.ROOT_PROJECT],
71                 [userUuid, false, false, true, false, ContextMenuKind.ROOT_PROJECT],
72                 [userUuid, false, false, false, false, ContextMenuKind.ROOT_PROJECT],
73                 [userUuid, true, true, true, false, ContextMenuKind.ROOT_PROJECT],
74                 [userUuid, true, true, false, false, ContextMenuKind.ROOT_PROJECT],
75                 [userUuid, true, false, true, false, ContextMenuKind.ROOT_PROJECT],
76                 [userUuid, true, false, false, false, ContextMenuKind.ROOT_PROJECT],
77
78                 [containerRequestUuid, false, true, true, false, ContextMenuKind.PROCESS_RESOURCE],
79                 [containerRequestUuid, false, true, false, false, ContextMenuKind.PROCESS_RESOURCE],
80                 [containerRequestUuid, false, false, true, false, ContextMenuKind.PROCESS_RESOURCE],
81                 [containerRequestUuid, false, false, false, false, ContextMenuKind.PROCESS_RESOURCE],
82                 [containerRequestUuid, false, false, false, true, ContextMenuKind.READONLY_PROCESS_RESOURCE],
83                 [containerRequestUuid, true, true, true, false, ContextMenuKind.PROCESS_ADMIN],
84                 [containerRequestUuid, true, true, false, false, ContextMenuKind.PROCESS_ADMIN],
85                 [containerRequestUuid, true, false, true, false, ContextMenuKind.PROCESS_ADMIN],
86                 [containerRequestUuid, true, false, false, false, ContextMenuKind.PROCESS_ADMIN],
87                 [containerRequestUuid, true, false, false, true, ContextMenuKind.READONLY_PROCESS_RESOURCE],
88             ]
89
90             cases.forEach(([resourceUuid, isAdminUser, isEditable, isTrashed, forceReadonly, expected]) => {
91                 const initialState = {
92                     properties: {
93                         [PROJECT_PANEL_CURRENT_UUID]: projectUuid,
94                     },
95                     resources: {
96                         [headCollectionUuid]: {
97                             uuid: headCollectionUuid,
98                             ownerUuid: projectUuid,
99                             currentVersionUuid: headCollectionUuid,
100                             isTrashed: isTrashed,
101                         },
102                         [oldCollectionUuid]: {
103                             uuid: oldCollectionUuid,
104                             currentVersionUuid: headCollectionUuid,
105                             isTrashed: isTrashed,
106                         },
107                         [projectUuid]: {
108                             uuid: projectUuid,
109                             ownerUuid: isEditable ? userUuid : otherUserUuid,
110                             canWrite: isEditable,
111                             groupClass: GroupClass.PROJECT,
112                         },
113                         [filterGroupUuid]: {
114                             uuid: filterGroupUuid,
115                             ownerUuid: isEditable ? userUuid : otherUserUuid,
116                             canWrite: isEditable,
117                             groupClass: GroupClass.FILTER,
118                         },
119                         [linkUuid]: {
120                             uuid: linkUuid,
121                         },
122                         [userUuid]: {
123                             uuid: userUuid,
124                         },
125                         [containerRequestUuid]: {
126                             uuid: containerRequestUuid,
127                             ownerUuid: projectUuid,
128                         },
129                     },
130                     auth: {
131                         user: {
132                             uuid: userUuid,
133                             isAdmin: isAdminUser,
134                         },
135                     },
136                 };
137                 const store = mockStore(initialState);
138
139                 let menuKind: any;
140                 try {
141                     menuKind = store.dispatch<any>(resourceUuidToContextMenuKind(resourceUuid as string, forceReadonly as boolean))
142                     expect(menuKind).toBe(expected);
143                 } catch (err) {
144                     throw new Error(`menuKind for resource ${JSON.stringify(initialState.resources[resourceUuid as string])} forceReadonly: ${forceReadonly} expected to be ${expected} but got ${menuKind}.`);
145                 }
146             });
147         });
148     });
149 });