Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix No returns required missing notification banner #1104

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/return-requirements.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ async function submitFrequencyReported (request, h) {
}

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

const pageData = await SubmitNoReturnsRequiredService.go(sessionId, request.payload)
const pageData = await SubmitNoReturnsRequiredService.go(sessionId, payload, yar)

if (pageData.error) {
return h.view('return-requirements/no-returns-required.njk', pageData)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
const NoReturnsRequiredPresenter = require('../../presenters/return-requirements/no-returns-required.presenter.js')
const NoReturnsRequiredValidator = require('../../validators/return-requirements/no-returns-required.validator.js')
const SessionModel = require('../../models/session.model.js')
const GeneralLib = require('../../lib/general.lib')

/**
* Orchestrates validating the data for `/return-requirements/{sessionId}/no-returns-required` page
Expand All @@ -20,16 +21,21 @@ const SessionModel = require('../../models/session.model.js')
*
* @param {string} sessionId - The id of the current session
* @param {Object} payload - The submitted form data
* @param {Object} yar - The Hapi `request.yar` session manager passed on by the controller
*
* @returns {Promise<Object>} The page data for the no returns required page
*/
async function go (sessionId, payload) {
async function go (sessionId, payload, yar) {
const session = await SessionModel.query().findById(sessionId)
const validationResult = _validate(payload)

if (!validationResult) {
await _save(session, payload)

if (session.checkPageVisited) {
GeneralLib.flashNotification(yar)
}

return {
checkPageVisited: session.checkPageVisited,
journey: session.journey
Expand Down
31 changes: 31 additions & 0 deletions test/controllers/return-requirements.controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const SubmitAgreementsExceptions = require('../../app/services/return-requiremen
const SubmitExistingService = require('../../app/services/return-requirements/submit-existing.service.js')
const SubmitFrequencyCollectedService = require('../../app/services/return-requirements/submit-frequency-collected.service.js')
const SubmitFrequencyReportedService = require('../../app/services/return-requirements/submit-frequency-reported.service.js')
const SubmitNoReturnsRequiredService = require('../../app/services/return-requirements/submit-no-returns-required.service.js')
const SubmitPointsService = require('../../app/services/return-requirements/submit-points.service.js')
const SubmitPurposeService = require('../../app/services/return-requirements/submit-purpose.service.js')
const SubmitReasonService = require('../../app/services/return-requirements/submit-reason.service.js')
Expand Down Expand Up @@ -502,6 +503,36 @@ describe('Return requirements controller', () => {
})
})
})

describe('POST', () => {
describe('when the request succeeds', () => {
describe('and the validation passes', () => {
beforeEach(async () => {
Sinon.stub(SubmitNoReturnsRequiredService, 'go').resolves({ })
})

it('redirects to /system/return-requirements/{sessionId}/check', async () => {
const response = await server.inject(_postOptions(path))

expect(response.statusCode).to.equal(302)
expect(response.headers.location).to.equal('/system/return-requirements/' + sessionId + '/check')
})
})

describe('and the validation fails', () => {
beforeEach(async () => {
Sinon.stub(SubmitNoReturnsRequiredService, 'go').resolves({ error: {} })
})

it('returns the page successfully with the error summary banner', async () => {
const response = await server.inject(_postOptions(path))

expect(response.statusCode).to.equal(200)
expect(response.payload).to.contain('There is a problem')
})
})
})
})
})

describe('/return-requirements/{sessionId}/points', () => {
Expand Down
3 changes: 3 additions & 0 deletions test/services/licences/view-licence-set-up.service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { describe, it, beforeEach, afterEach } = exports.lab = Lab.script()
const { expect } = Code

// Things we need to stub
const FeatureFlagsConfig = require('../../../config/feature-flags.config.js')
const FetchAgreementsService = require('../../../app/services/licences/fetch-agreements.service.js')
const FetchChargeVersionsService = require('../../../app/services/licences/fetch-charge-versions.service.js')
const FetchReturnVersionsService = require('../../../app/services/licences/fetch-return-versions.service.js')
Expand All @@ -24,6 +25,8 @@ describe('View Licence Set Up service', () => {
let auth = {}

beforeEach(() => {
Sinon.stub(FeatureFlagsConfig, 'enableRequirementsForReturns').value(false)

Sinon.stub(FetchAgreementsService, 'go').returns([
{
id: '123',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// Test framework dependencies
const Lab = require('@hapi/lab')
const Code = require('@hapi/code')
const Sinon = require('sinon')

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

// Test helpers
Expand All @@ -17,11 +18,13 @@ const SubmitNoReturnsRequiredService = require('../../../app/services/return-req
describe('Return Requirements - Submit No Returns Required service', () => {
let payload
let session
let sessionData
let yarStub

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

session = await SessionHelper.add({
sessionData = {
data: {
checkPageVisited: false,
licence: {
Expand All @@ -36,7 +39,15 @@ describe('Return Requirements - Submit No Returns Required service', () => {
requirements: [{}],
startDateOptions: 'licenceStartDate'
}
})
}

session = await SessionHelper.add(sessionData)

yarStub = { flash: Sinon.stub() }
})

afterEach(() => {
Sinon.restore()
})

describe('when called', () => {
Expand All @@ -56,10 +67,34 @@ describe('Return Requirements - Submit No Returns Required service', () => {
})

it('returns the correct details the controller needs to redirect the journey', async () => {
const result = await SubmitNoReturnsRequiredService.go(session.id, payload)
const result = await SubmitNoReturnsRequiredService.go(session.id, payload, yarStub)

expect(result).to.equal({ checkPageVisited: false, journey: 'no-returns-required' })
})

describe('and the page has been visited', () => {
beforeEach(async () => {
session = await SessionHelper.add({ data: { ...sessionData.data, checkPageVisited: true } })
})

it('returns the correct details the controller needs to redirect the journey to the check page', async () => {
const result = await SubmitNoReturnsRequiredService.go(session.id, payload, yarStub)

expect(result).to.equal({
checkPageVisited: true,
journey: 'no-returns-required'
})
})

it('sets the notification message title to "Updated" and the text to "Changes made" ', async () => {
await SubmitNoReturnsRequiredService.go(session.id, payload, yarStub)

const [flashType, notification] = yarStub.flash.args[0]

expect(flashType).to.equal('notification')
expect(notification).to.equal({ title: 'Updated', text: 'Changes made' })
})
})
})

describe('with an invalid payload', () => {
Expand Down
Loading