-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Technical implementation for SIR-945
- Loading branch information
Showing
6 changed files
with
219 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %} |