Skip to content

Commit

Permalink
add test for empty error message
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGoodall committed Oct 21, 2024
1 parent 9ffb0b9 commit 7a92376
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/middleware/issueDetails.middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { fetchIf, renderTemplate } from './middleware.builders.js'
import * as v from 'valibot'
import escape from 'escape-html'
import logger from '../utils/logger.js'

export const IssueDetailsQueryParams = v.strictObject({
lpa: v.string(),
Expand All @@ -44,10 +45,12 @@ const validateIssueDetailsQueryParams = validateQueryParams({
* @param {{value: string}?} issue
* @returns {string}
*/
export const issueErrorMessageHtml = (errorMessage, issue) =>
`<p class="govuk-error-message">${errorMessage}</p>${
escape(issue ? issue.value ?? '' : '')
}`
export const issueErrorMessageHtml = (errorMessage, issue) => {
if (!errorMessage) return ''
return `<p class="govuk-error-message">${errorMessage}</p>${
escape(issue ? issue.value ?? '' : '')
}`
}

/**
*
Expand Down Expand Up @@ -111,7 +114,10 @@ export function prepareIssueDetailsTemplateParams (req, res, next) {
let classes = ''
const fieldValue = entity[datasetField] || { value: '' }
if (fieldValue.issue) {
valueHtml += issueErrorMessageHtml(entity[datasetField].issue.message, null)
if (!fieldValue.issue.message) {
logger.warn('no issue message found for issue in entity', { entity })
}
valueHtml += issueErrorMessageHtml(fieldValue.issue.message, null)
classes += 'dl-summary-card-list__row--error'
}
valueHtml += escape(entity[datasetField]?.value || '')
Expand Down
6 changes: 6 additions & 0 deletions test/unit/middleware/issueDetails.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ describe('issueDetails.middleware.js', () => {
const result = issueErrorMessageHtml(errorMessage, issue)
expect(result).toBe(`<p class="govuk-error-message">${errorMessage}</p>`)
})

it('should return an empty string if errorMessage is null', () => {
const issue = { value: '02-02-2022' }
const result = issueErrorMessageHtml(null, issue)
expect(result).toBe('')
})
})

describe('getIssueField', () => {
Expand Down

0 comments on commit 7a92376

Please sign in to comment.