From 0e3a49aba371e4da2ffc4eaa39e94318e5779d3e Mon Sep 17 00:00:00 2001 From: MellyGray Date: Tue, 12 Dec 2023 13:52:09 +0100 Subject: [PATCH] feat(IntegrationDatasetDownload): fix size conversion --- public/locales/en/dataset.json | 2 +- src/files/domain/models/File.ts | 7 ++++--- .../access-dataset-menu/AccessDatasetMenu.tsx | 6 +++--- .../AccessDatasetMenu.stories.tsx | 4 ++++ .../dataset/domain/models/DatasetMother.ts | 8 ++++---- .../AccessDatasetMenu.spec.tsx | 18 ++++++++++++------ .../e2e/sections/dataset/Dataset.spec.tsx | 2 +- 7 files changed, 29 insertions(+), 18 deletions(-) diff --git a/public/locales/en/dataset.json b/public/locales/en/dataset.json index f7784bd93..b9a083d83 100644 --- a/public/locales/en/dataset.json +++ b/public/locales/en/dataset.json @@ -58,7 +58,7 @@ "header": "Download Options", "zip": "Download ZIP", "originalZip": "Original Format ZIP", - "archiveZip": "Archive Format (.tab) ZIP" + "archivalZip": "Archival Format (.tab) ZIP" } }, "uploadFiles": "Upload Files" diff --git a/src/files/domain/models/File.ts b/src/files/domain/models/File.ts index cc89d0437..7120feb86 100644 --- a/src/files/domain/models/File.ts +++ b/src/files/domain/models/File.ts @@ -20,7 +20,7 @@ export class FileSize { } constructor(readonly value: number, readonly unit: FileSizeUnit) { - ;[this.value, this.unit] = this.convertToLargestUnit(value, unit) + ;[this.value, this.unit] = FileSize.convertToLargestUnit(value, unit) } toString(): string { @@ -33,7 +33,7 @@ export class FileSize { return this.value * FileSize.multiplier[this.unit] } - private convertToLargestUnit(value: number, unit: FileSizeUnit): [number, FileSizeUnit] { + static convertToLargestUnit(value: number, unit: FileSizeUnit): [number, FileSizeUnit] { let convertedValue = value let convertedUnit = unit @@ -45,7 +45,7 @@ export class FileSize { return [convertedValue, convertedUnit] } - private getNextUnit(unit: FileSizeUnit): FileSizeUnit { + static getNextUnit(unit: FileSizeUnit): FileSizeUnit { switch (unit) { case FileSizeUnit.BYTES: return FileSizeUnit.KILOBYTES @@ -75,6 +75,7 @@ export class FileDownloadSize extends FileSize { readonly mode: FileDownloadMode ) { super(value, unit) + ;[this.value, this.unit] = FileDownloadSize.convertToLargestUnit(value, unit) } } diff --git a/src/sections/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.tsx b/src/sections/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.tsx index 499a62528..dee590789 100644 --- a/src/sections/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.tsx +++ b/src/sections/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.tsx @@ -66,7 +66,7 @@ const DatasetDownloadOptions = ({ }: DatasetDownloadOptionsProps) => { const { t } = useTranslation('dataset') function getFormattedFileSize(mode: FileDownloadMode): string { - const foundSize = fileDownloadSizes && fileDownloadSizes.find((size) => size.mode === mode) + const foundSize = fileDownloadSizes.find((size) => size.mode === mode) return foundSize ? foundSize.toString() : '' } @@ -77,14 +77,14 @@ const DatasetDownloadOptions = ({ {getFormattedFileSize(FileDownloadMode.ORIGINAL)}) - {t('datasetActionButtons.accessDataset.downloadOptions.archiveZip')} ( + {t('datasetActionButtons.accessDataset.downloadOptions.archivalZip')} ( {getFormattedFileSize(FileDownloadMode.ARCHIVAL)}) ) : ( {t('datasetActionButtons.accessDataset.downloadOptions.zip')} ( - {getFormattedFileSize(FileDownloadMode.ORIGINAL)}) ) + {getFormattedFileSize(FileDownloadMode.ORIGINAL)}) ) } diff --git a/src/stories/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.stories.tsx b/src/stories/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.stories.tsx index 402e9753e..52bfda20c 100644 --- a/src/stories/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.stories.tsx +++ b/src/stories/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.stories.tsx @@ -2,6 +2,7 @@ import { Meta, StoryObj } from '@storybook/react' import { WithI18next } from '../../../WithI18next' import { WithSettings } from '../../../WithSettings' import { + DatasetDownloadUrlsMother, DatasetFileDownloadSizeMother, DatasetPermissionsMother, DatasetVersionMother @@ -28,6 +29,7 @@ export const WithDownloadNotAllowed: Story = { version={DatasetVersionMother.createReleased()} permissions={DatasetPermissionsMother.createWithFilesDownloadNotAllowed()} fileDownloadSizes={[DatasetFileDownloadSizeMother.createOriginal()]} + downloadUrls={DatasetDownloadUrlsMother.create()} /> ) } @@ -38,6 +40,7 @@ export const WithoutTabularFiles: Story = { version={DatasetVersionMother.createReleased()} permissions={DatasetPermissionsMother.createWithAllAllowed()} fileDownloadSizes={[DatasetFileDownloadSizeMother.createOriginal()]} + downloadUrls={DatasetDownloadUrlsMother.create()} /> ) } @@ -51,6 +54,7 @@ export const WithTabularFiles: Story = { DatasetFileDownloadSizeMother.createArchival(), DatasetFileDownloadSizeMother.createOriginal() ]} + downloadUrls={DatasetDownloadUrlsMother.create()} /> ) } diff --git a/tests/component/dataset/domain/models/DatasetMother.ts b/tests/component/dataset/domain/models/DatasetMother.ts index 2f325a163..624c02227 100644 --- a/tests/component/dataset/domain/models/DatasetMother.ts +++ b/tests/component/dataset/domain/models/DatasetMother.ts @@ -204,12 +204,12 @@ export class DatasetFileDownloadSizeMother { ) } - static createArchival(): FileDownloadSize { - return this.create({ mode: FileDownloadMode.ARCHIVAL }) + static createArchival(props?: Partial): FileDownloadSize { + return this.create({ mode: FileDownloadMode.ARCHIVAL, ...props }) } - static createOriginal(): FileDownloadSize { - return this.create({ mode: FileDownloadMode.ORIGINAL }) + static createOriginal(props?: Partial): FileDownloadSize { + return this.create({ mode: FileDownloadMode.ORIGINAL, ...props }) } } diff --git a/tests/component/sections/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.spec.tsx b/tests/component/sections/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.spec.tsx index cfa5f5847..352a6174d 100644 --- a/tests/component/sections/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.spec.tsx +++ b/tests/component/sections/dataset/dataset-action-buttons/access-dataset-menu/AccessDatasetMenu.spec.tsx @@ -5,6 +5,7 @@ import { DatasetPermissionsMother, DatasetVersionMother } from '../../../../dataset/domain/models/DatasetMother' +import { FileSizeUnit } from '../../../../../../src/files/domain/models/File' const downloadUrls = DatasetDownloadUrlsMother.create() describe('AccessDatasetMenu', () => { @@ -91,7 +92,9 @@ describe('AccessDatasetMenu', () => { it('displays one dropdown option if there are no tabular files', () => { const version = DatasetVersionMother.createReleased() const permissions = DatasetPermissionsMother.createWithFilesDownloadAllowed() - const fileDownloadSizes = [DatasetFileDownloadSizeMother.createOriginal()] + const fileDownloadSizes = [ + DatasetFileDownloadSizeMother.createOriginal({ value: 2000, unit: FileSizeUnit.BYTES }) + ] cy.customMount( { ) cy.findByRole('button', { name: 'Access Dataset' }).should('exist') cy.findByRole('button', { name: 'Access Dataset' }).click() - cy.findByText(/Download ZIP \(\d+(\.\d+)? (B|KB|MB|GB|TB|PB)\)/) + cy.findByText('Download ZIP (2 KB)') .should('exist') .should('have.attr', 'href', downloadUrls.original) }) @@ -112,8 +115,11 @@ describe('AccessDatasetMenu', () => { const version = DatasetVersionMother.createReleased() const permissions = DatasetPermissionsMother.createWithFilesDownloadAllowed() const fileDownloadSizes = [ - DatasetFileDownloadSizeMother.createOriginal(), - DatasetFileDownloadSizeMother.createArchival() + DatasetFileDownloadSizeMother.createOriginal({ value: 2000, unit: FileSizeUnit.BYTES }), + DatasetFileDownloadSizeMother.createArchival({ + value: 43483094340394, + unit: FileSizeUnit.BYTES + }) ] cy.customMount( { ) cy.findByRole('button', { name: 'Access Dataset' }).should('exist') cy.findByRole('button', { name: 'Access Dataset' }).click() - cy.findByText(/Original Format ZIP \(\d+(\.\d+)? (B|KB|MB|GB|TB|PB)\)/) + cy.findByText('Original Format ZIP (2 KB)') .should('exist') .should('have.attr', 'href', downloadUrls.original) - cy.findByText(/Archive Format \(\.tab\) ZIP \(\d+(\.\d+)? (B|KB|MB|GB|TB|PB)\)/) + cy.findByText('Archival Format (.tab) ZIP (39.5 TB)') .should('exist') .should('have.attr', 'href', downloadUrls.archival) }) diff --git a/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx b/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx index 19c7e9a8d..3fb4dd9ae 100644 --- a/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx +++ b/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx @@ -367,7 +367,7 @@ describe('Dataset', () => { }) }) - it.only('applies filters to the Files Table in the correct order', () => { + it('applies filters to the Files Table in the correct order', () => { const files = [ FileHelper.create('csv', { description: 'Some description',