Skip to content

Commit

Permalink
feat(IntegrationDatasetDownload): fix size conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
MellyGray committed Dec 12, 2023
1 parent 6312255 commit 0e3a49a
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion public/locales/en/dataset.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions src/files/domain/models/File.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -75,6 +75,7 @@ export class FileDownloadSize extends FileSize {
readonly mode: FileDownloadMode
) {
super(value, unit)
;[this.value, this.unit] = FileDownloadSize.convertToLargestUnit(value, unit)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() : ''
}

Expand All @@ -77,14 +77,14 @@ const DatasetDownloadOptions = ({
{getFormattedFileSize(FileDownloadMode.ORIGINAL)})
</DropdownButtonItem>
<DropdownButtonItem href={downloadUrls[FileDownloadMode.ARCHIVAL]}>
{t('datasetActionButtons.accessDataset.downloadOptions.archiveZip')} (
{t('datasetActionButtons.accessDataset.downloadOptions.archivalZip')} (
{getFormattedFileSize(FileDownloadMode.ARCHIVAL)})
</DropdownButtonItem>
</>
) : (
<DropdownButtonItem href={downloadUrls[FileDownloadMode.ORIGINAL]}>
{t('datasetActionButtons.accessDataset.downloadOptions.zip')} (
{getFormattedFileSize(FileDownloadMode.ORIGINAL)}) )
{getFormattedFileSize(FileDownloadMode.ORIGINAL)})
</DropdownButtonItem>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Meta, StoryObj } from '@storybook/react'
import { WithI18next } from '../../../WithI18next'
import { WithSettings } from '../../../WithSettings'
import {
DatasetDownloadUrlsMother,
DatasetFileDownloadSizeMother,
DatasetPermissionsMother,
DatasetVersionMother
Expand All @@ -28,6 +29,7 @@ export const WithDownloadNotAllowed: Story = {
version={DatasetVersionMother.createReleased()}
permissions={DatasetPermissionsMother.createWithFilesDownloadNotAllowed()}
fileDownloadSizes={[DatasetFileDownloadSizeMother.createOriginal()]}
downloadUrls={DatasetDownloadUrlsMother.create()}
/>
)
}
Expand All @@ -38,6 +40,7 @@ export const WithoutTabularFiles: Story = {
version={DatasetVersionMother.createReleased()}
permissions={DatasetPermissionsMother.createWithAllAllowed()}
fileDownloadSizes={[DatasetFileDownloadSizeMother.createOriginal()]}
downloadUrls={DatasetDownloadUrlsMother.create()}
/>
)
}
Expand All @@ -51,6 +54,7 @@ export const WithTabularFiles: Story = {
DatasetFileDownloadSizeMother.createArchival(),
DatasetFileDownloadSizeMother.createOriginal()
]}
downloadUrls={DatasetDownloadUrlsMother.create()}
/>
)
}
8 changes: 4 additions & 4 deletions tests/component/dataset/domain/models/DatasetMother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,12 +204,12 @@ export class DatasetFileDownloadSizeMother {
)
}

static createArchival(): FileDownloadSize {
return this.create({ mode: FileDownloadMode.ARCHIVAL })
static createArchival(props?: Partial<FileDownloadSize>): FileDownloadSize {
return this.create({ mode: FileDownloadMode.ARCHIVAL, ...props })
}

static createOriginal(): FileDownloadSize {
return this.create({ mode: FileDownloadMode.ORIGINAL })
static createOriginal(props?: Partial<FileDownloadSize>): FileDownloadSize {
return this.create({ mode: FileDownloadMode.ORIGINAL, ...props })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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(
<AccessDatasetMenu
fileDownloadSizes={fileDownloadSizes}
Expand All @@ -103,7 +106,7 @@ describe('AccessDatasetMenu', () => {
)
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)
})
Expand All @@ -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(
<AccessDatasetMenu
Expand All @@ -126,10 +132,10 @@ describe('AccessDatasetMenu', () => {
)
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)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 0e3a49a

Please sign in to comment.