Skip to content

Commit

Permalink
SIR-894 : Story – WP – Check your answers – Dead animals (#222)
Browse files Browse the repository at this point in the history
* Technical implementation for SIR-894

* Removed console logs
  • Loading branch information
sujithvg authored Nov 18, 2024
1 parent 531d682 commit 3266382
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 40 deletions.
55 changes: 44 additions & 11 deletions server/routes/__tests__/water-pollution/effect-on-wildlife.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,57 @@ const baseAnswer = {
questionAsked: question.text,
questionResponse: true
}

const payload = {
effectOnWildlife: 'yes',
yesDetails: 'Further details'
}

describe(url, () => {
describe('GET', () => {
it(`Should return success response and correct view for ${url}`, async () => {
await submitGetRequest({ url }, header)
})
it(`Should return success response and correct view when yes is selected for ${url}`, async () => {
const sessionData = {
'water-pollution/effect-on-wildlife': [{
questionId: baseAnswer.questionId,
answerId: question.answers.yes.answerId
}]
}
const response = await submitGetRequest({ url }, 'Have you seen any dead or distressed fish or animals nearby?', constants.statusCodes.OK, sessionData)
expect(response.payload).toContain('<input class="govuk-radios__input" id="answerId" name="answerId" type="radio" value="201" checked data-aria-controls="conditional-answerId">')
})
it(`Should return success response and correct view when no is selected for ${url}`, async () => {
const sessionData = {
'water-pollution/effect-on-wildlife': [{
questionId: baseAnswer.questionId,
answerId: question.answers.no.answerId
}]
}
const response = await submitGetRequest({ url }, 'Have you seen any dead or distressed fish or animals nearby?', constants.statusCodes.OK, sessionData)
expect(response.payload).toContain('<input class="govuk-radios__input" id="answerId-2" name="answerId" type="radio" value="202"')
})
it(`Should return success response and correct view when yes is selected with yesDetails for ${url}`, async () => {
const sessionData = {
'water-pollution/effect-on-wildlife': [{
questionId: baseAnswer.questionId,
answerId: question.answers.yes.answerId
}, {
questionId: baseAnswer.questionId,
answerId: question.answers.yesDetails.answerId,
otherDetails: 'Further details'
}]
}
const response = await submitGetRequest({ url }, 'Have you seen any dead or distressed fish or animals nearby?', constants.statusCodes.OK, sessionData)
expect(response.payload).toContain('<input class="govuk-radios__input" id="answerId" name="answerId" type="radio" value="201" checked data-aria-controls="conditional-answerId">')
expect(response.payload).toContain('Further details</textarea>')
})
})

describe('POST', () => {
it('Happy accepts Yes and yes Details and forwards to WATER_POLLUTION_OTHER_INFORMATION', async () => {
const answerId = question.answers.yes.answerId
const yesDetails = 'Further details'
const options = {
url,
payload: {
effectOnWildlife: 'yes',
yesDetails: 'Further details'
answerId,
yesDetails
}
}
const response = await submitPostRequest(options)
Expand All @@ -40,14 +71,15 @@ describe(url, () => {
}, {
...baseAnswer,
answerId: question.answers.yesDetails.answerId,
otherDetails: payload.yesDetails
otherDetails: yesDetails
}])
})
it('Happy accepts Yes and empty yes details and forwards to WATER_POLLUTION_OTHER_INFORMATION', async () => {
const answerId = question.answers.yes.answerId
const options = {
url,
payload: {
effectOnWildlife: 'yes'
answerId
}
}
const response = await submitPostRequest(options)
Expand All @@ -58,10 +90,11 @@ describe(url, () => {
}])
})
it('Happy accepts No and forwards to WATER_POLLUTION_OTHER_INFORMATION', async () => {
const answerId = question.answers.no.answerId
const options = {
url,
payload: {
effectOnWildlife: 'no'
answerId
}
}
const response = await submitPostRequest(options)
Expand Down
50 changes: 31 additions & 19 deletions server/routes/water-pollution/effect-on-wildlife.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,57 +11,69 @@ const baseAnswer = {
}

const handlers = {
get: async (_request, h) => {
get: async (request, h) => {
return h.view(constants.views.WATER_POLLUTION_EFFECT_ON_WILDLIFE, {
...getContext()
...getContext(request)
})
},
post: async (request, h) => {
// get payload
let { answerId, yesDetails } = request.payload

// validate payload
const errorSummary = validatePayload(request.payload)
const errorSummary = validatePayload(answerId)
if (errorSummary.errorList.length > 0) {
return h.view(constants.views.WATER_POLLUTION_EFFECT_ON_WILDLIFE, {
...getContext(),
errorSummary,
...request.payload
...getContext(request),
errorSummary
})
}

request.yar.set(constants.redisKeys.WATER_POLLUTION_EFFECT_ON_WILDLIFE, buildAnswers(request.payload))
// convert answerId to number
answerId = Number(answerId)

// set answer in session
request.yar.set(constants.redisKeys.WATER_POLLUTION_EFFECT_ON_WILDLIFE, buildAnswers(answerId, yesDetails))

return h.redirect(constants.routes.WATER_POLLUTION_OTHER_INFORMATION)
// handle redirection
return h.redirect(request.yar.get(constants.redisKeys.REFERER) || constants.routes.WATER_POLLUTION_OTHER_INFORMATION)
}
}

const getContext = () => {
const getContext = (request) => {
const answers = request.yar.get(question.key)
return {
question
question,
answers
}
}

const validatePayload = payload => {
const validatePayload = answerId => {
const errorSummary = getErrorSummary()
if (!payload.effectOnWildlife) {
if (!answerId) {
errorSummary.errorList.push({
text: 'Select yes if you\'ve seen dead or distressed fish or other animals nearby',
href: '#effectOnWildlife'
href: '#answerId'
})
}
return errorSummary
}

const buildAnswers = payload => {
const answers = [{
const buildAnswers = (answerId, yesDetails) => {
const answers = []
answers.push({
...baseAnswer,
answerId: payload.effectOnWildlife === 'yes' ? question.answers.yes.answerId : question.answers.no.answerId
}]
if (payload.effectOnWildlife === 'yes' && payload.yesDetails) {
answerId
})

if (answerId === question.answers.yes.answerId && yesDetails) {
answers.push({
...baseAnswer,
answerId: question.answers.yesDetails.answerId,
otherDetails: payload.yesDetails
otherDetails: yesDetails
})
}

return answers
}

Expand Down
21 changes: 11 additions & 10 deletions server/views/water-pollution/effect-on-wildlife.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,17 @@
name: "yesDetails",
errorMessage: findErrorMessageById(errorSummary, "yesDetails"),
classes: "govuk-!-width-two-third",
value: getAnswer(answers, question.answers.yesDetails.answerId),
label: {
text: question.answers.yesDetails.text
}
}) }}
{% endset -%}

{{ govukRadios ({
id: "effectOnWildlife",
name: "effectOnWildlife",
errorMessage: findErrorMessageById(errorSummary, "effectOnWildlife"),
id: "answerId",
name: "answerId",
errorMessage: findErrorMessageById(errorSummary, "answerId"),
fieldset: {
legend: {
text: question.text,
Expand All @@ -31,17 +32,17 @@
},
items: [
{
value: "yes",
text: "Yes",
value: question.answers.yes.answerId,
text: question.answers.yes.text,
checked: getAnswer(answers, question.answers.yes.answerId),
conditional: {
html: sourceDetailsHtml
},
checked: effectOnWildlife === 'yes'
}
},
{
value: "no",
text: "No",
checked: effectOnWildlife === 'no'
value: question.answers.no.answerId,
text: question.answers.no.text,
checked: getAnswer(answers, question.answers.no.answerId)
}
]
}) }}
Expand Down

0 comments on commit 3266382

Please sign in to comment.