Skip to content

Commit

Permalink
Added: getCollectionMetadataBlocks repository unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Apr 3, 2024
1 parent 23f0180 commit 5955617
Showing 1 changed file with 82 additions and 27 deletions.
109 changes: 82 additions & 27 deletions test/unit/metadataBlocks/MetadataBlocksRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { TestConstants } from '../../testHelpers/TestConstants'
import axios from 'axios'

describe('getMetadataBlockByName', () => {
describe('MetadataBlocksRepository', () => {
const sut: MetadataBlocksRepository = new MetadataBlocksRepository()
const testMetadataBlockName = 'test'

Expand All @@ -23,40 +23,95 @@ describe('getMetadataBlockByName', () => {
)
})

test('should return metadata block on successful response', async () => {
const testSuccessfulResponse = {
data: {
status: 'OK',
data: createMetadataBlockPayload()
describe('getCollectionMetadataBlocks', () => {
const expectedRequestConfig = {
headers: TestConstants.TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY.headers,
params: {
onlyDisplayedOnCreate: true,
returnDatasetFieldTypes: true
}
}
jest.spyOn(axios, 'get').mockResolvedValue(testSuccessfulResponse)

const actual = await sut.getMetadataBlockByName(testMetadataBlockName)
test('should return collection metadata blocks on successful response', async () => {
const testSuccessfulResponse = {
data: {
status: 'OK',
data: [createMetadataBlockPayload()]
}
}
jest.spyOn(axios, 'get').mockResolvedValue(testSuccessfulResponse)

expect(axios.get).toHaveBeenCalledWith(
`${TestConstants.TEST_API_URL}/metadatablocks/${testMetadataBlockName}`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG
)
expect(actual).toMatchObject(createMetadataBlockModel())
const actual = await sut.getCollectionMetadataBlocks(
TestConstants.TEST_DUMMY_COLLECTION_ID,
true
)

expect(axios.get).toHaveBeenCalledWith(
`${TestConstants.TEST_API_URL}/dataverses/${TestConstants.TEST_DUMMY_COLLECTION_ID}/metadatablocks`,
expectedRequestConfig
)
expect(actual[0]).toMatchObject(createMetadataBlockModel())
expect(actual.length).toEqual(1)
})

test('should return error result on error response', async () => {
const testErrorResponse = {
response: {
status: 'ERROR',
message: 'test'
}
}
jest.spyOn(axios, 'get').mockRejectedValue(testErrorResponse)

let error: ReadError = undefined
await sut
.getCollectionMetadataBlocks(TestConstants.TEST_DUMMY_COLLECTION_ID, true)
.catch((e) => (error = e))

expect(axios.get).toHaveBeenCalledWith(
`${TestConstants.TEST_API_URL}/dataverses/${TestConstants.TEST_DUMMY_COLLECTION_ID}/metadatablocks`,
expectedRequestConfig
)
expect(error).toBeInstanceOf(Error)
})
})

test('should return error result on error response', async () => {
const testErrorResponse = {
response: {
status: 'ERROR',
message: 'test'
describe('getMetadataBlockByName', () => {
test('should return metadata block on successful response', async () => {
const testSuccessfulResponse = {
data: {
status: 'OK',
data: createMetadataBlockPayload()
}
}
}
jest.spyOn(axios, 'get').mockRejectedValue(testErrorResponse)
jest.spyOn(axios, 'get').mockResolvedValue(testSuccessfulResponse)

let error: ReadError = undefined
await sut.getMetadataBlockByName(testMetadataBlockName).catch((e) => (error = e))
const actual = await sut.getMetadataBlockByName(testMetadataBlockName)

expect(axios.get).toHaveBeenCalledWith(
`${TestConstants.TEST_API_URL}/metadatablocks/${testMetadataBlockName}`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG
)
expect(error).toBeInstanceOf(Error)
expect(axios.get).toHaveBeenCalledWith(
`${TestConstants.TEST_API_URL}/metadatablocks/${testMetadataBlockName}`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG
)
expect(actual).toMatchObject(createMetadataBlockModel())
})

test('should return error result on error response', async () => {
const testErrorResponse = {
response: {
status: 'ERROR',
message: 'test'
}
}
jest.spyOn(axios, 'get').mockRejectedValue(testErrorResponse)

let error: ReadError = undefined
await sut.getMetadataBlockByName(testMetadataBlockName).catch((e) => (error = e))

expect(axios.get).toHaveBeenCalledWith(
`${TestConstants.TEST_API_URL}/metadatablocks/${testMetadataBlockName}`,
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG
)
expect(error).toBeInstanceOf(Error)
})
})
})

0 comments on commit 5955617

Please sign in to comment.