|
| 1 | +import { ApiConfig, deleteCollectionFeaturedItem, WriteError } from '../../../src' |
| 2 | +import { TestConstants } from '../../testHelpers/TestConstants' |
| 3 | +import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' |
| 4 | +import { |
| 5 | + createCollectionViaApi, |
| 6 | + deleteCollectionViaApi |
| 7 | +} from '../../testHelpers/collections/collectionHelper' |
| 8 | +import { createCollectionCustomFeaturedItemViaApi } from '../../testHelpers/collections/collectionFeaturedItemsHelper' |
| 9 | + |
| 10 | +describe('execute', () => { |
| 11 | + const testCollectionAlias = 'deleteCollectionFeaturedItemTest' |
| 12 | + let featuredItemTestId: number |
| 13 | + |
| 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 | + beforeAll(async () => { |
| 23 | + try { |
| 24 | + await createCollectionViaApi(testCollectionAlias) |
| 25 | + const featuredItem = await createCollectionCustomFeaturedItemViaApi(testCollectionAlias, { |
| 26 | + content: '<p class="rte-paragraph">Test content</p>', |
| 27 | + displayOrder: 1, |
| 28 | + withFile: true, |
| 29 | + fileName: 'featured-item-test-image.png' |
| 30 | + }) |
| 31 | + featuredItemTestId = featuredItem.id |
| 32 | + } catch (error) { |
| 33 | + throw new Error( |
| 34 | + `Tests beforeAll(): Error while creating test collection: ${testCollectionAlias}` |
| 35 | + ) |
| 36 | + } |
| 37 | + }) |
| 38 | + |
| 39 | + afterAll(async () => { |
| 40 | + try { |
| 41 | + await deleteCollectionViaApi(testCollectionAlias) |
| 42 | + } catch (error) { |
| 43 | + throw new Error( |
| 44 | + `Tests afterAll(): Error while deleting test collection: ${testCollectionAlias}` |
| 45 | + ) |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + test('should succesfully delete the featured item', async () => { |
| 50 | + const actual = await deleteCollectionFeaturedItem.execute(featuredItemTestId) |
| 51 | + |
| 52 | + expect(actual).toBeUndefined() |
| 53 | + }) |
| 54 | + |
| 55 | + test('should throw an error when featured item does not exist', async () => { |
| 56 | + const invalidFeaturedItemId = 99 |
| 57 | + let writeError: WriteError | undefined |
| 58 | + |
| 59 | + try { |
| 60 | + await deleteCollectionFeaturedItem.execute(invalidFeaturedItemId) |
| 61 | + } catch (error) { |
| 62 | + writeError = error as WriteError |
| 63 | + } finally { |
| 64 | + expect(writeError).toBeInstanceOf(WriteError) |
| 65 | + expect((writeError as WriteError).message).toEqual( |
| 66 | + `There was an error when writing the resource. Reason was: [404] Could not find dataverse featured item with identifier ${invalidFeaturedItemId}` |
| 67 | + ) |
| 68 | + } |
| 69 | + }) |
| 70 | +}) |
0 commit comments