diff --git a/server/routes/__tests__/smell/clothing-and-hair.spec.js b/server/routes/__tests__/smell/clothing-and-hair.spec.js new file mode 100644 index 0000000..a9204c7 --- /dev/null +++ b/server/routes/__tests__/smell/clothing-and-hair.spec.js @@ -0,0 +1,97 @@ +import { submitGetRequest, submitPostRequest } from '../../../__test-helpers__/server.js' +import { questionSets } from '../../../utils/question-sets.js' +import constants from '../../../utils/constants.js' + +const url = constants.routes.SMELL_CLOTHING_AND_HAIR +const question = questionSets.SMELL.questions.SMELL_CLOTHING_AND_HAIR +const header = question.text +const baseAnswer = { + questionId: question.questionId, + questionAsked: question.text, + questionResponse: true +} + +describe(url, () => { + describe('GET', () => { + it(`Should return success response and correct view for ${url} if current smell`, async () => { + const sessionData = { + 'smell/indoors': [{ + questionId: questionSets.SMELL.questions.SMELL_INDOORS.questionId, + answerId: questionSets.SMELL.questions.SMELL_INDOORS.answers.yes.answerId + }] + } + await submitGetRequest({ url }, header, constants.statusCodes.OK, sessionData) + }) + it(`Should return success response and correct view for ${url} if past smell`, async () => { + const sessionData = { + 'smell/indoors': [{ + questionId: questionSets.SMELL.questions.SMELL_INDOORS.questionId, + answerId: questionSets.SMELL.questions.SMELL_INDOORS.answers.no.answerId + }] + } + const wasHeader = header.replace('Does', 'Did') + await submitGetRequest({ url }, wasHeader, constants.statusCodes.OK, sessionData) + }) + }) + + describe('POST', () => { + it('Happy: Yes and continues to smell/effect-on-daily-life', async () => { + const options = { + url, + payload: { + answerId: question.answers.yes.answerId + } + } + const response = await submitPostRequest(options) + expect(response.headers.location).toEqual(constants.routes.SMELL_EFFECT_ON_DAILY_LIFE) + expect(response.request.yar.get(constants.redisKeys.SMELL_CLOTHING_AND_HAIR)).toEqual([{ + ...baseAnswer, + answerId: question.answers.yes.answerId + }]) + }) + it('Happy: No and continues to smell/effect-on-daily-life', async () => { + const options = { + url, + payload: { + answerId: question.answers.no.answerId + } + } + const response = await submitPostRequest(options) + expect(response.headers.location).toEqual(constants.routes.SMELL_EFFECT_ON_DAILY_LIFE) + expect(response.request.yar.get(constants.redisKeys.SMELL_CLOTHING_AND_HAIR)).toEqual([{ + ...baseAnswer, + answerId: question.answers.no.answerId + }]) + }) + it('Sad: Rejects empty payload with current smell is noticeable indoors', async () => { + const sessionData = { + 'smell/indoors': [{ + questionId: questionSets.SMELL.questions.SMELL_INDOORS.questionId, + answerId: questionSets.SMELL.questions.SMELL_INDOORS.answers.yes.answerId + }] + } + const options = { + url, + payload: {} + } + const response = await submitPostRequest(options, constants.statusCodes.OK, sessionData) + expect(response.payload).toContain('There is a problem') + expect(response.payload).toContain('Select yes if the smell sticks to your clothing or hair') + }) + it('Sad: Rejects empty payload with current smell is not noticeable indoors', async () => { + const sessionData = { + 'smell/indors': [{ + questionId: questionSets.SMELL.questions.SMELL_INDOORS.questionId, + answerId: questionSets.SMELL.questions.SMELL_INDOORS.answers.no.answerId + }] + } + const options = { + url, + payload: {} + } + const response = await submitPostRequest(options, constants.statusCodes.OK, sessionData) + expect(response.payload).toContain('There is a problem') + expect(response.payload).toContain('Select yes if the smell stuck to your clothing or hair') + }) + }) +}) diff --git a/server/routes/smell/clothing-and-hair.js b/server/routes/smell/clothing-and-hair.js index e2a49ff..9958cce 100644 --- a/server/routes/smell/clothing-and-hair.js +++ b/server/routes/smell/clothing-and-hair.js @@ -1,15 +1,82 @@ import constants from '../../utils/constants.js' +import { questionSets } from '../../utils/question-sets.js' +import { getErrorSummary } from '../../utils/helpers.js' + +const question = questionSets.SMELL.questions.SMELL_CLOTHING_AND_HAIR + +const baseAnswer = { + questionId: question.questionId, + questionAsked: question.text, + questionResponse: true +} const handlers = { - get: (_request, _h) => { - return 'hello world' + get: async (request, h) => { + return h.view(constants.views.SMELL_CLOTHING_AND_HAIR, { + ...getContext(request) + }) + }, + post: async (request, h) => { + // get payload + let { answerId } = request.payload + const { current } = getContext(request) + + // validate payload for errors + const errorSummary = validatePayload(answerId, current) + if (errorSummary.errorList.length > 0) { + return h.view(constants.views.SMELL_CLOTHING_AND_HAIR, { + question, + current, + errorSummary + }) + } + + // convert answerId to number + answerId = Number(answerId) + + // set answer in session + request.yar.set(constants.redisKeys.SMELL_CLOTHING_AND_HAIR, buildAnswers(answerId)) + + return h.redirect(constants.routes.SMELL_EFFECT_ON_DAILY_LIFE) } } +const getContext = request => { + const currentAnswer = request.yar.get(constants.redisKeys.SMELL_INDOORS) + const current = currentAnswer && currentAnswer[0].answerId === questionSets.SMELL.questions.SMELL_INDOORS.answers.yes.answerId + return { + question, + current + } +} + +const validatePayload = (answerId, current) => { + const errorSummary = getErrorSummary() + if (!answerId) { + errorSummary.errorList.push({ + text: `Select yes if the smell ${current ? 'sticks' : 'stuck'} to your clothing or hair`, + href: '#answerId' + }) + } + return errorSummary +} + +const buildAnswers = answerId => { + return [{ + ...baseAnswer, + answerId + }] +} + export default [ { method: 'GET', path: constants.routes.SMELL_CLOTHING_AND_HAIR, handler: handlers.get + }, + { + method: 'POST', + path: constants.routes.SMELL_CLOTHING_AND_HAIR, + handler: handlers.post } ] diff --git a/server/routes/smell/effect-on-daily-life.js b/server/routes/smell/effect-on-daily-life.js new file mode 100644 index 0000000..5d27ba5 --- /dev/null +++ b/server/routes/smell/effect-on-daily-life.js @@ -0,0 +1,15 @@ +import constants from '../../utils/constants.js' + +const handlers = { + get: (_request, _h) => { + return 'hello world' + } +} + +export default [ + { + method: 'GET', + path: constants.routes.SMELL_EFFECT_ON_DAILY_LIFE, + handler: handlers.get + } +] diff --git a/server/utils/constants.js b/server/utils/constants.js index 4533faf..6b85d6a 100644 --- a/server/utils/constants.js +++ b/server/utils/constants.js @@ -53,6 +53,7 @@ const SMELL_CURRENT = 'smell/current' const SMELL_SMELL_STRENGTH = 'smell/smell-strength' const SMELL_INDOORS = 'smell/indoors' const SMELL_CLOTHING_AND_HAIR = 'smell/clothing-and-hair' +const SMELL_EFFECT_ON_DAILY_LIFE = 'smell/effect-on-daily-life' const SMELL_SOURCE = 'smell/source' const SMELL_REPORT_LOCAL_COUNCIL = 'smell/report-local-council' const SMELL_CONTACT_LOCAL_COUNCIL = 'smell/contact-local-council' @@ -110,6 +111,7 @@ const views = { SMELL_SMELL_STRENGTH, SMELL_INDOORS, SMELL_CLOTHING_AND_HAIR, + SMELL_EFFECT_ON_DAILY_LIFE, SMELL_CONTACT, SMELL_IMAGES_OR_VIDEO, SMELL_OTHER_INFORMATION diff --git a/server/utils/question-sets.js b/server/utils/question-sets.js index def7a86..3567eee 100644 --- a/server/utils/question-sets.js +++ b/server/utils/question-sets.js @@ -517,6 +517,21 @@ const questionSets = { } } }, + SMELL_CLOTHING_AND_HAIR: { + questionId: 500, + key: constants.redisKeys.SMELL_CLOTHING_AND_HAIR, + text: 'Does the smell stick to your clothing or hair?', + answers: { + yes: { + answerId: 501, + text: 'Yes' + }, + no: { + answerId: 502, + text: 'No' + } + } + }, SMELL_CONTACT: { questionId: 1, key: constants.redisKeys.SMELL_CONTACT, diff --git a/server/views/smell/clothing-and-hair.html b/server/views/smell/clothing-and-hair.html new file mode 100644 index 0000000..6db7c50 --- /dev/null +++ b/server/views/smell/clothing-and-hair.html @@ -0,0 +1,21 @@ +{% extends 'form-layout.html' %} + +{% set title = (question.text if current else question.text.replace('Does', 'Did')) %} +{% set pageTitle = title.replace('?', '') %} + +{% set items = [ + { + value: question.answers.yes.answerId, + text: question.answers.yes.text + }, + { + value: question.answers.no.answerId, + text: question.answers.no.text + } +]%} + +{% block formContent %} + + {% include "partials/radios.html" %} + +{% endblock %}