Skip to content

Commit

Permalink
SIR-903: new page for pollution-substance (#143)
Browse files Browse the repository at this point in the history
* new page for pollution-substance

* sonarcloud fix
  • Loading branch information
teddmason committed Aug 22, 2024
1 parent 1a40bfc commit 97db0af
Show file tree
Hide file tree
Showing 9 changed files with 345 additions and 7 deletions.
38 changes: 38 additions & 0 deletions server/__mock-data__/session-water-pollution.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import moment from 'moment'

const yesterday = moment().seconds(0).milliseconds(0).subtract(1, 'days')
const today = moment().seconds(0).milliseconds(0)
const pollutionSubstance = 'What do you think the pollution is?'

const session = JSON.parse(`{
"home": {
Expand Down Expand Up @@ -80,6 +81,27 @@ const session = JSON.parse(`{
"answerId": 2801
}
],
"water-pollution/pollution-substance": [
{
"questionId": 2900,
"questionAsked": "What do you think the pollution is?",
"questionResponse": true,
"answerId": 2901
},
{
"questionId": 2900,
"questionAsked": "What do you think the pollution is?",
"questionResponse": true,
"answerId": 2905
},
{
"questionId": 2900,
"questionAsked": "What do you think the pollution is?",
"questionResponse": true,
"answerId": 2907,
"otherDetails": "other details"
}
],
"water-pollution/other-information": "test",
"water-pollution/when": "${yesterday.toISOString()}",
"submission-timestamp": "${today.toISOString()}"
Expand Down Expand Up @@ -145,6 +167,22 @@ const payload =
questionAsked: 'Do you want to send us any images or videos of the pollution?',
questionResponse: true,
answerId: 2801
}, {
questionId: 2900,
questionAsked: pollutionSubstance,
questionResponse: true,
answerId: 2901
}, {
questionId: 2900,
questionAsked: pollutionSubstance,
questionResponse: true,
answerId: 2905
}, {
questionId: 2900,
questionAsked: pollutionSubstance,
questionResponse: true,
answerId: 2907,
otherDetails: 'other details'
}]
}
}
Expand Down
18 changes: 18 additions & 0 deletions server/routes/__tests__/water-pollution/other-information.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ describe(url, () => {
questionAsked: 'Do you want to send us any images or videos of the pollution?',
questionResponse: true,
answerId: 2801
}),
expect.objectContaining({
questionId: 2900,
questionAsked: 'What do you think the pollution is?',
questionResponse: true,
answerId: 2901
}),
expect.objectContaining({
questionId: 2900,
questionAsked: 'What do you think the pollution is?',
questionResponse: true,
answerId: 2905
}),
expect.objectContaining({
questionId: 2900,
questionAsked: 'What do you think the pollution is?',
questionResponse: true,
answerId: 2907
})
])
})
Expand Down
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
}])
})
})
})
12 changes: 6 additions & 6 deletions server/routes/__tests__/water-pollution/when.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ describe(url, () => {
})

