Skip to content

Commit 9670ac1

Browse files
authored
feat/DF-607: Handles partial page when clicking 'Save and exit' (#1031)
* Handles partial page when clicking 'Save and exit' * Extra coverage * Updated plugin version * Saves path and invalid state * Fixed test * Change of property name * Saves URL at point of saving payload to flash * Removed fallback '/' * Bypass save if not path stored * Stash * Extra coverage Extracted method to new file for easy mock * Lint fix
1 parent 9682116 commit 9670ac1

10 files changed

Lines changed: 180 additions & 9 deletions

File tree

package-lock.json

Lines changed: 31 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"license": "SEE LICENSE IN LICENSE",
4242
"dependencies": {
4343
"@aws-sdk/client-sns": "^3.965.0",
44-
"@defra/forms-engine-plugin": "^4.0.34",
44+
"@defra/forms-engine-plugin": "^4.0.35",
4545
"@defra/forms-model": "^3.0.601",
4646
"@defra/hapi-tracing": "^1.30.0",
4747
"@elastic/ecs-pino-format": "^1.5.0",

src/server/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export const PREVIEW_PATH_PREFIX = '/preview'
22
export const ERROR_PREVIEW_PATH_PREFIX = '/error-preview'
33
export const FORM_PREFIX = '/form'
4+
export const SAVE_AND_EXIT_PAYLOAD = 'SAVE_AND_EXIT_PAYLOAD'

src/server/index.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { type Server } from '@hapi/hapi'
33
import { StatusCodes } from 'http-status-codes'
44

55
import { FORM_PREFIX } from '~/src/server/constants.js'
6-
import { createServer } from '~/src/server/index.js'
6+
import { configureEnginePlugin, createServer } from '~/src/server/index.js'
77
import {
88
getFormDefinition,
99
getFormMetadata
@@ -488,4 +488,36 @@ describe('Model cache', () => {
488488
expect(res.statusCode).toBe(StatusCodes.OK)
489489
})
490490
})
491+
492+
describe('configureEnginePlugin', () => {
493+
const mockFlash = jest.fn()
494+
const mockYar = {
495+
flash: mockFlash
496+
}
497+
const mockRequest = /** @type {any} */ {
498+
params: {
499+
slug: 'test-form'
500+
},
501+
yar: mockYar,
502+
url: {
503+
pathname: '/my-path'
504+
}
505+
}
506+
const mockH = {
507+
redirect: jest.fn()
508+
}
509+
test('should handle save and exit', async () => {
510+
const [pluginObject] = await configureEnginePlugin({})
511+
expect(pluginObject).toBeDefined()
512+
const saveAndExitFunc = pluginObject.options.saveAndExit
513+
expect(saveAndExitFunc).toBeDefined()
514+
saveAndExitFunc(mockRequest, mockH, undefined)
515+
expect(mockFlash).toHaveBeenCalledWith(
516+
'SAVE_AND_EXIT_PAYLOAD',
517+
{ action: undefined, crumb: undefined, __currentPagePath: '/my-path' },
518+
true
519+
)
520+
expect(mockH.redirect).toHaveBeenCalledWith('/save-and-exit/test-form')
521+
})
522+
})
491523
})

src/server/index.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { join, parse } from 'path'
22

3-
import plugin from '@defra/forms-engine-plugin'
3+
import plugin, { CURRENT_PAGE_PATH_KEY } from '@defra/forms-engine-plugin'
44
import { checkFormStatus } from '@defra/forms-engine-plugin/engine/helpers.js'
55
import { FormModel } from '@defra/forms-engine-plugin/engine/models/FormModel.js'
66
import { formSubmissionService } from '@defra/forms-engine-plugin/services/index.js'
@@ -29,7 +29,7 @@ import forwardLogs from '~/src/server/common/helpers/logging/forward-logs.js'
2929
import { requestLogger } from '~/src/server/common/helpers/logging/request-logger.js'
3030
import { requestTracing } from '~/src/server/common/helpers/logging/request-tracing.js'
3131
import { buildRedisClient } from '~/src/server/common/helpers/redis-client.js'
32-
import { FORM_PREFIX } from '~/src/server/constants.js'
32+
import { FORM_PREFIX, SAVE_AND_EXIT_PAYLOAD } from '~/src/server/constants.js'
3333
import { FeedbackPageController } from '~/src/server/plugins/FeedbackPageController.js'
3434
import { SummaryPageWithConfirmationEmailController } from '~/src/server/plugins/SummaryPageWithConfirmationEmailController.js'
3535
import { configureBlankiePlugin } from '~/src/server/plugins/blankie.js'
@@ -159,10 +159,21 @@ export const configureEnginePlugin = async ({
159159
h: FormResponseToolkit,
160160
_context: FormContext
161161
) => {
162-
const { params } = request
162+
const { params, yar } = request
163163
const { slug } = params
164164
const { isPreview, state } = checkFormStatus(params)
165165

166+
// Payload from current page without crumb and action
167+
// (in case current page is not saved to state yet i.e. only partially completed)
168+
const pagePayload = {
169+
...request.payload,
170+
crumb: undefined,
171+
action: undefined,
172+
[CURRENT_PAGE_PATH_KEY]: request.url.pathname
173+
}
174+
175+
yar.flash(SAVE_AND_EXIT_PAYLOAD, pagePayload, true)
176+
166177
return h.redirect(
167178
!isPreview
168179
? `/save-and-exit/${slug}`
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { SAVE_AND_EXIT_PAYLOAD } from '~/src/server/constants.js'
2+
3+
/**
4+
* Get Save and Exit payload - extracted here to a separate file so it can easily be mocked
5+
* @param {Request<{ Params: SaveAndExitParams }>} request
6+
*/
7+
export function getPayloadFromFlash(request) {
8+
return request.yar.flash(SAVE_AND_EXIT_PAYLOAD)
9+
}
10+
11+
/**
12+
* @import { Request } from '@hapi/hapi'
13+
* @import { SaveAndExitParams } from '~/src/server/models/save-and-exit.js'
14+
*/

0 commit comments

Comments
 (0)