Skip to content

Commit

Permalink
Handle API returning 404 for Adjudications
Browse files Browse the repository at this point in the history
In the event the API returns a 404 we would throw an unhandled exception
which would stop the user completing the referral.

This change ensures that we handle this scenario and throw for anything
else.

The UI already handles the scenario of the adjudications array being
empty, we so we pass an empty array in this case.
  • Loading branch information
libuk committed Sep 18, 2023
1 parent 580a6d0 commit 426e98e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { adjudicationFactory, applicationFactory, personFactory } from '../../..
import { itShouldHaveNextValue, itShouldHavePreviousValue } from '../../../shared-examples'
import { PageBodyAdjudication, mapAdjudicationsForPageBody } from '../../../utils'
import Adjudications, { adjudicationResponse } from './adjudications'
import { SanitisedError } from '../../../../sanitisedError'

jest.mock('../../../../services/personService')
jest.mock('../../../utils')
Expand Down Expand Up @@ -66,6 +67,24 @@ describe('Adjudications', () => {
expect(getAdjudicationsMock).toHaveBeenCalledWith(callConfig, application.person.crn)
expect(mapAdjudicationsForPageBody).toHaveBeenCalledWith(apiAdjudications)
})

it('sets the number of adjudications to 0 if none are found', async () => {
const err = <SanitisedError>{ data: { status: 404 } }
const getAdjudicationsMock = jest.fn().mockImplementation(() => {
throw err
})

const personService = createMock<PersonService>({
getAdjudications: getAdjudicationsMock,
})

const page = await Adjudications.initialize({}, application, callConfig, { personService })

expect(page.body).toEqual({ adjudications })

expect(getAdjudicationsMock).toHaveBeenCalledWith(callConfig, application.person.crn)
expect(mapAdjudicationsForPageBody).toHaveBeenCalledWith([])
})
})

itShouldHavePreviousValue(new Adjudications({}, application), 'dashboard')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ export default class Adjudications implements TasklistPage {
callConfig: CallConfig,
dataServices: DataServices,
) {
const adjudications = await dataServices.personService.getAdjudications(callConfig, application.person.crn)
let adjudications: Adjudication[] = []

try {
adjudications = await dataServices.personService.getAdjudications(callConfig, application.person.crn)
} catch (e) {
if (e?.data?.status !== 404) {
throw e
}
}

const page = new Adjudications({ adjudications: mapAdjudicationsForPageBody(adjudications) }, application)

Expand Down

0 comments on commit 426e98e

Please sign in to comment.