From 681dcebb587ffd27afaf12c839abb0594494cc4d Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Tue, 2 Apr 2024 08:40:20 -0400 Subject: [PATCH 01/10] feat: update useDatasets.tsx to call getDatasetsWithCount useCase --- .../collection/datasets-list/useDatasets.tsx | 84 ++++++++++++++----- .../datasets-list/DatasetsList.spec.tsx | 17 ++-- 2 files changed, 72 insertions(+), 29 deletions(-) diff --git a/src/sections/collection/datasets-list/useDatasets.tsx b/src/sections/collection/datasets-list/useDatasets.tsx index 3e2504255..bc84cee9e 100644 --- a/src/sections/collection/datasets-list/useDatasets.tsx +++ b/src/sections/collection/datasets-list/useDatasets.tsx @@ -1,10 +1,12 @@ import { useEffect, useState } from 'react' import { DatasetRepository } from '../../../dataset/domain/repositories/DatasetRepository' -import { getDatasets } from '../../../dataset/domain/useCases/getDatasets' -import { getTotalDatasetsCount } from '../../../dataset/domain/useCases/getTotalDatasetsCount' +import { getDatasetsWithCount } from '../../../dataset/domain/useCases/getDatasetsWithCount' import { TotalDatasetsCount } from '../../../dataset/domain/models/TotalDatasetsCount' import { DatasetPaginationInfo } from '../../../dataset/domain/models/DatasetPaginationInfo' import { DatasetPreview } from '../../../dataset/domain/models/DatasetPreview' +import { DatasetsWithCount } from '../../../dataset/domain/models/DatasetsWithCount' +import { getTotalDatasetsCount } from '../../../dataset/domain/useCases/getTotalDatasetsCount' +import { getDatasets } from '../../../dataset/domain/useCases/getDatasets' export function useDatasets( datasetRepository: DatasetRepository, @@ -17,20 +19,6 @@ export function useDatasets( const [isLoading, setIsLoading] = useState(true) const [totalDatasetsCount, setTotalDatasetsCount] = useState() - const fetchTotalDatasetsCount: () => Promise = () => { - return getTotalDatasetsCount(datasetRepository, collectionId) - .then((totalDatasetsCount: TotalDatasetsCount) => { - setTotalDatasetsCount(totalDatasetsCount) - if (totalDatasetsCount !== paginationInfo.totalItems) { - paginationInfo = paginationInfo.withTotal(totalDatasetsCount) - onPaginationInfoChange(paginationInfo) - } - return totalDatasetsCount - }) - .catch(() => { - throw new Error('There was an error getting the datasets count info') - }) - } const fetchDatasets = (totalDatasetsCount: TotalDatasetsCount) => { if (typeof totalDatasetsCount !== 'undefined') { if (totalDatasetsCount === 0) { @@ -53,16 +41,66 @@ export function useDatasets( }) } } - - useEffect(() => { - setIsLoading(true) - - fetchTotalDatasetsCount() - .then((totalDatasetsCount) => fetchDatasets(totalDatasetsCount)) + const fetchTotalDatasetsCount: () => Promise = () => { + return getTotalDatasetsCount(datasetRepository, collectionId) + .then((totalDatasetsCount: TotalDatasetsCount) => { + setTotalDatasetsCount(totalDatasetsCount) + if (totalDatasetsCount !== paginationInfo.totalItems) { + paginationInfo = paginationInfo.withTotal(totalDatasetsCount) + onPaginationInfoChange(paginationInfo) + } + return totalDatasetsCount + }) .catch(() => { - console.error('There was an error getting the datasets') + throw new Error('There was an error getting the datasets count info') + }) + } + + const fetchDatasetsWithCount = () => { + console.log('fetching datasets with count') + console.log('collectionId', collectionId) + console.log('paginationInfo', paginationInfo) + return getDatasetsWithCount(datasetRepository, collectionId, paginationInfo) + .then((datasetsWithCount: DatasetsWithCount) => { + console.log('datasetsWithCount', datasetsWithCount) + setDatasets(datasetsWithCount.datasetPreviews) + setTotalDatasetsCount(datasetsWithCount.totalCount) + if (typeof datasetsWithCount.totalCount !== 'undefined') { + if (datasetsWithCount.totalCount === 0) { + console.log('no datasets found') + setIsLoading(false) + return Promise.resolve() + } + } + if (datasetsWithCount.totalCount !== paginationInfo.totalItems) { + paginationInfo = paginationInfo.withTotal(datasetsWithCount.totalCount) + onPaginationInfoChange(paginationInfo) + } + console.log('paginationInfo', paginationInfo) + console.log('totalPages', paginationInfo.totalPages) + if (paginationInfo.page > paginationInfo.totalPages) { + setPageNumberNotFound(true) + setIsLoading(false) + return Promise.resolve() + } + if (datasetsWithCount.totalCount !== paginationInfo.totalItems) { + paginationInfo = paginationInfo.withTotal(datasetsWithCount.totalCount) + onPaginationInfoChange(paginationInfo) + } setIsLoading(false) }) + .catch((error) => { + console.log(JSON.stringify(error)) + throw new Error('There was an error getting the datasets') + }) + } + + useEffect(() => { + setIsLoading(true) + fetchDatasetsWithCount().catch(() => { + console.error('There was an error getting the datasets') + setIsLoading(false) + }) }, [datasetRepository, paginationInfo.page]) return { diff --git a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx index 3eb42ed4b..35a2cf64f 100644 --- a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx +++ b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx @@ -2,14 +2,17 @@ import { DatasetRepository } from '../../../../../src/dataset/domain/repositorie import { DatasetsList } from '../../../../../src/sections/collection/datasets-list/DatasetsList' import { DatasetPaginationInfo } from '../../../../../src/dataset/domain/models/DatasetPaginationInfo' import { DatasetPreviewMother } from '../../../dataset/domain/models/DatasetPreviewMother' +import { DatasetsWithCount } from '../../../../../src/dataset/domain/models/DatasetsWithCount' +import { DatasetPreview } from '@iqss/dataverse-client-javascript' const datasetRepository: DatasetRepository = {} as DatasetRepository const totalDatasetsCount = 200 const datasets = DatasetPreviewMother.createMany(totalDatasetsCount) +const datasetsWithCount = { datasetPreviews: datasets, totalCount: totalDatasetsCount } describe('Datasets List', () => { beforeEach(() => { datasetRepository.getAll = cy.stub().resolves(datasets) - datasetRepository.getTotalDatasetsCount = cy.stub().resolves(totalDatasetsCount) + datasetRepository.getAllWithCount = cy.stub().resolves(datasetsWithCount) }) it('renders skeleton while loading', () => { @@ -22,16 +25,18 @@ describe('Datasets List', () => { }) it('renders no datasets message when there are no datasets', () => { - datasetRepository.getAll = cy.stub().resolves([]) + const emptyDatasets: DatasetPreview[] = [] + const emptyDatasetsWithCount = { datasetPreviews: emptyDatasets, totalCount: 0 } + datasetRepository.getAllWithCount = cy.stub().resolves(emptyDatasetsWithCount) cy.customMount() cy.findByText(/This dataverse currently has no datasets./).should('exist') }) - it('renders the datasets list', () => { + it.only('renders the datasets list', () => { cy.customMount() - cy.wrap(datasetRepository.getAll).should( + cy.wrap(datasetRepository.getAllWithCount).should( 'be.calledOnceWith', 'root', new DatasetPaginationInfo(1, 10, totalDatasetsCount) @@ -50,7 +55,7 @@ describe('Datasets List', () => { cy.findByRole('button', { name: '6' }).click() - cy.wrap(datasetRepository.getAll).should( + cy.wrap(datasetRepository.getAllWithCount).should( 'be.calledWith', 'root', new DatasetPaginationInfo(1, 10, totalDatasetsCount).goToPage(6) @@ -63,7 +68,7 @@ describe('Datasets List', () => { ) - cy.wrap(datasetRepository.getAll).should( + cy.wrap(datasetRepository.getAllWithCount).should( 'be.calledWith', 'root', new DatasetPaginationInfo(1, 10, totalDatasetsCount).goToPage(5) From 548459eab401c4a112a0a3b628ebc2a0c042c8f2 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Thu, 4 Apr 2024 16:26:29 -0400 Subject: [PATCH 02/10] refactor: (useDatasets.tsx) use getDatasetsWithCount() to get paged datasets --- .../collection/datasets-list/useDatasets.tsx | 72 +++---------------- 1 file changed, 11 insertions(+), 61 deletions(-) diff --git a/src/sections/collection/datasets-list/useDatasets.tsx b/src/sections/collection/datasets-list/useDatasets.tsx index bc84cee9e..706dc5c89 100644 --- a/src/sections/collection/datasets-list/useDatasets.tsx +++ b/src/sections/collection/datasets-list/useDatasets.tsx @@ -19,78 +19,28 @@ export function useDatasets( const [isLoading, setIsLoading] = useState(true) const [totalDatasetsCount, setTotalDatasetsCount] = useState() - const fetchDatasets = (totalDatasetsCount: TotalDatasetsCount) => { - if (typeof totalDatasetsCount !== 'undefined') { - if (totalDatasetsCount === 0) { - setIsLoading(false) - return - } - if (paginationInfo.page > paginationInfo.totalPages) { - setPageNumberNotFound(true) - setIsLoading(false) - return - } - return getDatasets(datasetRepository, collectionId, paginationInfo) - .then((datasets: DatasetPreview[]) => { - setDatasets(datasets) - setIsLoading(false) - return datasets - }) - .catch(() => { - throw new Error('There was an error getting the datasets') - }) - } - } - const fetchTotalDatasetsCount: () => Promise = () => { - return getTotalDatasetsCount(datasetRepository, collectionId) - .then((totalDatasetsCount: TotalDatasetsCount) => { - setTotalDatasetsCount(totalDatasetsCount) - if (totalDatasetsCount !== paginationInfo.totalItems) { - paginationInfo = paginationInfo.withTotal(totalDatasetsCount) - onPaginationInfoChange(paginationInfo) - } - return totalDatasetsCount - }) - .catch(() => { - throw new Error('There was an error getting the datasets count info') - }) - } - const fetchDatasetsWithCount = () => { - console.log('fetching datasets with count') - console.log('collectionId', collectionId) - console.log('paginationInfo', paginationInfo) return getDatasetsWithCount(datasetRepository, collectionId, paginationInfo) .then((datasetsWithCount: DatasetsWithCount) => { - console.log('datasetsWithCount', datasetsWithCount) - setDatasets(datasetsWithCount.datasetPreviews) - setTotalDatasetsCount(datasetsWithCount.totalCount) - if (typeof datasetsWithCount.totalCount !== 'undefined') { - if (datasetsWithCount.totalCount === 0) { - console.log('no datasets found') - setIsLoading(false) - return Promise.resolve() - } - } - if (datasetsWithCount.totalCount !== paginationInfo.totalItems) { - paginationInfo = paginationInfo.withTotal(datasetsWithCount.totalCount) - onPaginationInfoChange(paginationInfo) - } - console.log('paginationInfo', paginationInfo) - console.log('totalPages', paginationInfo.totalPages) - if (paginationInfo.page > paginationInfo.totalPages) { - setPageNumberNotFound(true) + setTotalDatasetsCount(totalDatasetsCount) + if (datasetsWithCount.totalCount === 0) { setIsLoading(false) return Promise.resolve() } - if (datasetsWithCount.totalCount !== paginationInfo.totalItems) { + if (totalDatasetsCount !== paginationInfo.totalItems) { paginationInfo = paginationInfo.withTotal(datasetsWithCount.totalCount) onPaginationInfoChange(paginationInfo) + + if (paginationInfo.page > paginationInfo.totalPages) { + setPageNumberNotFound(true) + setIsLoading(false) + return Promise.resolve() + } + setDatasets(datasetsWithCount.datasetPreviews) + setIsLoading(false) } - setIsLoading(false) }) .catch((error) => { - console.log(JSON.stringify(error)) throw new Error('There was an error getting the datasets') }) } From addd922d51eb6e3d61eb016d1d2d50e48530ded1 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Thu, 4 Apr 2024 16:27:09 -0400 Subject: [PATCH 03/10] refactor: (component tests) use stub for getDatasetsWithCount() to get paged datasets --- .../sections/collection/Collection.spec.tsx | 2 ++ .../datasets-list/DatasetsList.spec.tsx | 18 +----------------- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/tests/component/sections/collection/Collection.spec.tsx b/tests/component/sections/collection/Collection.spec.tsx index 7cd271ae0..b5680f5c5 100644 --- a/tests/component/sections/collection/Collection.spec.tsx +++ b/tests/component/sections/collection/Collection.spec.tsx @@ -10,11 +10,13 @@ const userRepository: UserRepository = {} as UserRepository const datasetRepository: DatasetRepository = {} as DatasetRepository const totalDatasetsCount = 200 const datasets = DatasetPreviewMother.createMany(totalDatasetsCount) +const datasetsWithCount = { datasetPreviews: datasets, totalCount: totalDatasetsCount } describe('Collection page', () => { beforeEach(() => { datasetRepository.getAll = cy.stub().resolves(datasets) datasetRepository.getTotalDatasetsCount = cy.stub().resolves(totalDatasetsCount) + datasetRepository.getAllWithCount = cy.stub().resolves(datasetsWithCount) }) it('renders collection title', () => { diff --git a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx index 35a2cf64f..846c69801 100644 --- a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx +++ b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx @@ -33,15 +33,9 @@ describe('Datasets List', () => { cy.findByText(/This dataverse currently has no datasets./).should('exist') }) - it.only('renders the datasets list', () => { + it('renders the datasets list', () => { cy.customMount() - cy.wrap(datasetRepository.getAllWithCount).should( - 'be.calledOnceWith', - 'root', - new DatasetPaginationInfo(1, 10, totalDatasetsCount) - ) - cy.findByText('1 to 10 of 200 Datasets').should('exist') datasets.forEach((dataset) => { cy.findByText(dataset.version.title) @@ -55,11 +49,6 @@ describe('Datasets List', () => { cy.findByRole('button', { name: '6' }).click() - cy.wrap(datasetRepository.getAllWithCount).should( - 'be.calledWith', - 'root', - new DatasetPaginationInfo(1, 10, totalDatasetsCount).goToPage(6) - ) cy.findByText('51 to 60 of 200 Datasets').should('exist') }) @@ -68,11 +57,6 @@ describe('Datasets List', () => { ) - cy.wrap(datasetRepository.getAllWithCount).should( - 'be.calledWith', - 'root', - new DatasetPaginationInfo(1, 10, totalDatasetsCount).goToPage(5) - ) cy.findByText('41 to 50 of 200 Datasets').should('exist') }) From aa206735c72fa6710331e78d0f5b85d356111868 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Thu, 4 Apr 2024 16:35:25 -0400 Subject: [PATCH 04/10] fix: lint errors --- .../collection/datasets-list/useDatasets.tsx | 12 ++++-------- .../collection/datasets-list/DatasetsList.spec.tsx | 2 -- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/src/sections/collection/datasets-list/useDatasets.tsx b/src/sections/collection/datasets-list/useDatasets.tsx index 706dc5c89..2aa14a52b 100644 --- a/src/sections/collection/datasets-list/useDatasets.tsx +++ b/src/sections/collection/datasets-list/useDatasets.tsx @@ -5,8 +5,6 @@ import { TotalDatasetsCount } from '../../../dataset/domain/models/TotalDatasets import { DatasetPaginationInfo } from '../../../dataset/domain/models/DatasetPaginationInfo' import { DatasetPreview } from '../../../dataset/domain/models/DatasetPreview' import { DatasetsWithCount } from '../../../dataset/domain/models/DatasetsWithCount' -import { getTotalDatasetsCount } from '../../../dataset/domain/useCases/getTotalDatasetsCount' -import { getDatasets } from '../../../dataset/domain/useCases/getDatasets' export function useDatasets( datasetRepository: DatasetRepository, @@ -20,8 +18,8 @@ export function useDatasets( const [totalDatasetsCount, setTotalDatasetsCount] = useState() const fetchDatasetsWithCount = () => { - return getDatasetsWithCount(datasetRepository, collectionId, paginationInfo) - .then((datasetsWithCount: DatasetsWithCount) => { + return getDatasetsWithCount(datasetRepository, collectionId, paginationInfo).then( + (datasetsWithCount: DatasetsWithCount) => { setTotalDatasetsCount(totalDatasetsCount) if (datasetsWithCount.totalCount === 0) { setIsLoading(false) @@ -39,10 +37,8 @@ export function useDatasets( setDatasets(datasetsWithCount.datasetPreviews) setIsLoading(false) } - }) - .catch((error) => { - throw new Error('There was an error getting the datasets') - }) + } + ) } useEffect(() => { diff --git a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx index 846c69801..36ffc56b6 100644 --- a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx +++ b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx @@ -1,8 +1,6 @@ import { DatasetRepository } from '../../../../../src/dataset/domain/repositories/DatasetRepository' import { DatasetsList } from '../../../../../src/sections/collection/datasets-list/DatasetsList' -import { DatasetPaginationInfo } from '../../../../../src/dataset/domain/models/DatasetPaginationInfo' import { DatasetPreviewMother } from '../../../dataset/domain/models/DatasetPreviewMother' -import { DatasetsWithCount } from '../../../../../src/dataset/domain/models/DatasetsWithCount' import { DatasetPreview } from '@iqss/dataverse-client-javascript' const datasetRepository: DatasetRepository = {} as DatasetRepository From b57886d22e039c60cd4332c08ac5f3ce1d4ff733 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Thu, 4 Apr 2024 21:45:14 -0400 Subject: [PATCH 05/10] DatasetsList.spec.tsx: show commented out assertion that is failing --- .../collection/datasets-list/DatasetsList.spec.tsx | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx index 36ffc56b6..8477a1eca 100644 --- a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx +++ b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx @@ -2,6 +2,7 @@ import { DatasetRepository } from '../../../../../src/dataset/domain/repositorie import { DatasetsList } from '../../../../../src/sections/collection/datasets-list/DatasetsList' import { DatasetPreviewMother } from '../../../dataset/domain/models/DatasetPreviewMother' import { DatasetPreview } from '@iqss/dataverse-client-javascript' +import { DatasetPaginationInfo } from '../../../../../src/dataset/domain/models/DatasetPaginationInfo' const datasetRepository: DatasetRepository = {} as DatasetRepository const totalDatasetsCount = 200 @@ -33,7 +34,13 @@ describe('Datasets List', () => { it('renders the datasets list', () => { cy.customMount() - + /* Commented out because of the assertion fails, even though the test passes, + and the stub is called + cy.wrap(datasetRepository.getAllWithCount).should( + 'be.calledOnceWith', + 'root', + new DatasetPaginationInfo(1, 10, totalDatasetsCount) + ) */ cy.findByText('1 to 10 of 200 Datasets').should('exist') datasets.forEach((dataset) => { cy.findByText(dataset.version.title) From 2c3aab3ddfa71a64c3fe1a8ddf3d9f883aee599b Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Thu, 4 Apr 2024 21:59:15 -0400 Subject: [PATCH 06/10] fix: comment out unused import (for commented out assertion) --- .../sections/collection/datasets-list/DatasetsList.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx index 8477a1eca..56a4dccd8 100644 --- a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx +++ b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx @@ -2,7 +2,7 @@ import { DatasetRepository } from '../../../../../src/dataset/domain/repositorie import { DatasetsList } from '../../../../../src/sections/collection/datasets-list/DatasetsList' import { DatasetPreviewMother } from '../../../dataset/domain/models/DatasetPreviewMother' import { DatasetPreview } from '@iqss/dataverse-client-javascript' -import { DatasetPaginationInfo } from '../../../../../src/dataset/domain/models/DatasetPaginationInfo' +//import { DatasetPaginationInfo } from '../../../../../src/dataset/domain/models/DatasetPaginationInfo' const datasetRepository: DatasetRepository = {} as DatasetRepository const totalDatasetsCount = 200 From 96541818a532615c30cb5fe34ba4643bfa224b45 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Wed, 10 Apr 2024 14:20:42 -0400 Subject: [PATCH 07/10] fix: remove getAll() and getTotalDatasetsCount() from DatasetRepository interface and useCases --- .../domain/repositories/DatasetRepository.ts | 4 ---- src/dataset/domain/useCases/getDatasets.ts | 13 ----------- .../domain/useCases/getTotalDatasetsCount.ts | 11 ---------- .../DatasetJSDataverseRepository.ts | 21 ------------------ .../dataset/DatasetErrorMockRepository.ts | 18 --------------- .../dataset/DatasetLoadingMockRepository.ts | 5 ----- src/stories/dataset/DatasetMockRepository.ts | 18 --------------- .../dataset/NoDatasetsMockRepository.ts | 18 --------------- .../DatasetJSDataverseRepository.spec.ts | 22 +++++-------------- 9 files changed, 6 insertions(+), 124 deletions(-) delete mode 100644 src/dataset/domain/useCases/getDatasets.ts delete mode 100644 src/dataset/domain/useCases/getTotalDatasetsCount.ts diff --git a/src/dataset/domain/repositories/DatasetRepository.ts b/src/dataset/domain/repositories/DatasetRepository.ts index a2cf92481..92c16eb8d 100644 --- a/src/dataset/domain/repositories/DatasetRepository.ts +++ b/src/dataset/domain/repositories/DatasetRepository.ts @@ -1,15 +1,11 @@ import { Dataset } from '../models/Dataset' -import { TotalDatasetsCount } from '../models/TotalDatasetsCount' import { DatasetPaginationInfo } from '../models/DatasetPaginationInfo' -import { DatasetPreview } from '../models/DatasetPreview' import { DatasetDTO } from '../useCases/DTOs/DatasetDTO' import { DatasetsWithCount } from '../models/DatasetsWithCount' export interface DatasetRepository { getByPersistentId: (persistentId: string, version?: string) => Promise getByPrivateUrlToken: (privateUrlToken: string) => Promise - getAll: (collectionId: string, paginationInfo: DatasetPaginationInfo) => Promise - getTotalDatasetsCount: (collectionId: string) => Promise create: (dataset: DatasetDTO) => Promise<{ persistentId: string }> getAllWithCount: ( collectionId: string, diff --git a/src/dataset/domain/useCases/getDatasets.ts b/src/dataset/domain/useCases/getDatasets.ts deleted file mode 100644 index 459550ffc..000000000 --- a/src/dataset/domain/useCases/getDatasets.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { DatasetRepository } from '../repositories/DatasetRepository' -import { DatasetPaginationInfo } from '../models/DatasetPaginationInfo' -import { DatasetPreview } from '../models/DatasetPreview' - -export async function getDatasets( - datasetRepository: DatasetRepository, - collectionId: string, - paginationInfo: DatasetPaginationInfo -): Promise { - return datasetRepository.getAll(collectionId, paginationInfo).catch((error: Error) => { - throw new Error(error.message) - }) -} diff --git a/src/dataset/domain/useCases/getTotalDatasetsCount.ts b/src/dataset/domain/useCases/getTotalDatasetsCount.ts deleted file mode 100644 index 2950cf689..000000000 --- a/src/dataset/domain/useCases/getTotalDatasetsCount.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { DatasetRepository } from '../repositories/DatasetRepository' -import { TotalDatasetsCount } from '../models/TotalDatasetsCount' - -export async function getTotalDatasetsCount( - datasetRepository: DatasetRepository, - collectionId: string -): Promise { - return datasetRepository.getTotalDatasetsCount(collectionId).catch((error: Error) => { - throw new Error(error.message) - }) -} diff --git a/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts b/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts index 38713fdb1..6e8e5c3ab 100644 --- a/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts +++ b/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts @@ -22,9 +22,7 @@ import { WriteError } from '@iqss/dataverse-client-javascript' import { JSDatasetMapper } from '../mappers/JSDatasetMapper' -import { TotalDatasetsCount } from '../../domain/models/TotalDatasetsCount' import { DatasetPaginationInfo } from '../../domain/models/DatasetPaginationInfo' -import { DatasetPreview } from '../../domain/models/DatasetPreview' import { JSDatasetPreviewMapper } from '../mappers/JSDatasetPreviewMapper' import { DatasetDTO } from '../../domain/useCases/DTOs/DatasetDTO' import { DatasetDTOMapper } from '../mappers/DatasetDTOMapper' @@ -33,25 +31,6 @@ import { DatasetsWithCount } from '../../domain/models/DatasetsWithCount' const includeDeaccessioned = true export class DatasetJSDataverseRepository implements DatasetRepository { - getAll(collectionId: string, paginationInfo: DatasetPaginationInfo): Promise { - return getAllDatasetPreviews - .execute(paginationInfo.pageSize, paginationInfo.offset, collectionId) - .then((subset: DatasetPreviewSubset) => { - return subset.datasetPreviews.map((datasetPreview: JSDatasetPreview) => - JSDatasetPreviewMapper.toDatasetPreview(datasetPreview) - ) - }) - } - - getTotalDatasetsCount(collectionId: string): Promise { - // TODO: refactor this so we don't make the same call twice? - return getAllDatasetPreviews - .execute(10, 0, collectionId) - .then((subset: DatasetPreviewSubset) => { - return subset.totalDatasetCount - }) - } - getAllWithCount( collectionId: string, paginationInfo: DatasetPaginationInfo diff --git a/src/stories/dataset/DatasetErrorMockRepository.ts b/src/stories/dataset/DatasetErrorMockRepository.ts index fba8bcc80..ec2f1d74f 100644 --- a/src/stories/dataset/DatasetErrorMockRepository.ts +++ b/src/stories/dataset/DatasetErrorMockRepository.ts @@ -1,29 +1,11 @@ import { Dataset } from '../../dataset/domain/models/Dataset' import { DatasetMockRepository } from './DatasetMockRepository' -import { TotalDatasetsCount } from '../../dataset/domain/models/TotalDatasetsCount' import { DatasetPaginationInfo } from '../../dataset/domain/models/DatasetPaginationInfo' -import { DatasetPreview } from '../../dataset/domain/models/DatasetPreview' import { DatasetsWithCount } from '../../dataset/domain/models/DatasetsWithCount' import { DatasetDTO } from '../../dataset/domain/useCases/DTOs/DatasetDTO' import { FakerHelper } from '../../../tests/component/shared/FakerHelper' export class DatasetErrorMockRepository implements DatasetMockRepository { - getAll(_collectionId: string, _paginationInfo: DatasetPaginationInfo): Promise { - return new Promise((_resolve, reject) => { - setTimeout(() => { - reject('Error thrown from mock') - }, 1000) - }) - } - - getTotalDatasetsCount(_collectionId: string): Promise { - return new Promise((_resolve, reject) => { - setTimeout(() => { - reject('Error thrown from mock') - }, 1000) - }) - } - getAllWithCount: ( collectionId: string, paginationInfo: DatasetPaginationInfo diff --git a/src/stories/dataset/DatasetLoadingMockRepository.ts b/src/stories/dataset/DatasetLoadingMockRepository.ts index 6b0635966..c7347ac83 100644 --- a/src/stories/dataset/DatasetLoadingMockRepository.ts +++ b/src/stories/dataset/DatasetLoadingMockRepository.ts @@ -1,13 +1,8 @@ import { DatasetMockRepository } from './DatasetMockRepository' import { DatasetPaginationInfo } from '../../dataset/domain/models/DatasetPaginationInfo' -import { DatasetPreview } from '../../dataset/domain/models/DatasetPreview' import { DatasetsWithCount } from '../../dataset/domain/models/DatasetsWithCount' export class DatasetLoadingMockRepository extends DatasetMockRepository { - getAll(_collectionId: string, _paginationInfo: DatasetPaginationInfo): Promise { - return new Promise(() => {}) - } - getDatasetsWithCount: ( collectionId: string, paginationInfo: DatasetPaginationInfo diff --git a/src/stories/dataset/DatasetMockRepository.ts b/src/stories/dataset/DatasetMockRepository.ts index 95e170658..f6c8b1f0a 100644 --- a/src/stories/dataset/DatasetMockRepository.ts +++ b/src/stories/dataset/DatasetMockRepository.ts @@ -1,30 +1,12 @@ import { Dataset } from '../../dataset/domain/models/Dataset' import { DatasetRepository } from '../../dataset/domain/repositories/DatasetRepository' import { DatasetMother } from '../../../tests/component/dataset/domain/models/DatasetMother' -import { TotalDatasetsCount } from '../../dataset/domain/models/TotalDatasetsCount' import { DatasetPaginationInfo } from '../../dataset/domain/models/DatasetPaginationInfo' -import { DatasetPreview } from '../../dataset/domain/models/DatasetPreview' import { DatasetPreviewMother } from '../../../tests/component/dataset/domain/models/DatasetPreviewMother' import { DatasetDTO } from '../../dataset/domain/useCases/DTOs/DatasetDTO' import { DatasetsWithCount } from '../../dataset/domain/models/DatasetsWithCount' import { FakerHelper } from '../../../tests/component/shared/FakerHelper' export class DatasetMockRepository implements DatasetRepository { - getAll(_collectionId: string, paginationInfo: DatasetPaginationInfo): Promise { - return new Promise((resolve) => { - setTimeout(() => { - resolve(DatasetPreviewMother.createManyRealistic(paginationInfo.pageSize)) - }, FakerHelper.loadingTimout()) - }) - } - - getTotalDatasetsCount(_collectionId: string): Promise { - return new Promise((resolve) => { - setTimeout(() => { - resolve(200) - }, FakerHelper.loadingTimout()) - }) - } - getAllWithCount: ( collectionId: string, paginationInfo: DatasetPaginationInfo diff --git a/src/stories/dataset/NoDatasetsMockRepository.ts b/src/stories/dataset/NoDatasetsMockRepository.ts index 66efc7c1e..1f2f96575 100644 --- a/src/stories/dataset/NoDatasetsMockRepository.ts +++ b/src/stories/dataset/NoDatasetsMockRepository.ts @@ -1,25 +1,7 @@ import { DatasetMockRepository } from './DatasetMockRepository' import { DatasetPaginationInfo } from '../../dataset/domain/models/DatasetPaginationInfo' -import { DatasetPreview } from '../../dataset/domain/models/DatasetPreview' -import { TotalDatasetsCount } from '../../dataset/domain/models/TotalDatasetsCount' import { DatasetsWithCount } from '../../dataset/domain/models/DatasetsWithCount' export class NoDatasetsMockRepository extends DatasetMockRepository { - getAll(_collectionId: string, _paginationInfo: DatasetPaginationInfo): Promise { - return new Promise((resolve) => { - setTimeout(() => { - resolve([]) - }, 1000) - }) - } - - getTotalDatasetsCount(_collectionId: string): Promise { - return new Promise((resolve) => { - setTimeout(() => { - resolve(0) - }, 1000) - }) - } - getAllWithCount: ( collectionId: string, paginationInfo: DatasetPaginationInfo diff --git a/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts b/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts index cb891da80..18a88ed8a 100644 --- a/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts +++ b/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts @@ -290,28 +290,18 @@ describe('Dataset JSDataverse Repository', () => { }) }) - it('gets the total dataset count', async () => { - await DatasetHelper.destroyAll() - await datasetRepository.getTotalDatasetsCount('root').then((count) => { - expect(count).to.equal(0) - }) - await DatasetHelper.createAndPublish() - - await datasetRepository.getTotalDatasetsCount('root').then((count) => { - expect(count).to.equal(1) - }) - }) - it('gets the DatasetPreview', async () => { await DatasetHelper.destroyAll() const datasetResponse = await DatasetHelper.createAndPublish() const paginationInfo = new DatasetPaginationInfo(1, 20) - await datasetRepository.getAll('root', paginationInfo).then((datasetPreview) => { - expect(datasetPreview.length).to.equal(1) - expect(datasetPreview[0].version.title).to.equal("Darwin's Finches") - expect(datasetPreview[0].persistentId).to.equal(datasetResponse.persistentId) + await datasetRepository.getAllWithCount('root', paginationInfo).then((datasetsWithCount) => { + expect(datasetsWithCount.totalCount).to.equal(1) + expect(datasetsWithCount.datasetPreviews[0].version.title).to.equal("Darwin's Finches") + expect(datasetsWithCount.datasetPreviews[0].persistentId).to.equal( + datasetResponse.persistentId + ) }) }) From 79dc551a032058f635870be387d730a8d45f0918 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Wed, 10 Apr 2024 14:43:24 -0400 Subject: [PATCH 08/10] fix: remove getAll() restore tests of getAllWithCount stub call --- .../datasets-list/DatasetsList.spec.tsx | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx index 56a4dccd8..869b70b57 100644 --- a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx +++ b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx @@ -2,7 +2,7 @@ import { DatasetRepository } from '../../../../../src/dataset/domain/repositorie import { DatasetsList } from '../../../../../src/sections/collection/datasets-list/DatasetsList' import { DatasetPreviewMother } from '../../../dataset/domain/models/DatasetPreviewMother' import { DatasetPreview } from '@iqss/dataverse-client-javascript' -//import { DatasetPaginationInfo } from '../../../../../src/dataset/domain/models/DatasetPaginationInfo' +import { DatasetPaginationInfo } from '../../../../../src/dataset/domain/models/DatasetPaginationInfo' const datasetRepository: DatasetRepository = {} as DatasetRepository const totalDatasetsCount = 200 @@ -10,7 +10,6 @@ const datasets = DatasetPreviewMother.createMany(totalDatasetsCount) const datasetsWithCount = { datasetPreviews: datasets, totalCount: totalDatasetsCount } describe('Datasets List', () => { beforeEach(() => { - datasetRepository.getAll = cy.stub().resolves(datasets) datasetRepository.getAllWithCount = cy.stub().resolves(datasetsWithCount) }) @@ -34,13 +33,12 @@ describe('Datasets List', () => { it('renders the datasets list', () => { cy.customMount() - /* Commented out because of the assertion fails, even though the test passes, - and the stub is called + cy.wrap(datasetRepository.getAllWithCount).should( 'be.calledOnceWith', 'root', - new DatasetPaginationInfo(1, 10, totalDatasetsCount) - ) */ + new DatasetPaginationInfo(1, 10, 0) + ) cy.findByText('1 to 10 of 200 Datasets').should('exist') datasets.forEach((dataset) => { cy.findByText(dataset.version.title) @@ -53,7 +51,11 @@ describe('Datasets List', () => { cy.customMount() cy.findByRole('button', { name: '6' }).click() - + cy.wrap(datasetRepository.getAllWithCount).should( + 'be.calledWith', + 'root', + new DatasetPaginationInfo(1, 10, 0).goToPage(6) + ) cy.findByText('51 to 60 of 200 Datasets').should('exist') }) @@ -61,7 +63,11 @@ describe('Datasets List', () => { cy.customMount( ) - + cy.wrap(datasetRepository.getAllWithCount).should( + 'be.calledWith', + 'root', + new DatasetPaginationInfo(1, 10, 0).goToPage(5) + ) cy.findByText('41 to 50 of 200 Datasets').should('exist') }) From 7d8955326ab1600b9c7eb80d4065da7e251eb27d Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Wed, 10 Apr 2024 17:30:05 -0400 Subject: [PATCH 09/10] fix: totalItems parameter --- .../sections/collection/datasets-list/DatasetsList.spec.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx index 869b70b57..c87fe16ba 100644 --- a/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx +++ b/tests/component/sections/collection/datasets-list/DatasetsList.spec.tsx @@ -54,7 +54,7 @@ describe('Datasets List', () => { cy.wrap(datasetRepository.getAllWithCount).should( 'be.calledWith', 'root', - new DatasetPaginationInfo(1, 10, 0).goToPage(6) + new DatasetPaginationInfo(1, 10, 200).goToPage(6) ) cy.findByText('51 to 60 of 200 Datasets').should('exist') }) From 762f6b33f1f07a4f906966cde80e3c4a52fe5836 Mon Sep 17 00:00:00 2001 From: Ellen Kraffmiller Date: Wed, 10 Apr 2024 18:34:18 -0400 Subject: [PATCH 10/10] fix: run test after destroyAll() completes --- .../DatasetJSDataverseRepository.spec.ts | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts b/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts index 18a88ed8a..6d26089f3 100644 --- a/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts +++ b/tests/e2e-integration/integration/datasets/DatasetJSDataverseRepository.spec.ts @@ -290,18 +290,21 @@ describe('Dataset JSDataverse Repository', () => { }) }) - it('gets the DatasetPreview', async () => { - await DatasetHelper.destroyAll() - - const datasetResponse = await DatasetHelper.createAndPublish() - const paginationInfo = new DatasetPaginationInfo(1, 20) - - await datasetRepository.getAllWithCount('root', paginationInfo).then((datasetsWithCount) => { - expect(datasetsWithCount.totalCount).to.equal(1) - expect(datasetsWithCount.datasetPreviews[0].version.title).to.equal("Darwin's Finches") - expect(datasetsWithCount.datasetPreviews[0].persistentId).to.equal( - datasetResponse.persistentId - ) + it('gets the DatasetPreview', () => { + cy.wrap(DatasetHelper.destroyAll()).then(() => { + return DatasetHelper.createAndPublish().then((datasetResponse) => { + const paginationInfo = new DatasetPaginationInfo(1, 20) + + return datasetRepository + .getAllWithCount('root', paginationInfo) + .then((datasetsWithCount) => { + expect(datasetsWithCount.totalCount).to.equal(1) + expect(datasetsWithCount.datasetPreviews[0].version.title).to.equal("Darwin's Finches") + expect(datasetsWithCount.datasetPreviews[0].persistentId).to.equal( + datasetResponse.persistentId + ) + }) + }) }) })