Merge branch '17305-projects-file-size-always-0'
authorDaniel Kutyła <daniel.kutyla@contractors.roche.com>
Mon, 7 Jun 2021 20:43:59 +0000 (22:43 +0200)
committerDaniel Kutyła <daniel.kutyla@contractors.roche.com>
Mon, 7 Jun 2021 20:44:02 +0000 (22:44 +0200)
closes #17305

Arvados-DCO-1.1-Signed-off-by: Daniel Kutyła <daniel.kutyla@contractors.roche.com>

src/common/formatters.ts
src/views-components/data-explorer/renderers.test.tsx [new file with mode: 0644]
src/views-components/data-explorer/renderers.tsx

index 17917127f1c26ac18221f8b34a2efcfd6413da5f..d8228bf5eb007f5c0843d0f16990a73e9b9a1d28 100644 (file)
@@ -20,7 +20,7 @@ export const formatDate = (isoDate?: string | null, utc: boolean = false) => {
     return "(none)";
 };
 
-export const formatFileSize = (size?: number) => {
+export const formatFileSize = (size?: number | string) => {
     if (typeof size === "number") {
         if (size === 0) { return "0 B"; }
 
@@ -29,6 +29,9 @@ export const formatFileSize = (size?: number) => {
                 return `${(size / base).toFixed()} ${unit}`;
             }
         }
+    } 
+    if ((typeof size === "string" && size === '') || size === undefined) {
+        return '';
     }
     return "0 B";
 };
diff --git a/src/views-components/data-explorer/renderers.test.tsx b/src/views-components/data-explorer/renderers.test.tsx
new file mode 100644 (file)
index 0000000..5f752b6
--- /dev/null
@@ -0,0 +1,85 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import * as React from 'react';
+import { mount, configure } from 'enzyme';
+import { ResourceFileSize } from './renderers';
+import * as Adapter from "enzyme-adapter-react-16";
+import { Provider } from 'react-redux';
+import configureMockStore from 'redux-mock-store'
+import { ResourceKind } from '../../models/resource';
+
+const middlewares = [];
+const mockStore = configureMockStore(middlewares);
+
+configure({ adapter: new Adapter() });
+
+describe('renderers', () => {
+    let props = null;
+
+    describe('ResourceFileSize', () => {
+        beforeEach(() => {
+            props = {
+                uuid: 'UUID',
+            };
+        });
+
+        it('should render collection fileSizeTotal', () => {
+            // given
+            const store = mockStore({ resources: {
+                [props.uuid]: {
+                    kind: ResourceKind.COLLECTION,
+                    fileSizeTotal: 100,
+                }
+            }});
+
+            // when
+            const wrapper = mount(<Provider store={store}>
+                <ResourceFileSize {...props}></ResourceFileSize>
+            </Provider>);
+
+            // then
+            expect(wrapper.text()).toContain('100 B');
+        });
+
+        it('should render 0 B as file size', () => {
+            // given
+            const store = mockStore({ resources: {} });
+
+            // when
+            const wrapper = mount(<Provider store={store}>
+                <ResourceFileSize {...props}></ResourceFileSize>
+            </Provider>);
+
+            // then
+            expect(wrapper.text()).toContain('0 B');
+        });
+        
+        it('should render empty string for non collection resource', () => {
+            // given
+            const store1 = mockStore({ resources: {
+                [props.uuid]: {
+                    kind: ResourceKind.PROJECT,
+                }
+            }});
+            const store2 = mockStore({ resources: {
+                [props.uuid]: {
+                    kind: ResourceKind.PROJECT,
+                }
+            }});
+
+            // when
+            const wrapper1 = mount(<Provider store={store1}>
+                <ResourceFileSize {...props}></ResourceFileSize>
+            </Provider>);
+            const wrapper2 = mount(<Provider store={store2}>
+                <ResourceFileSize {...props}></ResourceFileSize>
+            </Provider>);
+
+            // then
+            expect(wrapper1.text()).toContain('');
+            expect(wrapper2.text()).toContain('');
+        });
+    });
+});
\ No newline at end of file
index ee40dd3988a2c4b028863903c5c533a045c1c147..0e3eefe231671e470075090d9f0c064b6d40bdc1 100644 (file)
@@ -417,6 +417,11 @@ export const renderFileSize = (fileSize?: number) =>
 export const ResourceFileSize = connect(
     (state: RootState, props: { uuid: string }) => {
         const resource = getResource<CollectionResource>(props.uuid)(state.resources);
+
+        if (resource && resource.kind !== ResourceKind.COLLECTION) {
+            return { fileSize: '' };
+        }
+
         return { fileSize: resource ? resource.fileSizeTotal : 0 };
     })((props: { fileSize?: number }) => renderFileSize(props.fileSize));