Skip to content

Commit

Permalink
Merge pull request #714 from ministryofjustice/fix-old-oasys-date-bug
Browse files Browse the repository at this point in the history
Add date validation on old oasys form pages
  • Loading branch information
patrickjfl authored May 21, 2024
2 parents f5376ef + 8ee7186 commit 3d73518
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { itShouldHaveNextValue, itShouldHavePreviousValue } from '../../../shared-examples'
import { personFactory, applicationFactory } from '../../../../testutils/factories/index'
import OldOasys from './oldOasys'
import { dateAndTimeInputsAreValidDates, dateIsComplete } from '../../../../utils/dateUtils'

jest.mock('../../../../utils/dateUtils', () => {
const actual = jest.requireActual('../../../../utils/dateUtils')
return {
...actual,
dateAndTimeInputsAreValidDates: jest.fn(),
dateIsComplete: jest.fn(),
}
})

describe('OldOasys', () => {
const application = applicationFactory.build({ person: personFactory.build({ name: 'Roger Smith' }) })
Expand All @@ -23,10 +33,26 @@ describe('OldOasys', () => {
hasOldOasys: 'Confirm whether they have an older OASys with risk of serious harm (RoSH) information',
})
})
it('returns an error when hasOldOasys is yes but oasysCompletedDate is blank', () => {
const page = new OldOasys({ hasOldOasys: 'yes' }, application)
expect(page.errors()).toEqual({
oasysCompletedDate: 'Enter the date the OASys was completed',
describe('when hasOldOasys is yes', () => {
it('returns an error when oasysCompletedDate is blank', () => {
const page = new OldOasys({ hasOldOasys: 'yes' }, application)
expect(page.errors()).toEqual({
oasysCompletedDate: 'Enter the date the OASys was completed',
})
})

describe('when the date is not valid', () => {
beforeEach(() => {
jest.resetAllMocks()
;(dateAndTimeInputsAreValidDates as jest.Mock).mockImplementation(() => false)
;(dateIsComplete as jest.Mock).mockImplementation(() => true)
})
it('returns an error', () => {
const page = new OldOasys({ hasOldOasys: 'yes' }, application)
expect(page.errors()).toEqual({
oasysCompletedDate: 'OASys completed date must be a real date',
})
})
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import TaskListPage from '../../../taskListPage'
import { nameOrPlaceholderCopy } from '../../../../utils/utils'
import { dateBodyProperties } from '../../../utils'
import { getQuestions } from '../../../utils/questions'
import { DateFormats, dateIsComplete } from '../../../../utils/dateUtils'
import { DateFormats, dateAndTimeInputsAreValidDates, dateIsComplete } from '../../../../utils/dateUtils'

type OldOasysBody = {
hasOldOasys: YesOrNo
Expand Down Expand Up @@ -59,8 +59,15 @@ export default class OldOasys implements TaskListPage {
if (!this.body.hasOldOasys) {
errors.hasOldOasys = 'Confirm whether they have an older OASys with risk of serious harm (RoSH) information'
}
if (this.body.hasOldOasys === 'yes' && !dateIsComplete(this.body, 'oasysCompletedDate')) {
errors.oasysCompletedDate = 'Enter the date the OASys was completed'
if (this.body.hasOldOasys === 'yes') {
if (!dateIsComplete(this.body, 'oasysCompletedDate')) {
errors.oasysCompletedDate = 'Enter the date the OASys was completed'
return errors
}

if (!dateAndTimeInputsAreValidDates(this.body, 'oasysCompletedDate')) {
errors.oasysCompletedDate = 'OASys completed date must be a real date'
}
}

return errors
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { itShouldHaveNextValue, itShouldHavePreviousValue } from '../../../shared-examples'
import { personFactory, applicationFactory } from '../../../../testutils/factories/index'
import OldOasys from './oldOasys'
import { dateAndTimeInputsAreValidDates, dateIsComplete } from '../../../../utils/dateUtils'

jest.mock('../../../../utils/dateUtils', () => {
const actual = jest.requireActual('../../../../utils/dateUtils')
return {
...actual,
dateAndTimeInputsAreValidDates: jest.fn(),
dateIsComplete: jest.fn(),
}
})

describe('OldOasys', () => {
const application = applicationFactory.build({ person: personFactory.build({ name: 'Roger Smith' }) })
Expand All @@ -23,10 +33,26 @@ describe('OldOasys', () => {
hasOldOasys: 'Confirm whether they have an older OASys with risk to self information',
})
})
it('returns an error when hasOldOasys is yes but oasysCompletedDate is blank', () => {
const page = new OldOasys({ hasOldOasys: 'yes' }, application)
expect(page.errors()).toEqual({
oasysCompletedDate: 'Enter the date the OASys was completed',
describe('when hasOldOasys is yes', () => {
it('returns an error when oasysCompletedDate is blank', () => {
const page = new OldOasys({ hasOldOasys: 'yes' }, application)
expect(page.errors()).toEqual({
oasysCompletedDate: 'Enter the date the OASys was completed',
})
})

describe('when the date is not valid', () => {
beforeEach(() => {
jest.resetAllMocks()
;(dateAndTimeInputsAreValidDates as jest.Mock).mockImplementation(() => false)
;(dateIsComplete as jest.Mock).mockImplementation(() => true)
})
it('returns an error ', () => {
const page = new OldOasys({ hasOldOasys: 'yes' }, application)
expect(page.errors()).toEqual({
oasysCompletedDate: 'OASys completed date must be a real date',
})
})
})
})
})
Expand Down
12 changes: 9 additions & 3 deletions server/form-pages/apply/risks-and-needs/risk-to-self/oldOasys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import TaskListPage from '../../../taskListPage'
import { nameOrPlaceholderCopy } from '../../../../utils/utils'
import { dateBodyProperties } from '../../../utils'
import { getQuestions } from '../../../utils/questions'
import { DateFormats, dateIsComplete } from '../../../../utils/dateUtils'
import { DateFormats, dateAndTimeInputsAreValidDates, dateIsComplete } from '../../../../utils/dateUtils'

type OldOasysBody = {
hasOldOasys: YesOrNo
Expand Down Expand Up @@ -59,8 +59,14 @@ export default class OldOasys implements TaskListPage {
if (!this.body.hasOldOasys) {
errors.hasOldOasys = 'Confirm whether they have an older OASys with risk to self information'
}
if (this.body.hasOldOasys === 'yes' && !dateIsComplete(this.body, 'oasysCompletedDate')) {
errors.oasysCompletedDate = 'Enter the date the OASys was completed'
if (this.body.hasOldOasys === 'yes') {
if (!dateIsComplete(this.body, 'oasysCompletedDate')) {
errors.oasysCompletedDate = 'Enter the date the OASys was completed'
return errors
}
if (!dateAndTimeInputsAreValidDates(this.body, 'oasysCompletedDate')) {
errors.oasysCompletedDate = 'OASys completed date must be a real date'
}
}

return errors
Expand Down

0 comments on commit 3d73518

Please sign in to comment.