Skip to content

Commit

Permalink
Add error handling to update form
Browse files Browse the repository at this point in the history
We need to handle errors provided by the API so that we show them in the
UI
  • Loading branch information
rich committed Jul 11, 2024

Verified

This commit was signed with the committer’s verified signature.
masci Massimiliano Pippi
1 parent aec075d commit e74583f
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -9,8 +9,13 @@ import UpdateOutOfServiceBedsController from './updateOutOfServiceBedsController
import { DateFormats } from '../../utils/dateUtils'
import { UpdateCas1OutOfServiceBed } from '../../@types/shared'
import paths from '../../paths/manage'
import { catchValidationErrorOrPropogate, generateConflictErrorAndRedirect } from '../../utils/validation'
import {
catchValidationErrorOrPropogate,
fetchErrorsAndUserInput,
generateConflictErrorAndRedirect,
} from '../../utils/validation'
import { SanitisedError } from '../../sanitisedError'
import { ErrorsAndUserInput } from '../../@types/ui'

jest.mock('../../utils/validation')

@@ -41,6 +46,12 @@ describe('updateOutOfServiceBedController', () => {
when(outOfServiceBedService.getOutOfServiceBed)
.calledWith(request.user.token, premisesId, outOfServiceBed.id)
.mockResolvedValue(outOfServiceBed)

when(fetchErrorsAndUserInput).calledWith(request).mockReturnValue({
errors: {},
errorSummary: [],
userInput: {},
})
})

describe('new', () => {
@@ -95,6 +106,24 @@ describe('updateOutOfServiceBedController', () => {
}),
)
})

it('renders the form with errors and user input if theres an error', async () => {
const errorsAndUserInput = createMock<ErrorsAndUserInput>()
when(fetchErrorsAndUserInput).calledWith(request).mockReturnValue(errorsAndUserInput)

const requestHandler = updateOutOfServiceBedController.new()

await requestHandler(request, response, next)

expect(response.render).toHaveBeenCalledWith(
expect.anything(),
expect.objectContaining({
errors: errorsAndUserInput.errors,
errorSummary: errorsAndUserInput.errorSummary,
...errorsAndUserInput.userInput,
}),
)
})
})

describe('create', () => {
11 changes: 10 additions & 1 deletion server/controllers/v2Manage/updateOutOfServiceBedsController.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,11 @@ import type { Request, RequestHandler, Response } from 'express'
import { OutOfServiceBedService, PremisesService } from '../../services'
import { DateFormats } from '../../utils/dateUtils'

import { catchValidationErrorOrPropogate, generateConflictErrorAndRedirect } from '../../utils/validation'
import {
catchValidationErrorOrPropogate,
fetchErrorsAndUserInput,
generateConflictErrorAndRedirect,
} from '../../utils/validation'
import paths from '../../paths/manage'
import { SanitisedError } from '../../sanitisedError'

@@ -15,6 +19,7 @@ export default class OutOfServiceBedsController {
new(): RequestHandler {
return async (req: Request, res: Response) => {
const { premisesId, bedId, id } = req.params
const { errors, errorSummary, userInput, errorTitle } = fetchErrorsAndUserInput(req)

const outOfServiceBedReasons = await this.outOfServiceBedService.getOutOfServiceBedReasons(req.user.token)
const outOfServiceBed = await this.outOfServiceBedService.getOutOfServiceBed(req.user.token, premisesId, id)
@@ -28,6 +33,10 @@ export default class OutOfServiceBedsController {
outOfServiceBed,
...DateFormats.isoDateToDateInputs(outOfServiceBed.startDate, 'startDate'),
...DateFormats.isoDateToDateInputs(outOfServiceBed.endDate, 'endDate'),
errors,
errorSummary,
errorTitle,
...userInput,
})
}
}

0 comments on commit e74583f

Please sign in to comment.