diff --git a/server/routes/__tests__/water-pollution/effect-on-wildlife.spec.js b/server/routes/__tests__/water-pollution/effect-on-wildlife.spec.js index 9539138..b3630c0 100644 --- a/server/routes/__tests__/water-pollution/effect-on-wildlife.spec.js +++ b/server/routes/__tests__/water-pollution/effect-on-wildlife.spec.js @@ -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('') + }) + 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(' { + 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('') + expect(response.payload).toContain('Further details') + }) }) 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) @@ -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) @@ -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) diff --git a/server/routes/water-pollution/effect-on-wildlife.js b/server/routes/water-pollution/effect-on-wildlife.js index f958af4..b60d8cd 100644 --- a/server/routes/water-pollution/effect-on-wildlife.js +++ b/server/routes/water-pollution/effect-on-wildlife.js @@ -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 } diff --git a/server/views/water-pollution/effect-on-wildlife.html b/server/views/water-pollution/effect-on-wildlife.html index 82aeaf7..f223a25 100644 --- a/server/views/water-pollution/effect-on-wildlife.html +++ b/server/views/water-pollution/effect-on-wildlife.html @@ -12,6 +12,7 @@ name: "yesDetails", errorMessage: findErrorMessageById(errorSummary, "yesDetails"), classes: "govuk-!-width-two-third", + value: getAnswer(answers, question.answers.yesDetails.answerId), label: { text: question.answers.yesDetails.text } @@ -19,9 +20,9 @@ {% endset -%} {{ govukRadios ({ - id: "effectOnWildlife", - name: "effectOnWildlife", - errorMessage: findErrorMessageById(errorSummary, "effectOnWildlife"), + id: "answerId", + name: "answerId", + errorMessage: findErrorMessageById(errorSummary, "answerId"), fieldset: { legend: { text: question.text, @@ -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) } ] }) }}