Skip to content

Commit

Permalink
Merge pull request #1398 from ral-facilities/bugfix/make-cart-limits-…
Browse files Browse the repository at this point in the history
…optional-#1397

#1397 - make fileCountMax / totalSizeMax optional
  • Loading branch information
louise-davies authored Aug 24, 2022
2 parents 7324fc0 + d8f070f commit 026a5d1
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 72 deletions.
8 changes: 4 additions & 4 deletions packages/datagateway-download/src/ConfigProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export interface DownloadSettings {
downloadApiUrl: string;
idsUrl: string;

fileCountMax: number;
totalSizeMax: number;
fileCountMax?: number;
totalSizeMax?: number;

accessMethods: DownloadSettingsAccessMethod;
routes: PluginRoute[];
Expand All @@ -30,8 +30,8 @@ const initialConfiguration = {
apiUrl: '',
downloadApiUrl: '',
idsUrl: '',
fileCountMax: -1,
totalSizeMax: -1,
fileCountMax: undefined,
totalSizeMax: undefined,
accessMethods: {},
routes: [],
helpSteps: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -66,6 +66,8 @@ describe('Download cart table component', () => {
description: 'Example description for Globus access method.',
},
},
routes: [],
helpSteps: [],
};

const createWrapper = (): ReactWrapper => {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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'
);
});

Expand All @@ -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'
);
});

Expand Down Expand Up @@ -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;
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ const DownloadCartTable: React.FC<DownloadCartTableProps> = (
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)`,
Expand Down Expand Up @@ -377,11 +377,11 @@ const DownloadCartTable: React.FC<DownloadCartTableProps> = (
{fileCount !== -1
? fileCount
: `${t('downloadCart.calculating')}...`}
{fileCountMax !== -1 && ` / ${fileCountMax}`}
{fileCountMax && ` / ${fileCountMax}`}
</Typography>
</Grid>
<Grid item>
{fileCount > fileCountMax && (
{fileCountMax && fileCount > fileCountMax && (
<Alert
id="fileLimitAlert"
variant="filled"
Expand Down Expand Up @@ -422,11 +422,11 @@ const DownloadCartTable: React.FC<DownloadCartTableProps> = (
{totalSize !== -1
? formatBytes(totalSize)
: `${t('downloadCart.calculating')}...`}
{totalSizeMax !== -1 && ` / ${formatBytes(totalSizeMax)}`}
{totalSizeMax && ` / ${formatBytes(totalSizeMax)}`}
</Typography>
</Grid>
<Grid item>
{totalSize > totalSizeMax && (
{totalSizeMax && totalSize > totalSizeMax && (
<Alert
id="sizeLimitAlert"
variant="filled"
Expand Down Expand Up @@ -502,8 +502,8 @@ const DownloadCartTable: React.FC<DownloadCartTableProps> = (
fileCountsLoading ||
sizesLoading ||
emptyItems ||
(fileCountMax !== -1 && fileCount > fileCountMax) ||
(totalSizeMax !== -1 && totalSize > totalSizeMax)
(fileCountMax ? fileCount > fileCountMax : false) ||
(totalSizeMax ? totalSize > totalSizeMax : false)
}
>
{t('downloadCart.download')}
Expand Down
48 changes: 0 additions & 48 deletions packages/datagateway-download/src/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ describe('index - fetchSettings', () => {
idsUrl: 'ids',
apiUrl: 'api',
downloadApiUrl: 'download-api',
fileCountMax: 5000,
totalSizeMax: 1000000000000,
accessMethods: {
https: {
idsUrl: 'https-ids',
Expand Down Expand Up @@ -214,8 +212,6 @@ describe('index - fetchSettings', () => {
Promise.resolve({
data: {
facilityName: 'Generic',
fileCountMax: 5000,
totalSizeMax: 1000000000000,
accessMethods: {
https: {
idsUrl: 'https-ids',
Expand Down Expand Up @@ -246,8 +242,6 @@ describe('index - fetchSettings', () => {
idsUrl: 'ids',
apiUrl: 'api',
downloadApiUrl: 'download-api',
fileCountMax: 5000,
totalSizeMax: 1000000000000,
},
})
);
Expand All @@ -271,8 +265,6 @@ describe('index - fetchSettings', () => {
idsUrl: 'ids',
apiUrl: 'api',
downloadApiUrl: 'download-api',
fileCountMax: 5000,
totalSizeMax: 1000000000000,
accessMethods: {
https: {
idsUrl: 'https-ids',
Expand Down Expand Up @@ -307,8 +299,6 @@ describe('index - fetchSettings', () => {
idsUrl: 'ids',
apiUrl: 'api',
downloadApiUrl: 'download-api',
fileCountMax: 5000,
totalSizeMax: 1000000000000,
accessMethods: {},
},
})
Expand Down Expand Up @@ -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({
Expand All @@ -427,8 +383,6 @@ describe('index - fetchSettings', () => {
description: 'Globus description',
},
},
fileCountMax: 5000,
totalSizeMax: 1000000000000,
},
})
);
Expand Down Expand Up @@ -464,8 +418,6 @@ describe('index - fetchSettings', () => {
description: 'Globus description',
},
},
fileCountMax: 5000,
totalSizeMax: 1000000000000,
routes: [
{
section: 'section',
Expand Down
7 changes: 0 additions & 7 deletions packages/datagateway-download/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,6 @@ export const fetchSettings = (): Promise<DownloadSettings | void> => {
);
}

// 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');
Expand Down

0 comments on commit 026a5d1

Please sign in to comment.