Merge branch '22349-doc-updates'
[arvados.git] / services / workbench2 / src / views-components / data-explorer / renderers.cy.js
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { GroupMembersCount, ProcessStatus, ResourceFileSize } from './renderers';
7 import { Provider } from 'react-redux';
8 import configureMockStore from 'redux-mock-store'
9 import { ResourceKind } from '../../models/resource';
10 import { ContainerRequestState as CR } from '../../models/container-request';
11 import { ContainerState as C } from '../../models/container';
12 import { ProcessStatus as PS } from '../../store/processes/process';
13 import { ThemeProvider, Theme, StyledEngineProvider } from '@mui/material';
14 import { CustomTheme } from 'common/custom-theme';
15
16 const middlewares = [];
17 const mockStore = configureMockStore(middlewares);
18
19 describe('renderers', () => {
20     let props = null;
21
22     describe('ProcessStatus', () => {
23         props = {
24             uuid: 'zzzzz-xvhdp-zzzzzzzzzzzzzzz',
25             theme: {
26                 customs: {
27                     colors: {
28                         // Color values are arbitrary, but they should be
29                         // representative of the colors used in the UI.
30                         green800: 'rgb(0, 255, 0)',
31                         red900: 'rgb(255, 0, 0)',
32                         orange: 'rgb(240, 173, 78)',
33                         grey600: 'rgb(128, 128, 128)',
34                     }
35                 },
36                 spacing: (value) => value * 8,
37                 palette: {
38                     common: {
39                         white: 'rgb(255, 255, 255)',
40                     },
41                 },
42             },
43         };
44
45         [
46             // CR Status ; Priority ; C Status ; Exit Code ; C RuntimeStatus ; Expected label ; Expected bg color ; Expected fg color
47             [CR.COMMITTED, 1, C.RUNNING, null, {}, PS.RUNNING, props.theme.palette.common.white, props.theme.customs.colors.green800],
48             [CR.COMMITTED, 1, C.RUNNING, null, { error: 'whoops' }, PS.FAILING, props.theme.palette.common.white, props.theme.customs.colors.red900],
49             [CR.COMMITTED, 1, C.RUNNING, null, { warning: 'watch out!' }, PS.WARNING, props.theme.palette.common.white, props.theme.customs.colors.green800],
50             [CR.FINAL, 1, C.CANCELLED, null, {}, PS.CANCELLED, props.theme.customs.colors.red900, props.theme.palette.common.white],
51             [CR.FINAL, 1, C.COMPLETE, 137, {}, PS.FAILED, props.theme.customs.colors.red900, props.theme.palette.common.white],
52             [CR.FINAL, 1, C.COMPLETE, 0, {}, PS.COMPLETED, props.theme.customs.colors.green800, props.theme.palette.common.white],
53             [CR.COMMITTED, 0, C.LOCKED, null, {}, PS.ONHOLD, props.theme.customs.colors.grey600, props.theme.palette.common.white],
54             [CR.COMMITTED, 0, C.QUEUED, null, {}, PS.ONHOLD, props.theme.customs.colors.grey600, props.theme.palette.common.white],
55             [CR.COMMITTED, 1, C.LOCKED, null, {}, PS.QUEUED, props.theme.palette.common.white, props.theme.customs.colors.grey600],
56             [CR.COMMITTED, 1, C.QUEUED, null, {}, PS.QUEUED, props.theme.palette.common.white, props.theme.customs.colors.grey600],
57         ].forEach(([crState, crPrio, cState, exitCode, rs, eLabel, eColor, tColor]) => {
58             it(`should render the state label '${eLabel}' and color '${eColor}' for CR state=${crState}, priority=${crPrio}, C state=${cState}, exitCode=${exitCode} and RuntimeStatus=${JSON.stringify(rs)}`, () => {
59                 const containerUuid = 'zzzzz-dz642-zzzzzzzzzzzzzzz';
60                 const store = mockStore({
61                     resources: {
62                         [props.uuid]: {
63                             kind: ResourceKind.CONTAINER_REQUEST,
64                             state: crState,
65                             containerUuid: containerUuid,
66                             priority: crPrio,
67                         },
68                         [containerUuid]: {
69                             kind: ResourceKind.CONTAINER,
70                             state: cState,
71                             runtimeStatus: rs,
72                             exitCode: exitCode,
73                         },
74                     }
75                 });
76
77                 cy.mount(
78                     <Provider store={store}>
79                         <ThemeProvider theme={CustomTheme}>
80                             <ProcessStatus {...props} />
81                         </ThemeProvider>
82                     </Provider>);
83
84                 cy.get('span').should('have.text', eLabel);
85                 cy.get('span').should('have.css', 'color', tColor);
86                 cy.get('[data-cy=process-status-chip]').should('have.css', 'background-color', eColor);
87             });
88         })
89     });
90
91     describe('ResourceFileSize', () => {
92         beforeEach(() => {
93             props = {
94                 uuid: 'UUID',
95             };
96         });
97
98         it('should render collection fileSizeTotal', () => {
99             // given
100             const store = mockStore({
101                 resources: {
102                     [props.uuid]: {
103                         kind: ResourceKind.COLLECTION,
104                         fileSizeTotal: 100,
105                     }
106                 }
107             });
108
109             // when
110             cy.mount(<Provider store={store}>
111                 <ResourceFileSize {...props}></ResourceFileSize>
112             </Provider>);
113
114             // then
115             cy.get('p').should('have.text', '100 B');
116         });
117
118         it('should render 0 B as file size', () => {
119             // given
120             const store = mockStore({ resources: {} });
121
122             // when
123             cy.mount(<Provider store={store}>
124                 <ResourceFileSize {...props}></ResourceFileSize>
125             </Provider>);
126
127             // then
128             cy.get('p').should('have.text', '0 B');
129         });
130
131         it('should render empty string for non collection resource', () => {
132             // given
133             const store1 = mockStore({
134                 resources: {
135                     [props.uuid]: {
136                         kind: ResourceKind.PROJECT,
137                         fileSizeTotal: 100,
138                     }
139                 }
140             });
141             const store2 = mockStore({
142                 resources: {
143                     [props.uuid]: {
144                         kind: ResourceKind.PROCESS,
145                         fileSizeTotal: 200,
146                     }
147                 }
148             });
149
150             // when
151             cy.mount(<Provider store={store1}>
152                 <ResourceFileSize {...props}></ResourceFileSize>
153             </Provider>);
154
155             // then
156             cy.get('p').should('have.text', '-');
157             
158             // when
159             cy.mount(<Provider store={store2}>
160                 <ResourceFileSize {...props}></ResourceFileSize>
161             </Provider>);
162
163             // then
164             cy.get('p').should('have.text', '-');
165         });
166     });
167
168     describe('GroupMembersCount', () => {
169         let fakeGroup;
170         beforeEach(() => {
171             props = {
172                 uuid: 'zzzzz-j7d0g-000000000000000',
173             };
174             fakeGroup = {
175                 "canManage": true,
176                 "canWrite": true,
177                 "createdAt": "2020-09-24T22:52:57.546521000Z",
178                 "deleteAt": null,
179                 "description": "Test Group",
180                 "etag": "0000000000000000000000000",
181                 "frozenByUuid": null,
182                 "groupClass": "role",
183                 "isTrashed": false,
184                 "kind": ResourceKind.GROUP,
185                 "modifiedAt": "2020-09-24T22:52:57.545669000Z",
186                 "modifiedByUserUuid": "zzzzz-tpzed-000000000000000",
187                 "name": "System group",
188                 "ownerUuid": "zzzzz-tpzed-000000000000000",
189                 "properties": {},
190                 "trashAt": null,
191                 "uuid": props.uuid,
192                 "writableBy": [
193                     "zzzzz-tpzed-000000000000000",
194                 ]
195             };
196         });
197
198         it('shows loading group count when no memberCount', () => {
199             // Given
200             const store = mockStore({resources: {
201                 [props.uuid]: fakeGroup,
202             }});
203
204             const wrapper = cy.mount(<Provider store={store}>
205                 <StyledEngineProvider injectFirst>
206                     <ThemeProvider theme={CustomTheme}>
207                         <GroupMembersCount {...props} />
208                     </ThemeProvider>
209                 </StyledEngineProvider>
210             </Provider>);
211
212             cy.get('[data-testid=three-dots-svg]').should('exist');
213         });
214
215         it('shows group count when memberCount present', () => {
216             // Given
217             const store = mockStore({resources: {
218                 [props.uuid]: {
219                     ...fakeGroup,
220                     "memberCount": 765,
221                 }
222             }});
223
224             cy.mount(<Provider store={store}>
225                 <StyledEngineProvider injectFirst>
226                     <ThemeProvider theme={CustomTheme}>
227                         <GroupMembersCount {...props} />
228                     </ThemeProvider>
229                 </StyledEngineProvider>
230             </Provider>);
231
232             cy.get('p').should('have.text', '765');
233         });
234
235         it('shows group count error icon when memberCount is null', () => {
236             // Given
237             const store = mockStore({resources: {
238                 [props.uuid]: {
239                     ...fakeGroup,
240                     "memberCount": null,
241                 }
242             }});
243
244             cy.mount(<Provider store={store}>
245                 <StyledEngineProvider injectFirst>
246                     <ThemeProvider theme={CustomTheme}>
247                         <GroupMembersCount {...props} />
248                     </ThemeProvider>
249                 </StyledEngineProvider>
250             </Provider>);
251
252             cy.get('[data-testid=ErrorRoundedIcon]').should('exist');
253         });
254
255     });
256
257 });