-
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.
SIR-903: new page for pollution-substance (#143)
* new page for pollution-substance * sonarcloud fix
- Loading branch information
Showing
9 changed files
with
345 additions
and
7 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
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
91 changes: 91 additions & 0 deletions
91
server/routes/__tests__/water-pollution/pollution-substance.spec.js
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,91 @@ | ||
import { submitGetRequest, submitPostRequest } from '../../../__test-helpers__/server.js' | ||
import constants from '../../../utils/constants.js' | ||
import { questionSets } from '../../../utils/question-sets.js' | ||
|
||
const url = constants.routes.WATER_POLLUTION_POLLUTION_SUBSTANCE | ||
const question = questionSets.WATER_POLLUTION.questions.WATER_POLLUTION_POLLUTION_SUBSTANCE | ||
const baseAnswer = { | ||
questionId: question.questionId, | ||
questionAsked: question.text, | ||
questionResponse: true | ||
} | ||
|
||
describe(url, () => { | ||
describe('GET', () => { | ||
it(`Should return success response and correct view for ${url}`, async () => { | ||
await submitGetRequest({ url }, baseAnswer.questionAsked) | ||
}) | ||
}) | ||
|
||
describe('POST', () => { | ||
it('Happy: accepts valid single answerID (non array) and redirects to WATER_POLLUTION_POLLUTION_APPEARANCE', async () => { | ||
const answerId = question.answers.sewage.answerId.toString() | ||
const options = { | ||
url, | ||
payload: { | ||
answerId | ||
} | ||
} | ||
const response = await submitPostRequest(options) | ||
expect(response.headers.location).toEqual(constants.routes.WATER_POLLUTION_POLLUTION_APPEARANCE) | ||
expect(response.request.yar.get(constants.redisKeys.WATER_POLLUTION_POLLUTION_SUBSTANCE)).toEqual([{ | ||
...baseAnswer, | ||
answerId: question.answers.sewage.answerId | ||
}]) | ||
}) | ||
it('Happy: accepts valid array of answerID and redirects to WATER_POLLUTION_POLLUTION_APPEARANCE', async () => { | ||
const answerId = [question.answers.sewage.answerId.toString(), question.answers.chemical.answerId.toString(), question.answers.rural.answerId.toString()] | ||
const options = { | ||
url, | ||
payload: { | ||
answerId | ||
} | ||
} | ||
const response = await submitPostRequest(options) | ||
expect(response.headers.location).toEqual(constants.routes.WATER_POLLUTION_POLLUTION_APPEARANCE) | ||
expect(response.request.yar.get(constants.redisKeys.WATER_POLLUTION_POLLUTION_SUBSTANCE)).toEqual([{ | ||
...baseAnswer, | ||
answerId: question.answers.sewage.answerId | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.chemical.answerId | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.rural.answerId | ||
} | ||
]) | ||
}) | ||
it('Happy: accepts valid answers with something else and other details and redirects to WATER_POLLUTION_POLLUTION_APPEARANCE', async () => { | ||
const answerId = question.answers.somethingElse.answerId.toString() | ||
const options = { | ||
url, | ||
payload: { | ||
answerId, | ||
somethingElseDetail: 'something else' | ||
} | ||
} | ||
const response = await submitPostRequest(options) | ||
expect(response.headers.location).toEqual(constants.routes.WATER_POLLUTION_POLLUTION_APPEARANCE) | ||
expect(response.request.yar.get(constants.redisKeys.WATER_POLLUTION_POLLUTION_SUBSTANCE)).toEqual([{ | ||
...baseAnswer, | ||
answerId: question.answers.somethingElse.answerId | ||
}, { | ||
...baseAnswer, | ||
answerId: question.answers.somethingElseDetail.answerId, | ||
otherDetails: 'something else' | ||
}]) | ||
}) | ||
it('Happy: accepts empty answerId, defaults to you do not know and redirects to WATER_POLLUTION_POLLUTION_APPEARANCE', async () => { | ||
const options = { | ||
url, | ||
payload: {} | ||
} | ||
const response = await submitPostRequest(options) | ||
expect(response.headers.location).toEqual(constants.routes.WATER_POLLUTION_POLLUTION_APPEARANCE) | ||
expect(response.request.yar.get(constants.redisKeys.WATER_POLLUTION_POLLUTION_SUBSTANCE)).toEqual([{ | ||
...baseAnswer, | ||
answerId: question.answers.unknown.answerId | ||
}]) | ||
}) | ||
}) | ||
}) |
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,79 @@ | ||
import constants from '../../utils/constants.js' | ||
import { questionSets } from '../../utils/question-sets.js' | ||
const question = questionSets.WATER_POLLUTION.questions.WATER_POLLUTION_POLLUTION_SUBSTANCE | ||
|
||
const baseAnswer = { | ||
questionId: question.questionId, | ||
questionAsked: question.text, | ||
questionResponse: true | ||
} | ||
|
||
const handlers = { | ||
get: async (_request, h) => h.view(constants.views.WATER_POLLUTION_POLLUTION_SUBSTANCE, { | ||
...getContext() | ||
}), | ||
post: async (request, h) => { | ||
// get payload | ||
let { answerId, somethingElseDetail } = request.payload | ||
|
||
// Convert answer to array if only a single string answer | ||
if (!Array.isArray(answerId)) { | ||
answerId = [answerId] | ||
} | ||
|
||
// set answer in session | ||
request.yar.set(constants.redisKeys.WATER_POLLUTION_POLLUTION_SUBSTANCE, buildAnswers(answerId, somethingElseDetail)) | ||
|
||
return h.redirect(constants.routes.WATER_POLLUTION_POLLUTION_APPEARANCE) | ||
} | ||
} | ||
|
||
const buildAnswers = (answerId, somethingElseDetail) => { | ||
const answers = [] | ||
let somethingElse = false | ||
// if no answer selected default to You do not know | ||
if (answerId.length === 1 && !answerId[0]) { | ||
answers.push({ | ||
...baseAnswer, | ||
answerId: question.answers.unknown.answerId | ||
}) | ||
} else { | ||
answerId.forEach(item => { | ||
if (Number(item) === question.answers.somethingElse.answerId) { | ||
somethingElse = true | ||
} | ||
answers.push({ | ||
...baseAnswer, | ||
answerId: Number(item) | ||
}) | ||
}) | ||
if (somethingElse && somethingElseDetail) { | ||
answers.push({ | ||
...baseAnswer, | ||
answerId: question.answers.somethingElseDetail.answerId, | ||
otherDetails: somethingElseDetail | ||
}) | ||
} | ||
} | ||
|
||
return answers | ||
} | ||
|
||
const getContext = () => { | ||
return { | ||
question | ||
} | ||
} | ||
|
||
export default [ | ||
{ | ||
method: 'GET', | ||
path: constants.routes.WATER_POLLUTION_POLLUTION_SUBSTANCE, | ||
handler: handlers.get | ||
}, | ||
{ | ||
method: 'POST', | ||
path: constants.routes.WATER_POLLUTION_POLLUTION_SUBSTANCE, | ||
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
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
Oops, something went wrong.