1- import { ComponentType , type Page } from '@defra/forms-model'
1+ import { ComponentType , ControllerType , type Page } from '@defra/forms-model'
22
33import { type FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
44import { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers/pages.js'
55import {
6+ checkSaveAndExitRepeater ,
67 copyNotYetValidatedState ,
78 prefillStateFromQueryParameters ,
89 stripParam
910} from '~/src/server/plugins/engine/pageControllers/helpers/state.js'
1011import {
1112 type AnyFormRequest ,
12- type FormContext ,
13- type FormContextRequest
13+ type FormContext
1414} from '~/src/server/plugins/engine/types.js'
1515import { type FormsService , type Services } from '~/src/server/types.js'
1616
17+ const mockGetCacheService = jest . fn ( )
18+ const mockCacheService = { setState : jest . fn ( ) }
19+
20+ jest . mock ( '~/src/server/plugins/engine/helpers.ts' , ( ) => ( {
21+ __esModule : true ,
22+ getCacheService : ( ...args : unknown [ ] ) => mockGetCacheService ( ...args )
23+ } ) )
24+
1725function buildMockPage (
1826 pagesOverride = { } ,
1927 stateOverride = { } ,
@@ -225,23 +233,26 @@ describe('State helpers', () => {
225233 } )
226234
227235 describe ( 'copyNotYetValidatedState' , ( ) => {
228- it ( 'should ignore if no invalid state' , ( ) => {
229- const mockRequest = { } as FormContextRequest
236+ beforeEach ( ( ) => {
237+ mockGetCacheService . mockReturnValue ( mockCacheService )
238+ } )
239+ it ( 'should ignore if no invalid state' , async ( ) => {
240+ const mockRequest = { } as AnyFormRequest
230241 const mockContext = {
231242 state : { abc : '123' } ,
232243 payload : { }
233244 } as unknown as FormContext
234- copyNotYetValidatedState ( mockRequest , mockContext )
245+ await copyNotYetValidatedState ( mockRequest , mockContext )
235246 expect ( mockContext . state ) . toEqual ( { abc : '123' } )
236247 expect ( mockContext . payload ) . toEqual ( { } )
237248 } )
238249
239- it ( 'should ignore if wrong path' , ( ) => {
250+ it ( 'should ignore if wrong path' , async ( ) => {
240251 const mockRequest = {
241252 url : {
242253 pathname : '/form-page1'
243254 }
244- } as unknown as FormContextRequest
255+ } as unknown as AnyFormRequest
245256 const mockContext = {
246257 state : {
247258 abc : '123' ,
@@ -252,7 +263,7 @@ describe('State helpers', () => {
252263 } ,
253264 payload : { }
254265 } as unknown as FormContext
255- copyNotYetValidatedState ( mockRequest , mockContext )
266+ await copyNotYetValidatedState ( mockRequest , mockContext )
256267 expect ( mockContext . state ) . toEqual ( {
257268 abc : '123' ,
258269 __stateNotYetValidated : {
@@ -263,12 +274,12 @@ describe('State helpers', () => {
263274 expect ( mockContext . payload ) . toEqual ( { } )
264275 } )
265276
266- it ( 'should apply if correct path' , ( ) => {
277+ it ( 'should apply if correct path' , async ( ) => {
267278 const mockRequest = {
268279 url : {
269280 pathname : '/form-page1'
270281 }
271- } as unknown as FormContextRequest
282+ } as unknown as AnyFormRequest
272283 const mockContext = {
273284 state : {
274285 abc : '123' ,
@@ -279,17 +290,70 @@ describe('State helpers', () => {
279290 } ,
280291 payload : { }
281292 } as unknown as FormContext
282- copyNotYetValidatedState ( mockRequest , mockContext )
293+ await copyNotYetValidatedState ( mockRequest , mockContext )
283294 expect ( mockContext . state ) . toEqual ( {
284295 abc : '123' ,
285- __stateNotYetValidated : {
286- def : '456' ,
287- __currentPagePath : '/form-page1'
288- }
296+ __stateNotYetValidated : undefined
289297 } )
290298 expect ( mockContext . payload ) . toEqual ( {
291299 def : '456'
292300 } )
293301 } )
294302 } )
303+
304+ describe ( 'checkSaveAndExitRepeater' , ( ) => {
305+ function createMockContextWithPath ( path : string ) {
306+ return {
307+ state : {
308+ abc : '123' ,
309+ __stateNotYetValidated : {
310+ def : '456' ,
311+ __currentPagePath : path
312+ }
313+ } ,
314+ payload : { }
315+ } as unknown as FormContext
316+ }
317+
318+ const mockModel = {
319+ def : {
320+ pages : [
321+ {
322+ controller : ControllerType . Repeat ,
323+ path : '/personal_details'
324+ }
325+ ]
326+ } ,
327+ basePath : 'form/preview/draft/repeater-test'
328+ } as unknown as FormModel
329+
330+ it ( 'should return undefined if url does not end in a guid' , ( ) => {
331+ const mockContext = createMockContextWithPath (
332+ '/form/preview/draft/repeater-test/personal_details'
333+ )
334+ expect ( checkSaveAndExitRepeater ( mockContext , mockModel ) ) . toBeUndefined ( )
335+ } )
336+
337+ it ( 'should return undefined if url ends in a guid but not a repeater path' , ( ) => {
338+ const mockContext = createMockContextWithPath (
339+ '/form/preview/draft/repeater-test/wrong_page/7d27fe6e-73e8-4265-84bd-1e118c92470b'
340+ )
341+ expect ( checkSaveAndExitRepeater ( mockContext , mockModel ) ) . toBeUndefined ( )
342+ } )
343+
344+ it ( 'should return undefined if url is not a string' , ( ) => {
345+ // @ts -expect-error - invalid dataype on purpose for this test
346+ const mockContext = createMockContextWithPath ( { } )
347+ expect ( checkSaveAndExitRepeater ( mockContext , mockModel ) ) . toBeUndefined ( )
348+ } )
349+
350+ it ( 'should return correct urls if url ends in a guid and is a repeater path' , ( ) => {
351+ const mockContext = createMockContextWithPath (
352+ '/form/preview/draft/repeater-test/personal_details/7d27fe6e-73e8-4265-84bd-1e118c92470b'
353+ )
354+ expect ( checkSaveAndExitRepeater ( mockContext , mockModel ) ) . toBe (
355+ '/form/preview/draft/repeater-test/personal_details/7d27fe6e-73e8-4265-84bd-1e118c92470b'
356+ )
357+ } )
358+ } )
295359} )
0 commit comments