Skip to content

Commit

Permalink
Add controls & validation for Rtn req setup page (#758)
Browse files Browse the repository at this point in the history
https://eaflood.atlassian.net/browse/WATER-4267

This change adds controls and validation for the setup page.

Depending on which control is chosen, the user will be redirected to a different follow up page.

If nothing is selected the user is presented with a validation warning.
  • Loading branch information
Demwunz authored Feb 27, 2024
1 parent c699e06 commit a69525d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/controllers/return-requirements.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ async function submitSetup (request, h) {
return h.view('return-requirements/setup.njk', pageData)
}

return h.redirect(`/system/return-requirements/${sessionId}/purpose`)
return h.redirect(`/system/return-requirements/${sessionId}/${pageData.redirect}`)
}

async function submitSiteDescription (request, h) {
Expand Down
17 changes: 16 additions & 1 deletion app/services/return-requirements/submit-setup.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const SetupValidator = require('../../validators/return-requirements/setup.valid
*
* The validation result is then combined with the output of the presenter to generate the page data needed by the view.
* If there was a validation error the controller will re-render the page so needs this information. If all is well the
* controller will redirect to the next page in the journey.
* controller will redirect to the next page in the journey depending on which radio item was chosen.
*
* @param {string} sessionId - The id of the current session
* @param {Object} payload - The submitted form data
Expand All @@ -34,10 +34,25 @@ async function go (sessionId, payload) {
activeNavBar: 'search',
error: validationResult,
pageTitle: 'How do you want to set up the return requirement?',
redirect: _redirect(payload.setup),
...formattedData
}
}

function _redirect (setup) {
let endpoint

if (setup === 'use_abstraction_data') {
endpoint = 'check-your-answers'
}

if (setup === 'set_up_manually') {
endpoint = 'purpose'
}

return endpoint
}

function _validate (payload) {
const validation = SetupValidator.go(payload)

Expand Down
2 changes: 1 addition & 1 deletion app/validators/return-requirements/setup.validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Joi = require('joi')

const VALID_VALUES = [
'use_abstraction_data',
'copy_an_existing_return_requirement'
'set_up_manually'
]

/**
Expand Down
7 changes: 5 additions & 2 deletions app/views/return-requirements/setup.njk
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@
checked: false
},
{
value: "copy_an_existing_return_requirement",
text: "Copy an existing return requirement",
divider: "or"
},
{
value: "set_up_manually",
text: "Set up manually",
checked: false
}
]
Expand Down
34 changes: 32 additions & 2 deletions test/services/return-requirements/submit-setup.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ describe('Submit Setup service', () => {
expect(result).to.equal({
activeNavBar: 'search',
error: null,
licenceRef: '01/ABC',
pageTitle: 'How do you want to set up the return requirement?',
licenceRef: '01/ABC'
redirect: 'check-your-answers'
}, { skip: ['id'] })
})
})
Expand All @@ -79,8 +80,9 @@ describe('Submit Setup service', () => {

expect(result).to.equal({
activeNavBar: 'search',
licenceRef: '01/ABC',
pageTitle: 'How do you want to set up the return requirement?',
licenceRef: '01/ABC'
redirect: undefined
}, { skip: ['id', 'error'] })
})

Expand All @@ -94,4 +96,32 @@ describe('Submit Setup service', () => {
})
})
})

describe('with different setups', () => {
describe('and setup is use_abstraction_data', () => {
beforeEach(() => {
payload = {
setup: 'use_abstraction_data'
}
})

it('redirects to the check your answers page', async () => {
const result = await SubmitSetupService.go(session.id, payload)
expect(result.redirect).to.equal('check-your-answers')
})
})

describe('and setup is set_up_manually', () => {
beforeEach(() => {
payload = {
setup: 'set_up_manually'
}
})

it('redirects to the purpose page', async () => {
const result = await SubmitSetupService.go(session.id, payload)
expect(result.redirect).to.equal('purpose')
})
})
})
})

0 comments on commit a69525d

Please sign in to comment.