diff --git a/test/functional/collections/GetMyDataCollectionItems.test.ts b/test/functional/collections/GetMyDataCollectionItems.test.ts index 5a9c43ec..94e2d672 100644 --- a/test/functional/collections/GetMyDataCollectionItems.test.ts +++ b/test/functional/collections/GetMyDataCollectionItems.test.ts @@ -3,8 +3,7 @@ import { createDataset, CreatedDatasetIdentifiers, CollectionPreview, - getMyDataCollectionItems, - ReadError + getMyDataCollectionItems } from '../../../src' import { TestConstants } from '../../testHelpers/TestConstants' import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' @@ -66,136 +65,213 @@ describe('execute', () => { throw new Error('Tests afterAll(): Error while deleting test collection') } }) - test('should return error message when repository returns empty item subset', async () => { - expect.assertions(2) - let readError: ReadError | undefined = undefined - try { - await getMyDataCollectionItems.execute( - testRoleIds, - testCollectionItemTypes, - [PublicationStatus.Deaccessioned], - undefined, - undefined, - undefined - ) - throw new Error('Use case should throw an error') - } catch (error) { - readError = error as ReadError - } finally { - expect(readError).toBeInstanceOf(ReadError) - expect(readError?.message).toEqual( - 'There was an error when reading the resource. Reason was: Sorry, no results were found.' - ) - } - }), - test('should return items when valid roles,collection types, and publishingStatuses are provided', async () => { - // Give enough time to Solr for indexing - await new Promise((resolve) => setTimeout(resolve, 5000)) + test('should return an empty item subset when repository returns no results', async () => { + const actual = await getMyDataCollectionItems.execute( + testRoleIds, + testCollectionItemTypes, + [PublicationStatus.Deaccessioned], + undefined, + undefined, + 'no-results-for-get-my-data-collection-items' + ) - try { - const actual = await getMyDataCollectionItems.execute( - testRoleIds, - testCollectionItemTypes, - testPublishingStatuses, - undefined, - undefined, - testCollectionAlias - ) + expect(actual.items).toEqual([]) + expect(actual.totalItemCount).toBe(0) + expect(actual.publicationStatusCounts).toEqual([ + { + publicationStatus: 'Published', + count: 0 + }, + { + publicationStatus: 'Unpublished', + count: 0 + }, + { + publicationStatus: 'Draft', + count: 0 + }, + { + publicationStatus: 'In Review', + count: 0 + }, + { + publicationStatus: 'Deaccessioned', + count: 0 + } + ]) + expect(actual.countPerObjectType).toEqual({ + collections: 0, + datasets: 0, + files: 0 + }) + }) - const actualCollectionPreview = actual.items[0] as CollectionPreview - expect(actualCollectionPreview.alias).toBe(testCollectionAlias) + test('should return items when valid roles,collection types, and publishingStatuses are provided', async () => { + // Give enough time to Solr for indexing + await new Promise((resolve) => setTimeout(resolve, 5000)) + + const actual = await getMyDataCollectionItems.execute( + testRoleIds, + testCollectionItemTypes, + testPublishingStatuses, + undefined, + undefined, + testCollectionAlias + ) - expect(actual.totalItemCount).toBe(1) - expect(actual.publicationStatusCounts).toEqual([ - { - publicationStatus: 'Published', - count: 0 - }, - { - publicationStatus: 'Unpublished', - count: 1 - }, - { - publicationStatus: 'Draft', - count: 0 - }, - { - publicationStatus: 'In Review', - count: 0 - }, - { - publicationStatus: 'Deaccessioned', - count: 0 - } - ]) - expect(actual.countPerObjectType).toEqual({ - collections: 1, - datasets: 0, - files: 0 - }) - } catch (error) { - console.log(error) - throw new Error('Item subset should be retrieved') + const actualCollectionPreview = actual.items[0] as CollectionPreview + expect(actualCollectionPreview.alias).toBe(testCollectionAlias) + + expect(actual.totalItemCount).toBe(1) + expect(actual.publicationStatusCounts).toEqual([ + { + publicationStatus: 'Published', + count: 0 + }, + { + publicationStatus: 'Unpublished', + count: 1 + }, + { + publicationStatus: 'Draft', + count: 0 + }, + { + publicationStatus: 'In Review', + count: 0 + }, + { + publicationStatus: 'Deaccessioned', + count: 0 } + ]) + expect(actual.countPerObjectType).toEqual({ + collections: 1, + datasets: 0, + files: 0 }) + }) - test('should return an error message when no role is specified', async () => { - expect.assertions(2) - let readError: ReadError | undefined = undefined - try { - await getMyDataCollectionItems.execute([], [], [], undefined, undefined, undefined) - throw new Error('Use case should throw an error') - } catch (error) { - readError = error as ReadError - } finally { - expect(readError).toBeInstanceOf(ReadError) - expect(readError?.message).toEqual( - `There was an error when reading the resource. Reason was: No results. Please select at least one Role.` - ) - } + test('should return an empty item subset when no role is specified', async () => { + const actual = await getMyDataCollectionItems.execute( + [], + [], + [], + undefined, + undefined, + undefined + ) + + expect(actual.items).toEqual([]) + expect(actual.totalItemCount).toBe(0) + expect(actual.publicationStatusCounts).toEqual([ + { + publicationStatus: 'Published', + count: 0 + }, + { + publicationStatus: 'Unpublished', + count: 0 + }, + { + publicationStatus: 'Draft', + count: 0 + }, + { + publicationStatus: 'In Review', + count: 0 + }, + { + publicationStatus: 'Deaccessioned', + count: 0 + } + ]) + expect(actual.countPerObjectType).toEqual({ + collections: 0, + datasets: 0, + files: 0 + }) }) - test('should return an error message when no publication status is specified', async () => { - expect.assertions(2) - let readError: ReadError | undefined = undefined - try { - await getMyDataCollectionItems.execute( - testRoleIds, - testCollectionItemTypes, - [], - undefined, - undefined, - undefined - ) - throw new Error('Use case should throw an error') - } catch (error) { - readError = error as ReadError - } finally { - expect(readError).toBeInstanceOf(ReadError) - expect(readError?.message).toEqual( - `There was an error when reading the resource. Reason was: No user found for: "Published, Unpublished, Draft, In Review, Deaccessioned"` - ) - } + + test('should return an empty item subset when no publication status is specified', async () => { + const actual = await getMyDataCollectionItems.execute( + testRoleIds, + testCollectionItemTypes, + [], + undefined, + undefined, + undefined + ) + + expect(actual.items).toEqual([]) + expect(actual.totalItemCount).toBe(0) + expect(actual.publicationStatusCounts).toEqual([ + { + publicationStatus: 'Published', + count: 0 + }, + { + publicationStatus: 'Unpublished', + count: 0 + }, + { + publicationStatus: 'Draft', + count: 0 + }, + { + publicationStatus: 'In Review', + count: 0 + }, + { + publicationStatus: 'Deaccessioned', + count: 0 + } + ]) + expect(actual.countPerObjectType).toEqual({ + collections: 0, + datasets: 0, + files: 0 + }) }) - test('should an error message when no collection type is specified', async () => { - expect.assertions(2) - let readError: ReadError | undefined = undefined - try { - await getMyDataCollectionItems.execute( - testRoleIds, - testCollectionItemTypes, - [], - undefined, - undefined, - undefined - ) - throw new Error('Use case should throw an error') - } catch (error) { - readError = error as ReadError - } finally { - expect(readError).toBeInstanceOf(ReadError) - expect(readError?.message).toEqual( - `There was an error when reading the resource. Reason was: No user found for: "Published, Unpublished, Draft, In Review, Deaccessioned"` - ) - } + + test('should return an empty item subset when no collection type is specified', async () => { + const actual = await getMyDataCollectionItems.execute( + testRoleIds, + [], + testPublishingStatuses, + undefined, + undefined, + undefined + ) + + expect(actual.items).toEqual([]) + expect(actual.totalItemCount).toBe(0) + expect(actual.publicationStatusCounts).toEqual([ + { + publicationStatus: 'Published', + count: 0 + }, + { + publicationStatus: 'Unpublished', + count: 0 + }, + { + publicationStatus: 'Draft', + count: 0 + }, + { + publicationStatus: 'In Review', + count: 0 + }, + { + publicationStatus: 'Deaccessioned', + count: 0 + } + ]) + expect(actual.countPerObjectType).toEqual({ + collections: 0, + datasets: 0, + files: 0 + }) }) }) diff --git a/test/integration/collections/CollectionsRepository.test.ts b/test/integration/collections/CollectionsRepository.test.ts index 43247978..56a24650 100644 --- a/test/integration/collections/CollectionsRepository.test.ts +++ b/test/integration/collections/CollectionsRepository.test.ts @@ -1720,13 +1720,15 @@ describe('CollectionsRepository', () => { describe('getMyDataCollectionItems', () => { let testDatasetIds: CreatedDatasetIdentifiers + let testSubCollectionCreated = false const testTextFile1Name = 'test-file-2.txt' const testSubCollectionAlias = 'collectionsRepositoryMyDataCollection' const testCollectionName = 'Scientific Research' + const testUserName = `myDataUser${Date.now()}` beforeAll(async () => { const createSuperUser = true - const myDataUserApiToken = await createApiTokenViaApi('myDataUser', createSuperUser) + const myDataUserApiToken = await createApiTokenViaApi(testUserName, createSuperUser) ApiConfig.init( TestConstants.TEST_API_URL, DataverseApiAuthMechanism.API_KEY, @@ -1743,6 +1745,7 @@ describe('CollectionsRepository', () => { `Tests beforeAll(): Error while creating subcollection ${testSubCollectionAlias}` ) }) + testSubCollectionCreated = true try { testDatasetIds = await createDataset.execute( TestConstants.TEST_NEW_DATASET_DTO, @@ -1757,19 +1760,23 @@ describe('CollectionsRepository', () => { }) afterAll(async () => { - try { - await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId) - } catch (error) { - throw new Error( - `Tests afterAll(): Error while deleting test dataset ${testDatasetIds.numericId}` - ) + if (testDatasetIds) { + try { + await deleteUnpublishedDatasetViaApi(testDatasetIds.numericId) + } catch (error) { + throw new Error( + `Tests afterAll(): Error while deleting test dataset ${testDatasetIds.numericId}` + ) + } } - try { - await deleteCollectionViaApi(testSubCollectionAlias) - } catch (error) { - throw new Error( - `Tests afterAll(): Error while deleting subcollection ${testSubCollectionAlias}` - ) + if (testSubCollectionCreated) { + try { + await deleteCollectionViaApi(testSubCollectionAlias) + } catch (error) { + throw new Error( + `Tests afterAll(): Error while deleting subcollection ${testSubCollectionAlias}` + ) + } } }) test('should return collection items given valid roleIds', async () => { @@ -2035,12 +2042,25 @@ describe('CollectionsRepository', () => { expect(actual.countPerObjectType.files).toBe(1) }) - test('should return error when role, type and publication status params are empty', async () => { - const expectedError = new ReadError('No results. Please select at least one Role.') - - await expect( - sut.getMyDataCollectionItems([], [], [], 0, 0, undefined, undefined) - ).rejects.toThrow(expectedError) + test('should return an empty item subset when role, type and publication status params are empty', async () => { + const actual = await sut.getMyDataCollectionItems([], [], [], 0, 0, undefined, undefined) + + expect(actual).toStrictEqual({ + items: [], + publicationStatusCounts: [ + { publicationStatus: PublicationStatus.Published, count: 0 }, + { publicationStatus: PublicationStatus.Unpublished, count: 0 }, + { publicationStatus: PublicationStatus.Draft, count: 0 }, + { publicationStatus: PublicationStatus.InReview, count: 0 }, + { publicationStatus: PublicationStatus.Deaccessioned, count: 0 } + ], + totalItemCount: 0, + countPerObjectType: { + collections: 0, + datasets: 0, + files: 0 + } + }) }) }) describe('linkCollection', () => {