|
| 1 | +import { ApiConfig, ReadError, getCollectionFeaturedItems } from '../../../src' |
| 2 | +import { TestConstants } from '../../testHelpers/TestConstants' |
| 3 | +import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' |
| 4 | +import { |
| 5 | + createCollectionFeaturedItemViaApi, |
| 6 | + deleteCollectionFeaturedItemViaApi |
| 7 | +} from '../../testHelpers/collections/collectionFeaturedItemsHelper' |
| 8 | +import { |
| 9 | + createCollectionViaApi, |
| 10 | + deleteCollectionViaApi |
| 11 | +} from '../../testHelpers/collections/collectionHelper' |
| 12 | +import { ROOT_COLLECTION_ID } from '../../../src/collections/domain/models/Collection' |
| 13 | + |
| 14 | +describe('execute', () => { |
| 15 | + const testCollectionAlias = 'collectionsRepositoryTestCollection' |
| 16 | + let testFeaturedItemId: number |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + ApiConfig.init( |
| 20 | + TestConstants.TEST_API_URL, |
| 21 | + DataverseApiAuthMechanism.API_KEY, |
| 22 | + process.env.TEST_API_KEY |
| 23 | + ) |
| 24 | + }) |
| 25 | + |
| 26 | + beforeAll(async () => { |
| 27 | + try { |
| 28 | + await createCollectionViaApi(testCollectionAlias) |
| 29 | + |
| 30 | + const featuredItemCreated = await createCollectionFeaturedItemViaApi(testCollectionAlias, { |
| 31 | + content: '<p class="rte-paragraph">Test content</p>', |
| 32 | + displayOrder: 1, |
| 33 | + withFile: true, |
| 34 | + fileName: 'featured-item-test-image.png' |
| 35 | + }) |
| 36 | + |
| 37 | + testFeaturedItemId = featuredItemCreated.id |
| 38 | + } catch (error) { |
| 39 | + throw new Error(`Error while creating collection featured item in ${testCollectionAlias}`) |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + afterAll(async () => { |
| 44 | + try { |
| 45 | + await deleteCollectionFeaturedItemViaApi(testFeaturedItemId) |
| 46 | + await deleteCollectionViaApi(testCollectionAlias) |
| 47 | + } catch (error) { |
| 48 | + throw new Error( |
| 49 | + `Tests afterAll(): Error while deleting featured item with id ${testFeaturedItemId}` |
| 50 | + ) |
| 51 | + } |
| 52 | + }) |
| 53 | + |
| 54 | + test('should return featured items array given a valid collection alias that has featured items', async () => { |
| 55 | + const featuredItemsResponse = await getCollectionFeaturedItems.execute(testCollectionAlias) |
| 56 | + |
| 57 | + expect(featuredItemsResponse.length).toBe(1) |
| 58 | + expect(featuredItemsResponse[0].id).toBe(testFeaturedItemId) |
| 59 | + expect(featuredItemsResponse[0].displayOrder).toBe(1) |
| 60 | + expect(featuredItemsResponse[0].content).toBe('<p class="rte-paragraph">Test content</p>') |
| 61 | + expect(featuredItemsResponse[0].imageFileUrl).toBe( |
| 62 | + 'http://localhost:8080/api/access/dataverseFeatureItemImage/1' |
| 63 | + ) |
| 64 | + expect(featuredItemsResponse[0].imageFileName).toBe('featured-item-test-image.png') |
| 65 | + }) |
| 66 | + |
| 67 | + it('should return imageFileUrl and imageFileName as undefined when featured item does not have an image', async () => { |
| 68 | + const featuredItemCreated = await createCollectionFeaturedItemViaApi(testCollectionAlias, { |
| 69 | + content: '<p class="rte-paragraph">Test content</p>', |
| 70 | + displayOrder: 2 |
| 71 | + }) |
| 72 | + |
| 73 | + const featuredItemsResponse = await getCollectionFeaturedItems.execute(testCollectionAlias) |
| 74 | + |
| 75 | + expect(featuredItemsResponse.length).toBe(2) |
| 76 | + expect(featuredItemsResponse[1].id).toBe(featuredItemCreated.id) |
| 77 | + expect(featuredItemsResponse[1].displayOrder).toBe(2) |
| 78 | + expect(featuredItemsResponse[1].content).toBe('<p class="rte-paragraph">Test content</p>') |
| 79 | + expect(featuredItemsResponse[1].imageFileUrl).toBeUndefined() |
| 80 | + expect(featuredItemsResponse[1].imageFileName).toBeUndefined() |
| 81 | + |
| 82 | + await deleteCollectionFeaturedItemViaApi(featuredItemCreated.id) |
| 83 | + }) |
| 84 | + |
| 85 | + test('should return empty featured items array given a valid collection alias that has no featured items', async () => { |
| 86 | + const featuredItemsResponse = await getCollectionFeaturedItems.execute(ROOT_COLLECTION_ID) |
| 87 | + |
| 88 | + expect(featuredItemsResponse).toStrictEqual([]) |
| 89 | + }) |
| 90 | + |
| 91 | + test('should throw an error when collection does not exist', async () => { |
| 92 | + const invalidCollectionAlias = 'invalid-collection-alias' |
| 93 | + let readError: ReadError | undefined |
| 94 | + |
| 95 | + try { |
| 96 | + await getCollectionFeaturedItems.execute(invalidCollectionAlias) |
| 97 | + } catch (error) { |
| 98 | + readError = error |
| 99 | + } finally { |
| 100 | + expect(readError).toBeInstanceOf(ReadError) |
| 101 | + expect((readError as ReadError).message).toEqual( |
| 102 | + `There was an error when reading the resource. Reason was: [404] Can't find dataverse with identifier='${invalidCollectionAlias}'` |
| 103 | + ) |
| 104 | + } |
| 105 | + }) |
| 106 | +}) |
0 commit comments