From d8f070fcc4dbc8a50ce49179e26a4e2179f7de3a Mon Sep 17 00:00:00 2001 From: Louise Davies Date: Wed, 24 Aug 2022 09:46:23 +0100 Subject: [PATCH] #1397 - make fileCountMax / totalSizeMax optional - this means we can more easily remember to check if they're set to a valid number via TS and to disable any features related to them if they're undefined --- .../src/ConfigProvider.tsx | 8 ++-- .../downloadCartTable.component.test.tsx | 41 ++++++++++++++-- .../downloadCartTable.component.tsx | 16 +++---- .../datagateway-download/src/index.test.tsx | 48 ------------------- packages/datagateway-download/src/index.tsx | 7 --- 5 files changed, 48 insertions(+), 72 deletions(-) diff --git a/packages/datagateway-download/src/ConfigProvider.tsx b/packages/datagateway-download/src/ConfigProvider.tsx index 1cbb1397c..9c756d707 100644 --- a/packages/datagateway-download/src/ConfigProvider.tsx +++ b/packages/datagateway-download/src/ConfigProvider.tsx @@ -16,8 +16,8 @@ export interface DownloadSettings { downloadApiUrl: string; idsUrl: string; - fileCountMax: number; - totalSizeMax: number; + fileCountMax?: number; + totalSizeMax?: number; accessMethods: DownloadSettingsAccessMethod; routes: PluginRoute[]; @@ -30,8 +30,8 @@ const initialConfiguration = { apiUrl: '', downloadApiUrl: '', idsUrl: '', - fileCountMax: -1, - totalSizeMax: -1, + fileCountMax: undefined, + totalSizeMax: undefined, accessMethods: {}, routes: [], helpSteps: [], diff --git a/packages/datagateway-download/src/downloadCart/downloadCartTable.component.test.tsx b/packages/datagateway-download/src/downloadCart/downloadCartTable.component.test.tsx index 6edd387be..0f27808e3 100644 --- a/packages/datagateway-download/src/downloadCart/downloadCartTable.component.test.tsx +++ b/packages/datagateway-download/src/downloadCart/downloadCartTable.component.test.tsx @@ -4,7 +4,7 @@ import DownloadCartTable from './downloadCartTable.component'; import { DownloadCartItem, fetchDownloadCart } from 'datagateway-common'; import { flushPromises } from '../setupTests'; import { act } from 'react-dom/test-utils'; -import { DownloadSettingsContext } from '../ConfigProvider'; +import { DownloadSettings, DownloadSettingsContext } from '../ConfigProvider'; import { Router } from 'react-router-dom'; import { ReactWrapper } from 'enzyme'; import { createMemoryHistory } from 'history'; @@ -47,7 +47,7 @@ describe('Download cart table component', () => { let cartItems: DownloadCartItem[] = []; // Create our mocked datagateway-download settings file. - let mockedSettings = { + let mockedSettings: DownloadSettings = { facilityName: 'LILS', apiUrl: 'https://example.com/api', downloadApiUrl: 'https://example.com/downloadApi', @@ -66,6 +66,8 @@ describe('Download cart table component', () => { description: 'Example description for Globus access method.', }, }, + routes: [], + helpSteps: [], }; const createWrapper = (): ReactWrapper => { @@ -122,7 +124,7 @@ describe('Download cart table component', () => { >).mockResolvedValue(cartItems); (removeAllDownloadCartItems as jest.MockedFunction< typeof removeAllDownloadCartItems - >).mockResolvedValue(null); + >).mockResolvedValue(undefined); (removeFromCart as jest.MockedFunction< typeof removeFromCart >).mockImplementation((entityType, entityIds) => { @@ -187,7 +189,7 @@ describe('Download cart table component', () => { '1 B' ); expect(wrapper.find('p#totalSizeDisplay').text()).toEqual( - expect.stringContaining('downloadCart.total_size: 4 B') + 'downloadCart.total_size: 4 B / 1 TB' ); }); @@ -209,7 +211,7 @@ describe('Download cart table component', () => { ); expect(wrapper.find('p#fileCountDisplay').text()).toEqual( - expect.stringContaining('downloadCart.number_of_files: 22 / 5000') + 'downloadCart.number_of_files: 22 / 5000' ); }); @@ -555,4 +557,33 @@ describe('Download cart table component', () => { mockedSettings = oldSettings; }); + + it('does not display error alerts if file/size limits are not set', async () => { + const oldSettings = mockedSettings; + mockedSettings = { + ...oldSettings, + totalSizeMax: undefined, + fileCountMax: undefined, + }; + + const wrapper = createWrapper(); + + await act(async () => { + await flushPromises(); + wrapper.update(); + }); + + // Make sure alerts are not displayed if limits are undefined + expect(wrapper.exists('div#fileLimitAlert')).toBeFalsy(); + expect(wrapper.exists('div#sizeLimitAlert')).toBeFalsy(); + + expect(wrapper.find('p#fileCountDisplay').text()).toEqual( + 'downloadCart.number_of_files: 22' + ); + expect(wrapper.find('p#totalSizeDisplay').text()).toEqual( + 'downloadCart.total_size: 4 B' + ); + + mockedSettings = oldSettings; + }); }); diff --git a/packages/datagateway-download/src/downloadCart/downloadCartTable.component.tsx b/packages/datagateway-download/src/downloadCart/downloadCartTable.component.tsx index 0e259b815..f818600a0 100644 --- a/packages/datagateway-download/src/downloadCart/downloadCartTable.component.tsx +++ b/packages/datagateway-download/src/downloadCart/downloadCartTable.component.tsx @@ -328,8 +328,8 @@ const DownloadCartTable: React.FC = ( style={{ height: `calc(100vh - 64px - 48px - 48px - 48px - 3rem${ emptyItems || - fileCount > fileCountMax || - totalSize > totalSizeMax + (fileCountMax && fileCount > fileCountMax) || + (totalSizeMax && totalSize > totalSizeMax) ? ' - 2rem' : '' } - (1.75 * 0.875rem + 12px)`, @@ -377,11 +377,11 @@ const DownloadCartTable: React.FC = ( {fileCount !== -1 ? fileCount : `${t('downloadCart.calculating')}...`} - {fileCountMax !== -1 && ` / ${fileCountMax}`} + {fileCountMax && ` / ${fileCountMax}`} - {fileCount > fileCountMax && ( + {fileCountMax && fileCount > fileCountMax && ( = ( {totalSize !== -1 ? formatBytes(totalSize) : `${t('downloadCart.calculating')}...`} - {totalSizeMax !== -1 && ` / ${formatBytes(totalSizeMax)}`} + {totalSizeMax && ` / ${formatBytes(totalSizeMax)}`} - {totalSize > totalSizeMax && ( + {totalSizeMax && totalSize > totalSizeMax && ( = ( fileCountsLoading || sizesLoading || emptyItems || - (fileCountMax !== -1 && fileCount > fileCountMax) || - (totalSizeMax !== -1 && totalSize > totalSizeMax) + (fileCountMax ? fileCount > fileCountMax : false) || + (totalSizeMax ? totalSize > totalSizeMax : false) } > {t('downloadCart.download')} diff --git a/packages/datagateway-download/src/index.test.tsx b/packages/datagateway-download/src/index.test.tsx index 86e249efd..9ed557642 100644 --- a/packages/datagateway-download/src/index.test.tsx +++ b/packages/datagateway-download/src/index.test.tsx @@ -185,8 +185,6 @@ describe('index - fetchSettings', () => { idsUrl: 'ids', apiUrl: 'api', downloadApiUrl: 'download-api', - fileCountMax: 5000, - totalSizeMax: 1000000000000, accessMethods: { https: { idsUrl: 'https-ids', @@ -214,8 +212,6 @@ describe('index - fetchSettings', () => { Promise.resolve({ data: { facilityName: 'Generic', - fileCountMax: 5000, - totalSizeMax: 1000000000000, accessMethods: { https: { idsUrl: 'https-ids', @@ -246,8 +242,6 @@ describe('index - fetchSettings', () => { idsUrl: 'ids', apiUrl: 'api', downloadApiUrl: 'download-api', - fileCountMax: 5000, - totalSizeMax: 1000000000000, }, }) ); @@ -271,8 +265,6 @@ describe('index - fetchSettings', () => { idsUrl: 'ids', apiUrl: 'api', downloadApiUrl: 'download-api', - fileCountMax: 5000, - totalSizeMax: 1000000000000, accessMethods: { https: { idsUrl: 'https-ids', @@ -307,8 +299,6 @@ describe('index - fetchSettings', () => { idsUrl: 'ids', apiUrl: 'api', downloadApiUrl: 'download-api', - fileCountMax: 5000, - totalSizeMax: 1000000000000, accessMethods: {}, }, }) @@ -373,40 +363,6 @@ describe('index - fetchSettings', () => { ); }); - it('logs an error if fileCountMax or totalSizeMax are undefined in the settings', async () => { - (axios.get as jest.Mock).mockImplementationOnce(() => - Promise.resolve({ - data: { - facilityName: 'Generic', - idsUrl: 'ids', - apiUrl: 'api', - downloadApiUrl: 'download-api', - accessMethods: { - https: { - idsUrl: 'https-ids', - displayName: 'HTTPS', - description: 'HTTP description', - }, - globus: { - displayName: 'Globus', - description: 'Globus description', - }, - }, - }, - }) - ); - - const settings = await fetchSettings(); - - expect(settings).toBeUndefined(); - expect(log.error).toHaveBeenCalled(); - - const mockLog = (log.error as jest.Mock).mock; - expect(mockLog.calls[0][0]).toEqual( - 'Error loading /datagateway-download-settings.json: fileCountMax or totalSizeMax is undefined in settings' - ); - }); - it('logs an error if no routes are defined in the settings', async () => { (axios.get as jest.Mock).mockImplementationOnce(() => Promise.resolve({ @@ -427,8 +383,6 @@ describe('index - fetchSettings', () => { description: 'Globus description', }, }, - fileCountMax: 5000, - totalSizeMax: 1000000000000, }, }) ); @@ -464,8 +418,6 @@ describe('index - fetchSettings', () => { description: 'Globus description', }, }, - fileCountMax: 5000, - totalSizeMax: 1000000000000, routes: [ { section: 'section', diff --git a/packages/datagateway-download/src/index.tsx b/packages/datagateway-download/src/index.tsx index b0fe7d47a..53137c9b9 100644 --- a/packages/datagateway-download/src/index.tsx +++ b/packages/datagateway-download/src/index.tsx @@ -89,13 +89,6 @@ export const fetchSettings = (): Promise => { ); } - // Ensure all fileCountMax and totalSizeMax are present. - if (!('fileCountMax' in settings && 'totalSizeMax' in settings)) { - throw new Error( - 'fileCountMax or totalSizeMax is undefined in settings' - ); - } - // Ensure access methods are present in the configuration. if (!('accessMethods' in settings)) { throw new Error('accessMethods is undefined in settings');