|
| 1 | +import { ApiConfig, WriteError, createCollection, getCollection } from '../../../src' |
| 2 | +import { TestConstants } from '../../testHelpers/TestConstants' |
| 3 | +import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' |
| 4 | +import { createCollectionDTO } from '../../testHelpers/collections/collectionHelper' |
| 5 | + |
| 6 | +describe('execute', () => { |
| 7 | + beforeEach(async () => { |
| 8 | + ApiConfig.init( |
| 9 | + TestConstants.TEST_API_URL, |
| 10 | + DataverseApiAuthMechanism.API_KEY, |
| 11 | + process.env.TEST_API_KEY |
| 12 | + ) |
| 13 | + }) |
| 14 | + |
| 15 | + test('should successfully create a new collection', async () => { |
| 16 | + const testNewCollection = createCollectionDTO() |
| 17 | + expect.assertions(1) |
| 18 | + let createdCollectionId = 0 |
| 19 | + try { |
| 20 | + createdCollectionId = await createCollection.execute(testNewCollection) |
| 21 | + } catch (error) { |
| 22 | + throw new Error('Collection should be created') |
| 23 | + } finally { |
| 24 | + const createdCollection = await getCollection.execute(createdCollectionId) |
| 25 | + expect(createdCollection.alias).toBe(testNewCollection.alias) |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + test('should throw an error when the parent collection does not exist', async () => { |
| 30 | + const testNewCollection = createCollectionDTO() |
| 31 | + expect.assertions(2) |
| 32 | + let writeError: WriteError |
| 33 | + try { |
| 34 | + await createCollection.execute(testNewCollection, TestConstants.TEST_DUMMY_COLLECTION_ID) |
| 35 | + throw new Error('Use case should throw an error') |
| 36 | + } catch (error) { |
| 37 | + writeError = error |
| 38 | + } finally { |
| 39 | + expect(writeError).toBeInstanceOf(WriteError) |
| 40 | + expect(writeError.message).toEqual( |
| 41 | + `There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'` |
| 42 | + ) |
| 43 | + } |
| 44 | + }) |
| 45 | +}) |
0 commit comments