Skip to content

Commit

Permalink
fix(FilesTable): incorrect behaviour when changing page size
Browse files Browse the repository at this point in the history
  • Loading branch information
MellyGray committed Oct 31, 2023
1 parent 54ed937 commit 4e80cf8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dev-env/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
POSTGRES_VERSION=13
DATAVERSE_DB_USER=dataverse
SOLR_VERSION=9.3.0
REGISTRY=ghcr.io
REGISTRY=docker.io
2 changes: 1 addition & 1 deletion src/files/domain/models/FilePaginationInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class FilePaginationInfo {

withPageSize(pageSize: number): FilePaginationInfo {
const getNewPage = (oldPageSize: number, newPageSize: number) => {
const newPage = Math.ceil((this.page * oldPageSize) / newPageSize)
const newPage = Math.ceil(((this.page - 1) * oldPageSize + 1) / newPageSize)
return newPage > 0 ? newPage : 1
}
return new FilePaginationInfo(getNewPage(this.pageSize, pageSize), pageSize, this.totalFiles)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('DatasetFiles', () => {

cy.findByLabelText('Files per page').select('25')

cy.findByRole('columnheader', { name: '26 to 50 of 200 Files' }).should('exist')
cy.findByRole('columnheader', { name: '1 to 25 of 200 Files' }).should('exist')
})

it('renders the files table with the correct header with a different page size ', () => {
Expand All @@ -116,6 +116,81 @@ describe('DatasetFiles', () => {
cy.findByRole('columnheader', { name: '101 to 150 of 200 Files' }).should('exist')
})

it('renders the first page if there is only one page and the user changes to a lower page size', () => {
const testFilesCountInfo = FilesCountInfoMother.create({
total: 32
})
fileRepository.getAllByDatasetPersistentId = cy.stub().resolves(testFiles)
fileRepository.getFilesCountInfoByDatasetPersistentId = cy.stub().resolves(testFilesCountInfo)
fileRepository.getFilesTotalDownloadSizeByDatasetPersistentId = cy.stub().resolves(19900)

cy.customMount(
<DatasetFiles
filesRepository={fileRepository}
datasetPersistentId={datasetPersistentId}
datasetVersion={datasetVersion}
/>
)

cy.findByRole('button', { name: '1' }).should('not.exist')
cy.findByRole('button', { name: '2' }).should('exist')
cy.findByRole('button', { name: '3' }).should('exist')
cy.findByRole('button', { name: '4' }).should('exist')

cy.findByLabelText('Files per page').select('50')

cy.findByRole('button', { name: '1' }).should('not.exist')
cy.findByRole('button', { name: '2' }).should('not.exist')
cy.findByRole('button', { name: '3' }).should('not.exist')
cy.findByRole('columnheader', { name: '1 to 32 of 32 Files' }).should('exist')

cy.findByLabelText('Files per page').select('10')

cy.findByRole('button', { name: '1' }).should('not.exist')
cy.findByRole('button', { name: '2' }).should('exist')
cy.findByRole('button', { name: '3' }).should('exist')
cy.findByRole('button', { name: '4' }).should('exist')
cy.findByRole('columnheader', { name: '1 to 10 of 32 Files' }).should('exist')
})

it('renders the page that includes the first element of the current page when changing the page size', () => {
const testFilesCountInfo = FilesCountInfoMother.create({
total: 32
})
fileRepository.getAllByDatasetPersistentId = cy.stub().resolves(testFiles)
fileRepository.getFilesCountInfoByDatasetPersistentId = cy.stub().resolves(testFilesCountInfo)
fileRepository.getFilesTotalDownloadSizeByDatasetPersistentId = cy.stub().resolves(19900)

cy.customMount(
<DatasetFiles
filesRepository={fileRepository}
datasetPersistentId={datasetPersistentId}
datasetVersion={datasetVersion}
/>
)

cy.findByRole('button', { name: '1' }).should('not.exist')
cy.findByRole('button', { name: '2' }).should('exist')
cy.findByRole('button', { name: '3' }).should('exist')
cy.findByRole('button', { name: '4' }).should('exist')

cy.findByLabelText('Files per page').select('25')

cy.findByRole('button', { name: '1' }).should('not.exist')
cy.findByRole('button', { name: '2' }).should('exist')
cy.findByRole('button', { name: '3' }).should('not.exist')
cy.findByRole('columnheader', { name: '1 to 25 of 32 Files' }).should('exist')

cy.findByRole('button', { name: '2' }).click()
cy.findByLabelText('Files per page').select('10')

cy.findByRole('button', { name: '1' }).should('exist')
cy.findByRole('button', { name: '2' }).should('exist')
cy.findByRole('button', { name: '3' }).should('not.exist')
cy.findByRole('button', { name: '4' }).should('exist')
cy.findByRole('columnheader', { name: '21 to 30 of 32 Files' }).should('exist')
})

it('maintains the selection when the page changes', () => {
cy.customMount(
<DatasetFiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('useFiles', () => {
cy.wrap(fileRepository.getAllByDatasetPersistentId).should('be.calledOnceWith', 'persistentId')

cy.findByText('Loading...').should('exist')
cy.wrap(fileRepository.getUserPermissionsById).should('be.calledWith', files[0].id)
cy.wrap(fileRepository.getUserPermissionsById).should('be.called')

cy.findByText('Loading...').should('exist')
cy.findByText('Files count: 100').should('exist')
Expand Down

0 comments on commit 4e80cf8

Please sign in to comment.