Skip to content

Commit c906112

Browse files
committed
Added test
1 parent 8721fd9 commit c906112

2 files changed

Lines changed: 82 additions & 20 deletions

File tree

src/server/plugins/engine/pageControllers/helpers/state.test.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { ComponentType, type Page } from '@defra/forms-model'
1+
import { ComponentType, ControllerType, type Page } from '@defra/forms-model'
22

33
import { type FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
44
import { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers/pages.js'
55
import {
6+
checkSaveAndExitRepeater,
67
copyNotYetValidatedState,
78
prefillStateFromQueryParameters,
89
stripParam
@@ -292,4 +293,56 @@ describe('State helpers', () => {
292293
})
293294
})
294295
})
296+
297+
describe('checkSaveAndExitRepeater', () => {
298+
function createMockContextWithPath(path) {
299+
return {
300+
state: {
301+
abc: '123',
302+
__stateNotYetValidated: {
303+
def: '456',
304+
__currentPagePath: path
305+
}
306+
},
307+
payload: {}
308+
} as unknown as FormContext
309+
}
310+
311+
const mockModel = {
312+
def: {
313+
pages: [
314+
{
315+
controller: ControllerType.Repeat,
316+
path: '/personal_details'
317+
}
318+
]
319+
},
320+
basePath: 'form/preview/draft/repeater-test'
321+
} as unknown as FormModel
322+
323+
it('should return undefined if url does not end in a guid', () => {
324+
const mockContext = createMockContextWithPath(
325+
'/form/preview/draft/repeater-test/personal_details'
326+
)
327+
expect(checkSaveAndExitRepeater(mockContext, mockModel)).toBeUndefined()
328+
})
329+
330+
it('should return undefined if url ends in a guid but not a repeater path', () => {
331+
const mockContext = createMockContextWithPath(
332+
'/form/preview/draft/repeater-test/wrong_page/7d27fe6e-73e8-4265-84bd-1e118c92470b'
333+
)
334+
expect(checkSaveAndExitRepeater(mockContext, mockModel)).toBeUndefined()
335+
})
336+
337+
it('should return correct urls if url ends in a guid and is a repeater path', () => {
338+
const mockContext = createMockContextWithPath(
339+
'/form/preview/draft/repeater-test/personal_details/7d27fe6e-73e8-4265-84bd-1e118c92470b'
340+
)
341+
expect(checkSaveAndExitRepeater(mockContext, mockModel)).toEqual({
342+
pathExcludingGuid: '/personal_details',
343+
pathIncludingGuid:
344+
'/personal_details/7d27fe6e-73e8-4265-84bd-1e118c92470b'
345+
})
346+
})
347+
})
295348
})

src/server/plugins/engine/pageControllers/helpers/state.ts

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,27 +125,36 @@ export function checkSaveAndExitRepeater(
125125
.filter((page) => page.controller === ControllerType.Repeat)
126126
.map((p) => `/${model.basePath}${p.path}/`)
127127

128-
if (typeof originalPath === 'string') {
129-
const lastParam = originalPath.split('/').at(-1) ?? ''
130-
if (lastParam.length === GUID_LENGTH && isValidUUID(lastParam)) {
131-
const pathWithoutBase = originalPath.substring(model.basePath.length + 1)
132-
const pathStripGuid = originalPath.substring(
133-
0,
134-
originalPath.length - GUID_LENGTH
135-
)
136-
if (repeaterPaths.includes(pathStripGuid)) {
137-
return {
138-
pathIncludingGuid: pathWithoutBase,
139-
pathExcludingGuid: pathWithoutBase.substring(
140-
0,
141-
pathWithoutBase.length - GUID_LENGTH - 1
142-
)
143-
}
144-
}
145-
}
128+
if (typeof originalPath !== 'string') {
129+
return undefined
130+
}
131+
132+
const segments = originalPath.split('/')
133+
const lastSegment = segments.at(-1) ?? ''
134+
135+
if (!isValidUUID(lastSegment)) {
136+
return undefined
137+
}
138+
139+
const baseOffset = model.basePath.length + 1
140+
const pathIncludingGuid = originalPath.substring(baseOffset)
141+
142+
const guidStartIndex = originalPath.length - GUID_LENGTH
143+
const originalPathWithoutGuid = originalPath.substring(0, guidStartIndex)
144+
145+
if (!repeaterPaths.includes(originalPathWithoutGuid)) {
146+
return undefined
146147
}
147148

148-
return undefined
149+
const pathExcludingGuid = pathIncludingGuid.substring(
150+
0,
151+
pathIncludingGuid.length - GUID_LENGTH - 1
152+
)
153+
154+
return {
155+
pathIncludingGuid,
156+
pathExcludingGuid
157+
}
149158
}
150159

151160
/**

0 commit comments

Comments
 (0)