|
1 | | -import { ComponentType, type Page } from '@defra/forms-model' |
| 1 | +import { ComponentType, ControllerType, type Page } from '@defra/forms-model' |
2 | 2 |
|
3 | 3 | import { type FormModel } from '~/src/server/plugins/engine/models/FormModel.js' |
4 | 4 | import { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers/pages.js' |
5 | 5 | import { |
| 6 | + checkSaveAndExitRepeater, |
6 | 7 | copyNotYetValidatedState, |
7 | 8 | prefillStateFromQueryParameters, |
8 | 9 | stripParam |
@@ -292,4 +293,56 @@ describe('State helpers', () => { |
292 | 293 | }) |
293 | 294 | }) |
294 | 295 | }) |
| 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 | + }) |
295 | 348 | }) |
0 commit comments