1+ import {
2+ ComponentType ,
3+ type FormDefinition ,
4+ type PageQuestion
5+ } from '@defra/forms-model'
16import Boom from '@hapi/boom'
27import { type ResponseObject , type ResponseToolkit } from '@hapi/hapi'
38import { StatusCodes } from 'http-status-codes'
@@ -14,6 +19,7 @@ import {
1419 getPageHref ,
1520 proceed ,
1621 safeGenerateCrumb ,
22+ setPageTitles ,
1723 type GlobalScope
1824} from '~/src/server/plugins/engine/helpers.js'
1925import { handleLegacyRedirect } from '~/src/server/plugins/engine/helpers.js'
@@ -51,6 +57,7 @@ describe('Helpers', () => {
5157 let h : FormResponseToolkit
5258
5359 beforeEach ( ( ) => {
60+ jest . clearAllMocks ( )
5461 const model = new FormModel ( definition , {
5562 basePath : 'test'
5663 } )
@@ -843,4 +850,53 @@ describe('Helpers', () => {
843850 expect ( response ) . toBe ( mockRedirectResponse )
844851 } )
845852 } )
853+
854+ describe ( 'setPageTitles' , ( ) => {
855+ const definition : FormDefinition = {
856+ name : 'Test Form' ,
857+ startPage : '/page1' ,
858+ pages : [
859+ {
860+ path : '/page1' ,
861+ title : '' ,
862+ next : [ ] ,
863+ components : [
864+ {
865+ type : ComponentType . TextField ,
866+ name : 'textfield1' ,
867+ title : 'What is your name?' ,
868+ options : { } ,
869+ schema : { }
870+ } ,
871+ {
872+ type : ComponentType . TextField ,
873+ name : 'textfield2' ,
874+ title : 'What is your favourite food?' ,
875+ options : { } ,
876+ schema : { }
877+ }
878+ ]
879+ } satisfies PageQuestion
880+ ] ,
881+ lists : [ ] ,
882+ sections : [ ] ,
883+ conditions : [ ]
884+ }
885+
886+ beforeEach ( ( ) => {
887+ jest . clearAllMocks ( )
888+ } )
889+ it ( 'should set title if missing' , ( ) => {
890+ const def = structuredClone ( definition )
891+ setPageTitles ( def )
892+ expect ( def . pages [ 0 ] . title ) . toBe ( 'What is your name?' )
893+ } )
894+
895+ it ( 'should keep title if supplied' , ( ) => {
896+ const def = structuredClone ( definition )
897+ def . pages [ 0 ] . title = 'Page 1 title'
898+ setPageTitles ( def )
899+ expect ( def . pages [ 0 ] . title ) . toBe ( 'Page 1 title' )
900+ } )
901+ } )
846902} )
0 commit comments