Skip to content

Commit

Permalink
Make the error shown by the date input the same as in the error summary
Browse files Browse the repository at this point in the history
  • Loading branch information
jaucourt committed Nov 20, 2024
1 parent b6fc2fe commit d182510
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,75 +1,117 @@
import { getData } from '../route'
import pageRoute from '../../../../routes/page-route.js'
import { nextPage } from '../../../../routes/next-page.js'
import { LICENCE_FOR } from '../../../../uri.js'
import { DATE_OF_BIRTH, LICENCE_FOR } from '../../../../uri.js'
import { dateOfBirthValidator } from '../../../../schema/validators/validators.js'

jest.mock('../../../../routes/next-page.js')
jest.mock('../../../../routes/page-route.js')
jest.mock('../../../../schema/validators/validators.js')
jest.mock('../../../../uri.js', () => ({
...jest.requireActual('../../../../uri.js'),
DATE_OF_BIRTH: {
page: Symbol('date-of-birth-page'),
uri: Symbol('/date-of-birth')
},
LICENCE_TO_START: {
page: Symbol('licence-to-start-page'),
uri: Symbol('/licence-to-start')
}
}))

describe('name > route', () => {
const mockRequest = (statusGet = () => {}, transactionGet = () => {}) => ({
const mockRequest = ({
pageGet = async () => {},
statusGet = async () => ({ [LICENCE_FOR.page]: true }),
transactionGet = async () => ({ isLicenceForYou: null })
} = {}) => ({
cache: () => ({
helpers: {
transaction: {
getCurrentPermission: transactionGet
},
status: {
getCurrentPermission: statusGet
},
page: {
getCurrentPermission: pageGet
}
}
})
})

describe('getData', () => {
it('should return isLicenceForYou as true, if isLicenceForYou is true on the transaction cache', async () => {
const transaction = () => ({
const transactionGet = async () => ({
isLicenceForYou: true
})
const status = () => ({
const statusGet = async () => ({
[LICENCE_FOR.page]: true
})
const result = await getData(mockRequest(status, transaction))

const result = await getData(mockRequest({ statusGet, transactionGet }))
expect(result.isLicenceForYou).toBeTruthy()
})

it('should return isLicenceForYou as false, if isLicenceForYou is false on the transaction cache', async () => {
const transaction = () => ({
const transactionGet = async () => ({
isLicenceForYou: false
})
const status = () => ({
const statusGet = async () => ({
[LICENCE_FOR.page]: true
})
const result = await getData(mockRequest(status, transaction))
const result = await getData(mockRequest({ statusGet, transactionGet }))
expect(result.isLicenceForYou).toBeFalsy()
})

it.each([
['full-date', 'object.missing'],
['day', 'any.required']
])('should add error details ({%s: %s}) to the page data', async (errorKey, errorValue) => {
const pageGet = async () => ({
error: { [errorKey]: errorValue }
})

const result = await getData(mockRequest({ pageGet }))
expect(result.error).toEqual({ errorKey, errorValue })
})

it('omits error if there is no error', async () => {
const result = await getData(mockRequest())
expect(result.error).toBeUndefined()
})

it('passes correct page name when getting page cache', async () => {
const pageGet = jest.fn(() => ({}))
await getData(mockRequest({ pageGet }))
expect(pageGet).toHaveBeenCalledWith(DATE_OF_BIRTH.page)
})
})

describe('redirectToStartOfJourney', () => {
it('should throw a redirect if not been to LICENCE_FOR page', async () => {
const transaction = () => ({
const transactionGet = async () => ({
isLicenceForYou: true
})
const status = () => ({
const statusGet = async () => ({
[LICENCE_FOR.page]: false
})
const func = () => getData(mockRequest(status, transaction))
const func = () => getData(mockRequest({ statusGet, transactionGet }))
await expect(func).rejects.toThrowRedirectTo(LICENCE_FOR.uri)
})

it('should not throw a redirect if not been to LICENCE_FOR page', async () => {
const transaction = () => ({
const transactionGet = async () => ({
isLicenceForYou: true
})
const status = () => ({
const statusGet = async () => ({
[LICENCE_FOR.page]: true
})

let error

try {
await getData(mockRequest(status, transaction))
await getData(mockRequest({ statusGet, transactionGet }))
} catch (e) {
error = e
}
Expand All @@ -80,7 +122,7 @@ describe('name > route', () => {

describe('default', () => {
it('should call the pageRoute with date-of-birth, /buy/date-of-birth, dateOfBirthValidator and nextPage', async () => {
expect(pageRoute).toBeCalledWith('date-of-birth', '/buy/date-of-birth', dateOfBirthValidator, nextPage, getData)
expect(pageRoute).toBeCalledWith(DATE_OF_BIRTH.page, DATE_OF_BIRTH.uri, dateOfBirthValidator, nextPage, getData)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@
id: "date-of-birth",
namePrefix: "date-of-birth",
items: dateInputItems,
errorMessage: { text: mssgs.dob_error } if error
errorMessage: { text: errorMap[data.error.errorKey][data.error.errorValue].text } if data.error
}) }}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@ const redirectToStartOfJourney = status => {
export const getData = async request => {
const { isLicenceForYou } = await request.cache().helpers.transaction.getCurrentPermission()
const status = await request.cache().helpers.status.getCurrentPermission()
const page = await request.cache().helpers.page.getCurrentPermission(DATE_OF_BIRTH.page)

redirectToStartOfJourney(status)

if (page?.error) {
const [errorKey] = Object.keys(page.error)
const errorValue = page.error[errorKey]

return { isLicenceForYou, error: { errorKey, errorValue } }
}
return { isLicenceForYou }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ jest.mock('../../../../uri.js', () => ({
}))

describe('licence-to-start > route', () => {
const getMockRequest = (isLicenceForYou = true) => ({
const getMockRequest = (isLicenceForYou = true, pageGet = () => {}) => ({
cache: () => ({
helpers: {
transaction: {
getCurrentPermission: () => ({
isLicenceForYou
})
},
page: {
getCurrentPermission: pageGet
}
}
})
Expand All @@ -40,6 +43,29 @@ describe('licence-to-start > route', () => {
const result = await getData(request)
expect(result.isLicenceForYou).toBeFalsy()
})

it.each([
['full-date', 'object.missing'],
['day', 'any.required']
])('should add error details ({%s: %s}) to the page data', async (errorKey, errorValue) => {
const pageGet = async () => ({
error: { [errorKey]: errorValue }
})

const result = await getData(getMockRequest(undefined, pageGet))
expect(result.error).toEqual({ errorKey, errorValue })
})

it('omits error if there is no error', async () => {
const result = await getData(getMockRequest())
expect(result.error).toBeUndefined()
})

it('passes correct page name when getting page cache', async () => {
const pageGet = jest.fn(() => {})
await getData(getMockRequest(undefined, pageGet))
expect(pageGet).toHaveBeenCalledWith(LICENCE_TO_START.page)
})
})

describe('default', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
id: "licence-start-date",
namePrefix: "licence-start-date",
items: dateInputItems,
errorMessage: { text: mssgs.licence_start_error_within + data.advancedPurchaseMaxDays + mssgs.licence_start_days } if error['licence-start-date-old'],
errorMessage: { text: errorMap[data.error.errorKey][data.error.errorValue].text } if data.error,
hint: {
text: mssgs.licence_start_hint + data.maxStartDate
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@ import { startDateValidator } from '../../../schema/validators/validators.js'
export const getData = async request => {
const fmt = 'DD MM YYYY'
const { isLicenceForYou } = await request.cache().helpers.transaction.getCurrentPermission()

return {
const page = await request.cache().helpers.page.getCurrentPermission(LICENCE_TO_START.page)
const pageData = {
isLicenceForYou,
exampleStartDate: moment().tz(SERVICE_LOCAL_TIME).add(1, 'days').format(fmt),
minStartDate: moment().tz(SERVICE_LOCAL_TIME).format(fmt),
maxStartDate: moment().tz(SERVICE_LOCAL_TIME).add(ADVANCED_PURCHASE_MAX_DAYS, 'days').format(fmt),
advancedPurchaseMaxDays: ADVANCED_PURCHASE_MAX_DAYS,
startAfterPaymentMinutes: START_AFTER_PAYMENT_MINUTES
}

if (page?.error) {
const [errorKey] = Object.keys(page.error)
const errorValue = page.error[errorKey]
pageData.error = { errorKey, errorValue }
}

return pageData
}

export default pageRoute(LICENCE_TO_START.page, LICENCE_TO_START.uri, startDateValidator, nextPage, getData)

0 comments on commit d182510

Please sign in to comment.