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