Skip to content

Commit

Permalink
feat(app): updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Demwunz committed May 14, 2024
1 parent 4254b22 commit 55b9d35
Show file tree
Hide file tree
Showing 12 changed files with 404 additions and 44 deletions.
6 changes: 3 additions & 3 deletions app/controllers/return-requirements.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@ async function submitAgreementsExceptions (request, h) {
}

async function submitAdditionalSubmissionOptions (request, h) {
const { params: { requirementIndex, sessionId }, payload, yar } = request
const { params: { sessionId }, payload, yar } = request

const pageData = await SubmitAdditionalSubmissionOptionsService.go(sessionId, requirementIndex, payload, yar)
const pageData = await SubmitAdditionalSubmissionOptionsService.go(sessionId, payload, yar)

if (pageData.error) {
return h.view('return-requirements/additional-submission-options.njk', pageData)
}

return h.redirect(`/system/return-requirements/${sessionId}/check-your-answers`)
return h.redirect(`/system/return-requirements/${sessionId}/check`)
}

async function submitCancel (request, h) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@
*
* @returns {Object} - The data formatted for the view template
*/
function go (session, requirementIndex) {
const { id, licence: { id: licenceId, licenceRef }, requirements } = session
const requirement = requirements[requirementIndex]
function go (session) {
const { id: sessionId, licence: { id: licenceId, licenceRef }, additionalSubmissionOptions } = session
const data = {
id,
additionalSubmissionOptions: additionalSubmissionOptions || '',
backLink: `/system/return-requirements/${sessionId}/check`,
licenceId,
licenceRef,
additionalSubmissionOptions: requirement?.additionalSubmissionOptions
? requirement.additionalSubmissionOptions.join(',')
: ''
sessionId
}

return data
Expand Down
2 changes: 1 addition & 1 deletion app/presenters/return-requirements/check.presenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function go (session) {
const { additionalSubmissionOptions, id: sessionId, journey, licence, note, reason } = session

return {
additionalSubmissionOptions: additionalSubmissionOptions || null,
additionalSubmissionOptions: additionalSubmissionOptions ? additionalSubmissionOptions.includes('multiple-upload') : false,
journey,
licenceRef: licence.licenceRef,
note: note ? note.content : null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function go (sessionId) {

return {
activeNavBar: 'search',
pageTitle: 'Select any additional submission options for the requirements for returns',
pageTitle: 'Select any additional submission options for the return requirements',
...formattedData
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ const SessionModel = require('../../models/session.model.js')
* @returns {Promise<Object>} If no errors it returns an empty object else the page data for the note page including the
* validation error details
*/
async function go (sessionId, requirementIndex, payload, yar) {
async function go (sessionId, payload, yar) {
const session = await SessionModel.query().findById(sessionId)

_handleOneOptionSelected(payload)
payload['additional-submission-options'] = _ensureIsArray(payload['additional-submission-options'])

const validationResult = _validate(payload)

if (!validationResult) {
const notification = _notification(session, requirementIndex, payload['additional-submission-options'])
await _save(session, requirementIndex, payload)
const notification = _notification(session, payload['additional-submission-options'])
await _save(session, payload)

if (notification) {
yar.flash('notification', notification)
Expand All @@ -44,7 +44,7 @@ async function go (sessionId, requirementIndex, payload, yar) {
return {}
}

const submittedSessionData = _submittedSessionData(session, requirementIndex, payload)
const submittedSessionData = _submittedSessionData(session, payload)

return {
activeNavBar: 'search',
Expand All @@ -54,38 +54,31 @@ async function go (sessionId, requirementIndex, payload, yar) {
}
}

/**
* When a single point is checked by the user, it returns as a string. When multiple points are checked, the
* 'points' is returned as an array. This function works to make those single selected string 'points' into an array
* for uniformity.
*/
function _handleOneOptionSelected (payload) {
if (!Array.isArray(payload['additional-submission-options'])) {
payload['additional-submission-options'] = [payload['additional-submission-options']]
}
function _ensureIsArray (options) {
return Array.isArray(options) ? options : [options]
}

function _notification (session, requirementIndex, newOptions) {
const additionalSubmissionOptions = session.requirements[requirementIndex]?.additionalSubmissionOptions
function _notification (session, newOptions) {
const additionalSubmissionOptions = session?.additionalSubmissionOptions

if (additionalSubmissionOptions && additionalSubmissionOptions !== newOptions) {
if (additionalSubmissionOptions !== newOptions) {
return {
text: 'Changes updates',
text: 'Changes updated',
title: 'Updated'
}
}

return null
}

async function _save (session, requirementIndex, payload) {
session.requirements[requirementIndex].additionalSubmissionOptions = payload['additional-submission-options']
async function _save (session, payload) {
session.additionalSubmissionOptions = payload['additional-submission-options']

return session.$update()
}

function _submittedSessionData (session, requirementIndex, payload) {
session.requirements[requirementIndex].additionalSubmissionOptions = payload['additional-submission-options'] || null
function _submittedSessionData (session, payload) {
session.additionalSubmissionOptions = payload['additional-submission-options'] || null

return AdditionalSubmissionOptionsPresenter.go(session)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,20 @@ function go (payload) {
* To make Joi validation straightforward, if the 'payload.additionalSubmissionOptions' is a string,
* it is turned into an array and validated as such.
*/
let additionalSubmissionOptions = payload['additional-submission-options']

if (!Array.isArray(additionalSubmissionOptions)) {
additionalSubmissionOptions = [additionalSubmissionOptions]
}
const additionalSubmissionOptions = payload['additional-submission-options']

const errorMessage = 'Select additional submission options for the requirements for returns'

const schema = Joi.object({
points: Joi.array()
'additional-submission-options': Joi.array()
.items(Joi.string())
.required()
}).messages({
'any.required': errorMessage,
'array.sparse': errorMessage
})

return schema.validate({ additionalSubmissionOptions }, { abortEarly: false })
return schema.validate({ 'additional-submission-options': additionalSubmissionOptions }, { abortEarly: false })
}

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
{% from "govuk/components/checkboxes/macro.njk" import govukCheckboxes %}
{% from 'govuk/components/error-summary/macro.njk' import govukErrorSummary %}

{% set backLink = "/system/return-requirements/" + id + "/check" %}
{% block breadcrumbs %}
{# Back link #}
{{
Expand All @@ -21,7 +20,7 @@
titleText: 'There is a problem',
errorList: [
{
text: 'error.text',
text: error.text,
href: '#additional-submission-options'
}
]
Expand Down Expand Up @@ -49,7 +48,7 @@
text: "Select all that apply"
},
errorMessage: {
text: "error.text"
text: error.text
} if error,
items: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Thing under test
const AdditionalSubmissionOptionsPresenter = require('../../../app/presenters/return-requirements/additional-submission-options.presenter.js')

describe('Return Requirements - Additional Submission Options presenter', () => {
let session

beforeEach(() => {
session = {
id: '61e07498-f309-4829-96a9-72084a54996d',
checkPageVisited: false,
licence: {
id: '8b7f78ba-f3ad-4cb6-a058-78abc4d1383d',
currentVersionStartDate: '2023-01-01T00:00:00.000Z',
endDate: null,
licenceRef: '01/ABC',
licenceHolder: 'Turbo Kid',
startDate: '2022-04-01T00:00:00.000Z'
},
journey: 'returns-required',
requirements: [{}],
startDateOptions: 'licenceStartDate',
reason: 'major-change'
}
})

describe('when provided with a session', () => {
it('correctly presents the data without additional submission options', () => {
const result = AdditionalSubmissionOptionsPresenter.go(session)

expect(result).to.be.equal({
backLink: '/system/return-requirements/61e07498-f309-4829-96a9-72084a54996d/check',
licenceId: '8b7f78ba-f3ad-4cb6-a058-78abc4d1383d',
additionalSubmissionOptions: '',
licenceRef: '01/ABC'
})
})
})

describe("the 'backLink' property", () => {
it("returns a link back to the 'check' page", () => {
const result = AdditionalSubmissionOptionsPresenter.go(session)

expect(result.backLink).to.equal('/system/return-requirements/61e07498-f309-4829-96a9-72084a54996d/check')
})
})

describe("the 'additionalSubmissionOptions' property", () => {
describe('when the user has previously submitted additional submission options', () => {
beforeEach(() => {
session.additionalSubmissionOptions = ['multiple-upload']
})

it('returns the options', () => {
const result = AdditionalSubmissionOptionsPresenter.go(session)

expect(result.additionalSubmissionOptions).to.equal(['multiple-upload'])
})
})

describe('when the user has not previously chosen options', () => {
it('returns empty options', () => {
const result = AdditionalSubmissionOptionsPresenter.go(session)

expect(result.additionalSubmissionOptions).to.be.equal('')
})
})
})
})
23 changes: 23 additions & 0 deletions test/presenters/return-requirements/check.presenter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ describe('Return Requirements - Check presenter', () => {
const result = CheckPresenter.go(session)

expect(result).to.equal({
additionalSubmissionOptions: false,
journey: 'returns-required',
licenceRef: '01/ABC',
note: null,
Expand All @@ -50,6 +51,28 @@ describe('Return Requirements - Check presenter', () => {
})
})

describe("the 'additionalSubmissionOptions' property", () => {
describe('when the user has checked additionalSubmissionOptions', () => {
beforeEach(() => {
session.additionalSubmissionOptions = ['multiple-upload']
})

it('returns a checked option', () => {
const result = CheckPresenter.go(session)

expect(result.additionalSubmissionOptions).to.be.true()
})
})

describe('when the user has not checked an option', () => {
it('returns no options', () => {
const result = CheckPresenter.go(session)

expect(result.additionalSubmissionOptions).to.be.false()
})
})
})

describe("the 'note' property", () => {
describe('when the user has added a note', () => {
beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict'

// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')

const { describe, it, beforeEach } = exports.lab = Lab.script()
const { expect } = Code

// Test helpers
const DatabaseSupport = require('../../support/database.js')
const SessionHelper = require('../../support/helpers/session.helper.js')

// Thing under test
const AdditionalSubmissionOptionsService = require('../../../app/services/return-requirements/additional-submission-options.service.js')

describe('Return Requirements - Additional Submission Options service', () => {
let session

beforeEach(async () => {
await DatabaseSupport.clean()

session = await SessionHelper.add({
data: {
checkPageVisited: false,
licence: {
id: '8b7f78ba-f3ad-4cb6-a058-78abc4d1383d',
currentVersionStartDate: '2023-01-01T00:00:00.000Z',
endDate: null,
licenceRef: '01/ABC',
licenceHolder: 'Turbo Kid',
startDate: '2022-04-01T00:00:00.000Z'
},
journey: 'returns-required',
requirements: [{}],
startDateOptions: 'licenceStartDate',
reason: 'major-change'
}
})
})

describe('when called', () => {
it('fetches the current setup session record', async () => {
const result = await AdditionalSubmissionOptionsService.go(session.id)

expect(result.sessionId).to.equal(session.id)
})

it('returns page data for the view', async () => {
const result = await AdditionalSubmissionOptionsService.go(session.id)

expect(result).to.equal({
activeNavBar: 'search',
additionalSubmissionOptions: '',
backLink: `/system/return-requirements/${session.id}/check`,
licenceId: '8b7f78ba-f3ad-4cb6-a058-78abc4d1383d',
licenceRef: '01/ABC',
pageTitle: 'Select any additional submission options for the return requirements'
}, { skip: ['sessionId'] })
})
})
})
Loading

0 comments on commit 55b9d35

Please sign in to comment.