From d18251076abd98f7bd47c166f96fbac6a0df3177 Mon Sep 17 00:00:00 2001 From: Phil Benson Date: Wed, 20 Nov 2024 16:49:38 +0000 Subject: [PATCH] Make the error shown by the date input the same as in the error summary --- .../date-of-birth/__tests__/route.spec.js | 72 +++++++++++++++---- .../date-of-birth/date-of-birth.njk | 2 +- .../pages/concessions/date-of-birth/route.js | 7 ++ .../licence-to-start/__tests__/route.spec.js | 28 +++++++- .../licence-to-start/licence-to-start.njk | 2 +- .../licence-details/licence-to-start/route.js | 12 +++- 6 files changed, 103 insertions(+), 20 deletions(-) diff --git a/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/__tests__/route.spec.js b/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/__tests__/route.spec.js index d1b7e5434..f20626ec9 100644 --- a/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/__tests__/route.spec.js +++ b/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/__tests__/route.spec.js @@ -1,15 +1,30 @@ 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: { @@ -17,6 +32,9 @@ describe('name > route', () => { }, status: { getCurrentPermission: statusGet + }, + page: { + getCurrentPermission: pageGet } } }) @@ -24,52 +42,76 @@ describe('name > route', () => { 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 } @@ -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) }) }) }) diff --git a/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/date-of-birth.njk b/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/date-of-birth.njk index 95a20bd54..29ea1d0a8 100644 --- a/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/date-of-birth.njk +++ b/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/date-of-birth.njk @@ -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 %} diff --git a/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/route.js b/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/route.js index 149f6614d..10515ffde 100644 --- a/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/route.js +++ b/packages/gafl-webapp-service/src/pages/concessions/date-of-birth/route.js @@ -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 } } diff --git a/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/__tests__/route.spec.js b/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/__tests__/route.spec.js index db6422e03..da16e0829 100644 --- a/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/__tests__/route.spec.js +++ b/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/__tests__/route.spec.js @@ -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 } } }) @@ -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', () => { diff --git a/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/licence-to-start.njk b/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/licence-to-start.njk index 13b0c4f9a..91c64f50c 100644 --- a/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/licence-to-start.njk +++ b/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/licence-to-start.njk @@ -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 } diff --git a/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/route.js b/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/route.js index 3774ab12f..8155c0cd2 100644 --- a/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/route.js +++ b/packages/gafl-webapp-service/src/pages/licence-details/licence-to-start/route.js @@ -8,8 +8,8 @@ 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), @@ -17,6 +17,14 @@ export const getData = async request => { 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)