Skip to content

Commit

Permalink
Merge pull request #212 from IQSS/208/fix-change-page-size-of-the-fil…
Browse files Browse the repository at this point in the history
…es-table

208 - Fix Files table incorrect behavior when changing page size
  • Loading branch information
GPortas authored Nov 29, 2023
2 parents 0c9a2b8 + 63d69d2 commit e24eae4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
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

0 comments on commit e24eae4

Please sign in to comment.