Skip to content

Commit

Permalink
Renamed getErrorFlags function to getDateErrorFlags
Browse files Browse the repository at this point in the history
  • Loading branch information
jaucourt committed Dec 9, 2024
1 parent f891b50 commit 54bfb0c
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getData } from '../route'
import pageRoute from '../../../../routes/page-route.js'
import { nextPage } from '../../../../routes/next-page.js'
import { DATE_OF_BIRTH, LICENCE_FOR } from '../../../../uri.js'
import { dateOfBirthValidator, getErrorFlags } from '../../../../schema/validators/validators.js'
import { dateOfBirthValidator, getDateErrorFlags } from '../../../../schema/validators/validators.js'

jest.mock('../../../../routes/next-page.js')
jest.mock('../../../../routes/page-route.js')
Expand Down Expand Up @@ -83,15 +83,15 @@ describe('name > route', () => {

it('adds return value of getErrorFlags to the page data', async () => {
const errorFlags = { unique: Symbol('error-flags') }
getErrorFlags.mockReturnValueOnce(errorFlags)
getDateErrorFlags.mockReturnValueOnce(errorFlags)
const result = await getData(mockRequest())
expect(result).toEqual(expect.objectContaining(errorFlags))
})

it('passes error to getErrorFlags', async () => {
const error = Symbol('error')
await getData(mockRequest({ pageGet: async () => ({ error }) }))
expect(getErrorFlags).toHaveBeenCalledWith(error)
expect(getDateErrorFlags).toHaveBeenCalledWith(error)
})

it('passes correct page name when getting page cache', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { DATE_OF_BIRTH, LICENCE_FOR } from '../../../uri.js'
import pageRoute from '../../../routes/page-route.js'
import { nextPage } from '../../../routes/next-page.js'
import GetDataRedirect from '../../../handlers/get-data-redirect.js'
import { dateOfBirthValidator, getErrorFlags } from '../../../schema/validators/validators.js'
import { dateOfBirthValidator, getDateErrorFlags } from '../../../schema/validators/validators.js'

const redirectToStartOfJourney = status => {
if (!status[LICENCE_FOR.page]) {
Expand All @@ -14,7 +14,7 @@ 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)
const pageData = { isLicenceForYou, ...getErrorFlags(page?.error) }
const pageData = { isLicenceForYou, ...getDateErrorFlags(page?.error) }

redirectToStartOfJourney(status)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import pageRoute from '../../../../routes/page-route.js'
import { nextPage } from '../../../../routes/next-page.js'
import { getData } from '../route'
import { LICENCE_TO_START } from '../../../../uri.js'
import { startDateValidator, getErrorFlags } from '../../../../schema/validators/validators.js'
import { startDateValidator, getDateErrorFlags } from '../../../../schema/validators/validators.js'

jest.mock('../../../../routes/next-page.js')
jest.mock('../../../../routes/page-route.js')
Expand Down Expand Up @@ -34,7 +34,7 @@ describe('licence-to-start > route', () => {

describe('getData', () => {
beforeEach(() => {
getErrorFlags.mockClear()
getDateErrorFlags.mockClear()
})

it('should return isLicenceForYou as true, if isLicenceForYou is true on the transaction cache', async () => {
Expand Down Expand Up @@ -74,15 +74,15 @@ describe('licence-to-start > route', () => {

it('adds return value of getErrorFlags to the page data', async () => {
const errorFlags = { unique: Symbol('error-flags') }
getErrorFlags.mockReturnValueOnce(errorFlags)
getDateErrorFlags.mockReturnValueOnce(errorFlags)
const result = await getData(getMockRequest())
expect(result).toEqual(expect.objectContaining(errorFlags))
})

it('passes error to getErrorFlags', async () => {
const error = Symbol('error')
await getData(getMockRequest(undefined, async () => ({ error })))
expect(getErrorFlags).toHaveBeenCalledWith(error)
expect(getDateErrorFlags).toHaveBeenCalledWith(error)
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { START_AFTER_PAYMENT_MINUTES, ADVANCED_PURCHASE_MAX_DAYS, SERVICE_LOCAL_
import { LICENCE_TO_START } from '../../../uri.js'
import pageRoute from '../../../routes/page-route.js'
import { nextPage } from '../../../routes/next-page.js'
import { getErrorFlags, startDateValidator } from '../../../schema/validators/validators.js'
import { getDateErrorFlags, startDateValidator } from '../../../schema/validators/validators.js'

export const getData = async request => {
const fmt = 'DD MM YYYY'
Expand All @@ -16,7 +16,7 @@ export const getData = async request => {
maxStartDate: moment().tz(SERVICE_LOCAL_TIME).add(ADVANCED_PURCHASE_MAX_DAYS, 'days').format(fmt),
advancedPurchaseMaxDays: ADVANCED_PURCHASE_MAX_DAYS,
startAfterPaymentMinutes: START_AFTER_PAYMENT_MINUTES,
...getErrorFlags(page?.error)
...getDateErrorFlags(page?.error)
}

if (page?.error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import pageRoute from '../../../../routes/page-route.js'
import { addLanguageCodeToUri } from '../../../../processors/uri-helper.js'
import { getData, validator } from '../route.js'
import { IDENTIFY, NEW_TRANSACTION } from '../../../../uri.js'
import { dateOfBirthValidator, getErrorFlags } from '../../../../schema/validators/validators.js'
import { dateOfBirthValidator, getDateErrorFlags } from '../../../../schema/validators/validators.js'

jest.mock('../../../../routes/page-route.js', () => jest.fn())
jest.mock('../../../../uri.js', () => ({
Expand Down Expand Up @@ -50,15 +50,15 @@ describe('getData', () => {

it('adds return value of getErrorFlags to the page data', async () => {
const errorFlags = { unique: Symbol('error-flags') }
getErrorFlags.mockReturnValueOnce(errorFlags)
getDateErrorFlags.mockReturnValueOnce(errorFlags)
const result = await getData(getMockRequest())
expect(result).toEqual(expect.objectContaining(errorFlags))
})

it('passes error to getErrorFlags', async () => {
const error = Symbol('error')
await getData(getMockRequest(undefined, async () => ({ error })))
expect(getErrorFlags).toHaveBeenCalledWith(error)
expect(getDateErrorFlags).toHaveBeenCalledWith(error)
})

it('passes correct page name when getting page cache', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Joi from 'joi'
import { validation } from '@defra-fish/business-rules-lib'
import { addLanguageCodeToUri } from '../../../processors/uri-helper.js'
import GetDataRedirect from '../../../handlers/get-data-redirect.js'
import { dateOfBirthValidator, getErrorFlags } from '../../../schema/validators/validators.js'
import { dateOfBirthValidator, getDateErrorFlags } from '../../../schema/validators/validators.js'

export const getData = async request => {
// If we are supplied a permission number, validate it or throw 400
Expand All @@ -26,7 +26,7 @@ export const getData = async request => {
uri: {
new: addLanguageCodeToUri(request, NEW_TRANSACTION.uri)
},
...getErrorFlags(page?.error)
...getDateErrorFlags(page?.error)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from 'joi'
import { dateOfBirthValidator, startDateValidator, getErrorFlags } from '../validators.js'
import { dateOfBirthValidator, startDateValidator, getDateErrorFlags } from '../validators.js'
import moment from 'moment-timezone'
const dateSchema = require('../../date.schema.js')

Expand Down Expand Up @@ -183,7 +183,7 @@ describe('startDate validator', () => {

describe('getErrorFlags', () => {
it('sets all error flags to be false when there are no errors', () => {
const result = getErrorFlags()
const result = getDateErrorFlags()
expect(result).toEqual({ isDayError: false, isMonthError: false, isYearError: false })
})

Expand All @@ -201,7 +201,7 @@ describe('getErrorFlags', () => {
])('when error is %s, should set %o in flags', (errorKey, expected) => {
const error = { [errorKey]: 'anything.at.all' }

const result = getErrorFlags(error)
const result = getDateErrorFlags(error)

expect(result).toEqual(expect.objectContaining(expected))
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const startDateValidator = payload => {
}
}

export const getErrorFlags = error => {
export const getDateErrorFlags = error => {
const errorFlags = { isDayError: false, isMonthError: false, isYearError: false }
const commonErrors = ['full-date', 'invalid-date', 'date-range', 'non-numeric']

Expand Down

0 comments on commit 54bfb0c

Please sign in to comment.