Skip to content

Commit 85d25e8

Browse files
committed
test: fix ts errors in functional tests
1 parent 73a2ea1 commit 85d25e8

9 files changed

+69
-78
lines changed

test/functional/collections/CreateCollection.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ describe('execute', () => {
2929
test('should throw an error when the parent collection does not exist', async () => {
3030
const testNewCollection = createCollectionDTO()
3131
expect.assertions(2)
32-
let writeError: WriteError
32+
let writeError: WriteError | undefined = undefined
3333
try {
3434
await createCollection.execute(testNewCollection, TestConstants.TEST_DUMMY_COLLECTION_ID)
3535
throw new Error('Use case should throw an error')
3636
} catch (error) {
3737
writeError = error
3838
} finally {
3939
expect(writeError).toBeInstanceOf(WriteError)
40-
expect(writeError.message).toEqual(
40+
expect(writeError?.message).toEqual(
4141
`There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'`
4242
)
4343
}

test/functional/collections/GetCollectionFacets.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ describe('execute', () => {
2727

2828
test('should throw an error when collection does not exist', async () => {
2929
expect.assertions(2)
30-
let readError: ReadError
30+
let readError: ReadError | undefined = undefined
3131
try {
3232
await getCollectionFacets.execute(TestConstants.TEST_DUMMY_COLLECTION_ID)
3333
throw new Error('Use case should throw an error')
3434
} catch (error) {
3535
readError = error
3636
} finally {
3737
expect(readError).toBeInstanceOf(ReadError)
38-
expect(readError.message).toEqual(
38+
expect(readError?.message).toEqual(
3939
`There was an error when reading the resource. Reason was: [404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'`
4040
)
4141
}

test/functional/collections/GetCollectionItems.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
} from '../../testHelpers/collections/collectionHelper'
1616
import { uploadFileViaApi } from '../../testHelpers/files/filesHelper'
1717
import { deleteUnpublishedDatasetViaApi } from '../../testHelpers/datasets/datasetHelper'
18-
import { CollectionItemSubset } from '../../../src/collections/domain/models/CollectionItemSubset'
1918

2019
describe('execute', () => {
2120
const testCollectionAlias = 'collectionsRepositoryFunctionalTestCollection'
@@ -59,33 +58,32 @@ describe('execute', () => {
5958
// Give enough time to Solr for indexing
6059
await new Promise((resolve) => setTimeout(resolve, 5000))
6160

62-
let actual: CollectionItemSubset
6361
try {
64-
actual = await getCollectionItems.execute(testCollectionAlias)
65-
} catch (error) {
66-
throw new Error('Item subset should be retrieved')
67-
} finally {
62+
const actual = await getCollectionItems.execute(testCollectionAlias)
63+
6864
const actualFilePreview = actual.items[0] as FilePreview
6965
const actualDatasetPreview = actual.items[1] as DatasetPreview
7066

7167
expect(actualFilePreview.name).toBe('test-file-1.txt')
7268
expect(actualDatasetPreview.title).toBe('Dataset created using the createDataset use case')
7369

7470
expect(actual.totalItemCount).toBe(2)
71+
} catch (error) {
72+
throw new Error('Item subset should be retrieved')
7573
}
7674
})
7775

7876
test('should throw an error when collection does not exist', async () => {
7977
expect.assertions(2)
80-
let readError: ReadError
78+
let readError: ReadError | undefined = undefined
8179
try {
8280
await getCollectionItems.execute(TestConstants.TEST_DUMMY_COLLECTION_ALIAS)
8381
throw new Error('Use case should throw an error')
8482
} catch (error) {
8583
readError = error
8684
} finally {
8785
expect(readError).toBeInstanceOf(ReadError)
88-
expect(readError.message).toEqual(
86+
expect(readError?.message).toEqual(
8987
`There was an error when reading the resource. Reason was: [400] Could not find dataverse with alias ${TestConstants.TEST_DUMMY_COLLECTION_ALIAS}`
9088
)
9189
}

test/functional/collections/GetCollectionUserPermissions.test.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
ApiConfig,
3-
CollectionUserPermissions,
4-
ReadError,
5-
getCollectionUserPermissions
6-
} from '../../../src'
1+
import { ApiConfig, ReadError, getCollectionUserPermissions } from '../../../src'
72
import { TestConstants } from '../../testHelpers/TestConstants'
83
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
94

@@ -19,49 +14,47 @@ describe('execute', () => {
1914
})
2015

2116
test('should return user permissions for the default collection', async () => {
22-
let actual: CollectionUserPermissions
2317
try {
24-
actual = await getCollectionUserPermissions.execute()
18+
const permissions = await getCollectionUserPermissions.execute()
19+
20+
expect(permissions.canAddDataset).toBe(true)
21+
expect(permissions.canAddCollection).toBe(true)
22+
expect(permissions.canDeleteCollection).toBe(true)
23+
expect(permissions.canEditCollection).toBe(true)
24+
expect(permissions.canManageCollectionPermissions).toBe(true)
25+
expect(permissions.canPublishCollection).toBe(true)
26+
expect(permissions.canViewUnpublishedCollection).toBe(true)
2527
} catch (error) {
2628
throw new Error('Permissions should be retrieved')
27-
} finally {
28-
expect(actual.canAddDataset).toBe(true)
29-
expect(actual.canAddCollection).toBe(true)
30-
expect(actual.canDeleteCollection).toBe(true)
31-
expect(actual.canEditCollection).toBe(true)
32-
expect(actual.canManageCollectionPermissions).toBe(true)
33-
expect(actual.canPublishCollection).toBe(true)
34-
expect(actual.canViewUnpublishedCollection).toBe(true)
3529
}
3630
})
3731
test('should return user permissions when a valid collection alias is provided', async () => {
38-
let actual: CollectionUserPermissions
3932
try {
40-
actual = await getCollectionUserPermissions.execute(ROOT_COLLECTION_ALIAS)
33+
const permissions = await getCollectionUserPermissions.execute(ROOT_COLLECTION_ALIAS)
34+
35+
expect(permissions.canAddDataset).toBe(true)
36+
expect(permissions.canAddCollection).toBe(true)
37+
expect(permissions.canDeleteCollection).toBe(true)
38+
expect(permissions.canEditCollection).toBe(true)
39+
expect(permissions.canManageCollectionPermissions).toBe(true)
40+
expect(permissions.canPublishCollection).toBe(true)
41+
expect(permissions.canViewUnpublishedCollection).toBe(true)
4142
} catch (error) {
4243
throw new Error('Permissions should be retrieved')
43-
} finally {
44-
expect(actual.canAddDataset).toBe(true)
45-
expect(actual.canAddCollection).toBe(true)
46-
expect(actual.canDeleteCollection).toBe(true)
47-
expect(actual.canEditCollection).toBe(true)
48-
expect(actual.canManageCollectionPermissions).toBe(true)
49-
expect(actual.canPublishCollection).toBe(true)
50-
expect(actual.canViewUnpublishedCollection).toBe(true)
5144
}
5245
})
5346

5447
test('should throw an error when collection does not exist', async () => {
5548
expect.assertions(2)
56-
let readError: ReadError
49+
let readError: ReadError | undefined = undefined
5750
try {
5851
await getCollectionUserPermissions.execute(TestConstants.TEST_DUMMY_COLLECTION_ID)
5952
throw new Error('Use case should throw an error')
6053
} catch (error) {
6154
readError = error
6255
} finally {
6356
expect(readError).toBeInstanceOf(ReadError)
64-
expect(readError.message).toEqual(
57+
expect(readError?.message).toEqual(
6558
`There was an error when reading the resource. Reason was: [404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'`
6659
)
6760
}

test/functional/collections/UpdateCollection.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ describe('execute', () => {
3838
test('should throw an error when the parent collection does not exist', async () => {
3939
const testNewCollection = createCollectionDTO()
4040
expect.assertions(2)
41-
let writeError: WriteError
41+
let writeError: WriteError | undefined = undefined
4242
try {
4343
await updateCollection.execute(TestConstants.TEST_DUMMY_COLLECTION_ID, testNewCollection)
4444
throw new Error('Use case should throw an error')
4545
} catch (error) {
4646
writeError = error
4747
} finally {
4848
expect(writeError).toBeInstanceOf(WriteError)
49-
expect(writeError.message).toEqual(
49+
expect(writeError?.message).toEqual(
5050
`There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'`
5151
)
5252
}

test/functional/datasets/CreateDataset.test.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createDataset, CreatedDatasetIdentifiers } from '../../../src/datasets'
1+
import { createDataset, DatasetDTO } from '../../../src/datasets'
22
import { ApiConfig } from '../../../src'
33
import { TestConstants } from '../../testHelpers/TestConstants'
44
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
@@ -48,16 +48,16 @@ describe('execute', () => {
4848
]
4949
}
5050
expect.assertions(3)
51-
let createdDatasetIdentifiers: CreatedDatasetIdentifiers
51+
5252
try {
53-
createdDatasetIdentifiers = await createDataset.execute(testNewDataset)
54-
} catch (error) {
55-
throw new Error('Dataset should be created')
56-
} finally {
53+
const createdDatasetIdentifiers = await createDataset.execute(testNewDataset)
54+
5755
expect(createdDatasetIdentifiers).not.toBeNull()
5856
expect(createdDatasetIdentifiers.numericId).not.toBeNull()
5957
expect(createdDatasetIdentifiers.persistentId).not.toBeNull()
6058
await deleteUnpublishedDatasetViaApi(createdDatasetIdentifiers.numericId)
59+
} catch (error) {
60+
throw new Error('Dataset should be created')
6161
}
6262
})
6363

@@ -94,25 +94,25 @@ describe('execute', () => {
9494
]
9595
}
9696
expect.assertions(5)
97-
let fieldValidationError: FieldValidationError
97+
let fieldValidationError: FieldValidationError | undefined = undefined
9898
try {
9999
await createDataset.execute(testNewDataset)
100100
throw new Error('Use case should throw an error')
101101
} catch (error) {
102102
fieldValidationError = error
103103
} finally {
104104
expect(fieldValidationError).toBeInstanceOf(FieldValidationError)
105-
expect(fieldValidationError.citationBlockName).toEqual('citation')
106-
expect(fieldValidationError.metadataFieldName).toEqual('title')
107-
expect(fieldValidationError.parentMetadataFieldName).toEqual(undefined)
108-
expect(fieldValidationError.message).toEqual(
105+
expect(fieldValidationError?.citationBlockName).toEqual('citation')
106+
expect(fieldValidationError?.metadataFieldName).toEqual('title')
107+
expect(fieldValidationError?.parentMetadataFieldName).toEqual(undefined)
108+
expect(fieldValidationError?.message).toEqual(
109109
'There was an error when validating the field title from metadata block citation. Reason was: The field should not be empty.'
110110
)
111111
}
112112
})
113113

114114
test('should throw an error when a second level required field is missing', async () => {
115-
const testNewDataset = {
115+
const testNewDataset: DatasetDTO = {
116116
metadataBlockValues: [
117117
{
118118
name: 'citation',
@@ -144,19 +144,19 @@ describe('execute', () => {
144144
]
145145
}
146146
expect.assertions(6)
147-
let fieldValidationError: FieldValidationError
147+
let fieldValidationError: FieldValidationError | undefined = undefined
148148
try {
149149
await createDataset.execute(testNewDataset)
150150
throw new Error('Use case should throw an error')
151151
} catch (error) {
152152
fieldValidationError = error
153153
} finally {
154154
expect(fieldValidationError).toBeInstanceOf(FieldValidationError)
155-
expect(fieldValidationError.citationBlockName).toEqual('citation')
156-
expect(fieldValidationError.metadataFieldName).toEqual('authorName')
157-
expect(fieldValidationError.parentMetadataFieldName).toEqual('author')
158-
expect(fieldValidationError.fieldPosition).toEqual(0)
159-
expect(fieldValidationError.message).toEqual(
155+
expect(fieldValidationError?.citationBlockName).toEqual('citation')
156+
expect(fieldValidationError?.metadataFieldName).toEqual('authorName')
157+
expect(fieldValidationError?.parentMetadataFieldName).toEqual('author')
158+
expect(fieldValidationError?.fieldPosition).toEqual(0)
159+
expect(fieldValidationError?.message).toEqual(
160160
'There was an error when validating the field authorName from metadata block citation with parent field author in position 0. Reason was: The field should not be empty.'
161161
)
162162
}
@@ -196,19 +196,19 @@ describe('execute', () => {
196196
]
197197
}
198198
expect.assertions(6)
199-
let fieldValidationError: FieldValidationError
199+
let fieldValidationError: FieldValidationError | undefined = undefined
200200
try {
201201
await createDataset.execute(testNewDataset)
202202
throw new Error('Use case should throw an error')
203203
} catch (error) {
204204
fieldValidationError = error
205205
} finally {
206206
expect(fieldValidationError).toBeInstanceOf(FieldValidationError)
207-
expect(fieldValidationError.citationBlockName).toEqual('citation')
208-
expect(fieldValidationError.metadataFieldName).toEqual('subject')
209-
expect(fieldValidationError.parentMetadataFieldName).toEqual(undefined)
210-
expect(fieldValidationError.fieldPosition).toEqual(1)
211-
expect(fieldValidationError.message).toEqual(
207+
expect(fieldValidationError?.citationBlockName).toEqual('citation')
208+
expect(fieldValidationError?.metadataFieldName).toEqual('subject')
209+
expect(fieldValidationError?.parentMetadataFieldName).toEqual(undefined)
210+
expect(fieldValidationError?.fieldPosition).toEqual(1)
211+
expect(fieldValidationError?.message).toEqual(
212212
'There was an error when validating the field subject from metadata block citation in position 1. Reason was: The field does not have a valid controlled vocabulary value.'
213213
)
214214
}

test/functional/metadataBlocks/GetAllFacetableMetadataFields.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ describe('execute', () => {
1212
})
1313

1414
test('should return all facetable metadata fields', async () => {
15-
let metadataFieldInfos: MetadataFieldInfo[] = null
15+
let metadataFieldInfos: MetadataFieldInfo[] | null = null
1616
try {
1717
metadataFieldInfos = await getAllFacetableMetadataFields.execute()
1818
} catch (error) {
1919
throw new Error('Should not raise an error')
2020
} finally {
21-
expect(metadataFieldInfos.length).toBe(59)
22-
expect(metadataFieldInfos[0].name).toBe('authorName')
23-
expect(metadataFieldInfos[0].displayName).toBe('Author Name')
21+
expect(metadataFieldInfos?.length).toBe(59)
22+
expect(metadataFieldInfos?.[0].name).toBe('authorName')
23+
expect(metadataFieldInfos?.[0].displayName).toBe('Author Name')
2424
}
2525
})
2626
})

test/functional/metadataBlocks/GetAllMetadataBlocks.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ describe('execute', () => {
1212
})
1313

1414
test('should successfully return metadatablocks', async () => {
15-
let metadataBlocks: MetadataBlock[] = null
15+
let metadataBlocks: MetadataBlock[] | null = null
1616
try {
1717
metadataBlocks = await getAllMetadataBlocks.execute()
1818
} catch (error) {
1919
throw new Error('Should not raise an error')
2020
} finally {
2121
expect(metadataBlocks).not.toBeNull()
22-
expect(metadataBlocks.length).toBe(6)
23-
expect(metadataBlocks[0].metadataFields.title.name).toBe('title')
22+
expect(metadataBlocks?.length).toBe(6)
23+
expect(metadataBlocks?.[0].metadataFields.title.name).toBe('title')
2424
}
2525
})
2626
})

test/functional/metadataBlocks/GetCollectionMetadataBlocks.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,29 @@ describe('execute', () => {
1212
})
1313

1414
test('should successfully return collection metadatablocks when collection exists', async () => {
15-
let collectionMetadataBlocks: MetadataBlock[] = null
15+
let collectionMetadataBlocks: MetadataBlock[] | null = null
1616
try {
1717
collectionMetadataBlocks = await getCollectionMetadataBlocks.execute('root')
1818
} catch (error) {
1919
throw new Error('Should not raise an error')
2020
} finally {
2121
expect(collectionMetadataBlocks).not.toBeNull()
22-
expect(collectionMetadataBlocks.length).toBe(1)
23-
expect(collectionMetadataBlocks[0].metadataFields.title.name).toBe('title')
22+
expect(collectionMetadataBlocks?.length).toBe(1)
23+
expect(collectionMetadataBlocks?.[0].metadataFields.title.name).toBe('title')
2424
}
2525
})
2626

2727
test('should throw an error when a collection is not found', async () => {
2828
expect.assertions(2)
29-
let readError: ReadError
29+
let readError: ReadError | undefined = undefined
3030
try {
3131
await getCollectionMetadataBlocks.execute('notFoundCollectionAlias')
3232
throw new Error('Use case should throw an error')
3333
} catch (error) {
3434
readError = error
3535
} finally {
3636
expect(readError).toBeInstanceOf(ReadError)
37-
expect(readError.message).toEqual(
37+
expect(readError?.message).toEqual(
3838
`There was an error when reading the resource. Reason was: [404] Can't find dataverse with identifier='notFoundCollectionAlias'`
3939
)
4040
}

0 commit comments

Comments
 (0)