describe('POST', () => {
it('Happy: accept a valid date time and continue to WATER_POLLUTION_POLLUTION_APPEARANCE', async () => {
it('Happy: accept a valid date time and continue to WATER_POLLUTION_POLLUTION_SUBSTANCE', async () => {
const dateTime = moment().seconds(0).milliseconds(0).subtract(1, 'days')
const response = await submitPostRequest(getOptions(dateTime))
expect(response.headers.location).toEqual(constants.routes.WATER_POLLUTION_POLLUTION_APPEARANCE)
expect(response.headers.location).toEqual(constants.routes.WATER_POLLUTION_POLLUTION_SUBSTANCE)
expect(response.request.yar.get(constants.redisKeys.WATER_POLLUTION_WHEN)).toEqual(dateTime.toISOString())
})
it('Happy: accept a valid date time and continue to WATER_POLLUTION_POLLUTION_APPEARANCE', async () => {
it('Happy: accept a valid date time and continue to WATER_POLLUTION_POLLUTION_SUBSTANCE', async () => {
const dateTime = moment().seconds(0).milliseconds(0).subtract(1, 'years').add(5, 'minutes')
const response = await submitPostRequest(getOptions(dateTime))
expect(response.headers.location).toEqual(constants.routes.WATER_POLLUTION_POLLUTION_APPEARANCE)
expect(response.headers.location).toEqual(constants.routes.WATER_POLLUTION_POLLUTION_SUBSTANCE)
expect(response.request.yar.get(constants.redisKeys.WATER_POLLUTION_WHEN)).toEqual(dateTime.toISOString())
})
it('Happy: accept a valid date time and continue to WATER_POLLUTION_POLLUTION_APPEARANCE', async () => {
it('Happy: accept a valid date time and continue to WATER_POLLUTION_POLLUTION_SUBSTANCE', async () => {
const dateTime = moment().seconds(0).milliseconds(0).add(3, 'minutes')
const response = await submitPostRequest(getOptions(dateTime))
expect(response.headers.location).toEqual(constants.routes.WATER_POLLUTION_POLLUTION_APPEARANCE)
expect(response.headers.location).toEqual(constants.routes.WATER_POLLUTION_POLLUTION_SUBSTANCE)
expect(response.request.yar.get(constants.redisKeys.WATER_POLLUTION_WHEN)).toEqual(dateTime.toISOString())
})
it('Sad path: valid datetime must not > 5 minutes into the future', async () => {
Expand Down
79 changes: 79 additions & 0 deletions server/routes/water-pollution/pollution-substance.js
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
}
]
2 changes: 1 addition & 1 deletion server/routes/water-pollution/when.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const handlers = {

request.yar.set(constants.redisKeys.WATER_POLLUTION_WHEN, dateTime.toISOString())

return h.redirect(constants.routes.WATER_POLLUTION_POLLUTION_APPEARANCE)
return h.redirect(constants.routes.WATER_POLLUTION_POLLUTION_SUBSTANCE)
}
}

Expand Down
2 changes: 2 additions & 0 deletions server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const WATER_POLLUTION_LOCATION_OPTION = 'water-pollution/location-option'
const WATER_POLLUTION_LOCATION_MAP = 'water-pollution/location-map'
const WATER_POLLUTION_LOCATION_DESCRIPTION = 'water-pollution/location-description'
const WATER_POLLUTION_WHEN = 'water-pollution/when'
const WATER_POLLUTION_POLLUTION_SUBSTANCE = 'water-pollution/pollution-substance'
const WATER_POLLUTION_POLLUTION_APPEARANCE = 'water-pollution/pollution-appearance'
const WATER_POLLUTION_IMAGES_OR_VIDEO = 'water-pollution/images-or-video'
const WATER_POLLUTION_LESS_THAN_10_METRES = 'water-pollution/less-than-10-metres'
Expand Down Expand Up @@ -79,6 +80,7 @@ const views = {
WATER_POLLUTION_LOCATION_MAP,
WATER_POLLUTION_LOCATION_DESCRIPTION,
WATER_POLLUTION_WHEN,
WATER_POLLUTION_POLLUTION_SUBSTANCE,
WATER_POLLUTION_POLLUTION_APPEARANCE,
WATER_POLLUTION_IMAGES_OR_VIDEO,
WATER_POLLUTION_LESS_THAN_10_METRES,
Expand Down
35 changes: 35 additions & 0 deletions server/utils/question-sets.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,41 @@ const questionSets = {
text: 'Give details about what you\'ve seen, including the type and number of fish or animals affected'
}
}
},
WATER_POLLUTION_POLLUTION_SUBSTANCE: {
questionId: 2900,
key: constants.redisKeys.WATER_POLLUTION_POLLUTION_SUBSTANCE,
text: 'What do you think the pollution is?',
answers: {
sewage: {
answerId: 2901,
text: 'Sewage'
},
chemical: {
answerId: 2902,
text: 'Oil or petrol'
},
rural: {
answerId: 2903,
text: 'Agricultural waste, for example from muck spreading'
},
refuse: {
answerId: 2904,
text: 'Rubbish or refuse'
},
somethingElse: {
answerId: 2905,
text: SOMETHING_ELSE
},
unknown: {
answerId: 2906,
text: 'You do not know'
},
somethingElseDetail: {
answerId: 2907,
text: 'Give details of what you think is in the water'
}
}
}
}
},
Expand Down
Loading

0 comments on commit 97db0af

Please sign in to comment.