-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLinkCollection.test.ts
More file actions
79 lines (72 loc) · 2.77 KB
/
Copy pathLinkCollection.test.ts
File metadata and controls
79 lines (72 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import {
ApiConfig,
WriteError,
createCollection,
getCollection,
linkCollection,
deleteCollection,
getCollectionItems
} from '../../../src'
import { TestConstants } from '../../testHelpers/TestConstants'
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
import { createCollectionDTO } from '../../testHelpers/collections/collectionHelper'
describe('execute', () => {
const firstCollectionAlias = 'linkCollection-functional-test-first'
const secondCollectionAlias = 'linkCollection-functional-test-second'
beforeEach(async () => {
ApiConfig.init(
TestConstants.TEST_API_URL,
DataverseApiAuthMechanism.API_KEY,
process.env.TEST_API_KEY
)
const firstCollection = createCollectionDTO(firstCollectionAlias)
const secondCollection = createCollectionDTO(secondCollectionAlias)
await createCollection.execute(firstCollection)
await createCollection.execute(secondCollection)
})
afterEach(async () => {
await Promise.all([
getCollection
.execute(firstCollectionAlias)
.then((collection) =>
collection && collection.id ? deleteCollection.execute(collection.id) : null
),
getCollection
.execute(secondCollectionAlias)
.then((collection) =>
collection && collection.id ? deleteCollection.execute(collection.id) : null
)
])
})
test('should successfully link two collections', async () => {
const firstCollection = await getCollection.execute(firstCollectionAlias)
const secondCollection = await getCollection.execute(secondCollectionAlias)
expect.assertions(1)
try {
await linkCollection.execute(secondCollection.alias, firstCollection.alias)
} catch (error) {
throw new Error('Collections should be linked successfully')
} finally {
await new Promise((resolve) => setTimeout(resolve, 5000))
const collectionItemSubset = await getCollectionItems.execute(firstCollectionAlias)
expect(collectionItemSubset.items.length).toBe(1)
}
})
test('should throw an error when linking a non-existent collection', async () => {
const invalidCollectionId = 99999
const firstCollection = await getCollection.execute(firstCollectionAlias)
expect.assertions(2)
let writeError: WriteError | undefined = undefined
try {
await linkCollection.execute(invalidCollectionId, firstCollection.id)
throw new Error('Use case should throw an error')
} catch (error) {
writeError = error as WriteError
} finally {
expect(writeError).toBeInstanceOf(WriteError)
expect(writeError?.message).toEqual(
`There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${invalidCollectionId}'`
)
}
})
})