From f1b321a3bb72b4345e9b18aa762a0dc04fd8bc0b Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Thu, 13 Jun 2024 16:14:01 +0100 Subject: [PATCH 1/2] Fix No returns required missing notification banner When a user changes the reason for a no returns requirement then the update notification banner should be displayed on the requirements check page. Ticket - https://eaflood.atlassian.net/jira/software/c/projects/WATER/boards/96?selectedIssue=WATER-4386 AC 4 states the no returns required page needs to show the same update message when the reason is changed. Previous work / pr - #1079 --- .../return-requirements.controller.js | 4 +- .../submit-no-returns-required.service.js | 8 +++- .../view-licence-set-up.service.test.js | 3 ++ ...submit-no-returns-required.service.test.js | 43 +++++++++++++++++-- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/app/controllers/return-requirements.controller.js b/app/controllers/return-requirements.controller.js index c671d0e01e..f0c2090ed4 100644 --- a/app/controllers/return-requirements.controller.js +++ b/app/controllers/return-requirements.controller.js @@ -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) diff --git a/app/services/return-requirements/submit-no-returns-required.service.js b/app/services/return-requirements/submit-no-returns-required.service.js index ee9ff109e2..25fa6ca285 100644 --- a/app/services/return-requirements/submit-no-returns-required.service.js +++ b/app/services/return-requirements/submit-no-returns-required.service.js @@ -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 @@ -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} 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 diff --git a/test/services/licences/view-licence-set-up.service.test.js b/test/services/licences/view-licence-set-up.service.test.js index 29ded7aaef..2f411f83e8 100644 --- a/test/services/licences/view-licence-set-up.service.test.js +++ b/test/services/licences/view-licence-set-up.service.test.js @@ -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') @@ -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', diff --git a/test/services/return-requirements/submit-no-returns-required.service.test.js b/test/services/return-requirements/submit-no-returns-required.service.test.js index 5c5a9d0a9d..2653fa79a1 100644 --- a/test/services/return-requirements/submit-no-returns-required.service.test.js +++ b/test/services/return-requirements/submit-no-returns-required.service.test.js @@ -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 @@ -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: { @@ -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', () => { @@ -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', () => { From b0b9cf67278093754aaff8aaaa5f9bd9b3b640bd Mon Sep 17 00:00:00 2001 From: jonathangoulding Date: Thu, 13 Jun 2024 16:35:50 +0100 Subject: [PATCH 2/2] fix: test coverage --- .../return-requirements.controller.test.js | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/controllers/return-requirements.controller.test.js b/test/controllers/return-requirements.controller.test.js index ec515f37f3..4b1c43d9cc 100644 --- a/test/controllers/return-requirements.controller.test.js +++ b/test/controllers/return-requirements.controller.test.js @@ -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') @@ -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', () => {