Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
},
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@defra/forms-model": "^3.0.585",
"@defra/forms-model": "^3.0.597",
"@defra/hapi-tracing": "^1.29.0",
"@elastic/ecs-pino-format": "^1.5.0",
"@hapi/boom": "^10.0.1",
Expand Down
70 changes: 70 additions & 0 deletions src/server/plugins/engine/models/FormModel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,4 +716,74 @@ describe('FormModel - Joined Conditions', () => {
expect(Object.keys(model.conditions)).toHaveLength(3)
})
})

describe('getSection', () => {
it('should look up section by name for V1 schema', () => {
const v1Definition = {
...definition,
sections: [
{ name: 'personal', title: 'Personal details' },
{ name: 'contact', title: 'Contact details' }
]
}

const model = new FormModel(v1Definition, { basePath: 'test' })

expect(model.getSection('personal')).toEqual(
expect.objectContaining({
name: 'personal',
title: 'Personal details'
})
)
expect(model.getSection('contact')).toEqual(
expect.objectContaining({
name: 'contact',
title: 'Contact details'
})
)
expect(model.getSection('nonexistent')).toBeUndefined()
})

it('should look up section by ID for V2 schema', () => {
const v2Definition = {
...definitionV2,
sections: [
{
id: 'a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d',
name: 'personal',
title: 'Personal details'
},
{
id: 'b2c3d4e5-6f7a-8b9c-0d1e-2f3a4b5c6d7e',
name: 'contact',
title: 'Contact details'
}
]
}

formDefinitionV2Schema.validate = jest
.fn()
.mockReturnValue({ value: v2Definition })

const model = new FormModel(v2Definition, { basePath: 'test' })

expect(model.getSection('a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d')).toEqual(
expect.objectContaining({
id: 'a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d',
name: 'personal',
title: 'Personal details'
})
)
expect(model.getSection('b2c3d4e5-6f7a-8b9c-0d1e-2f3a4b5c6d7e')).toEqual(
expect.objectContaining({
id: 'b2c3d4e5-6f7a-8b9c-0d1e-2f3a4b5c6d7e',
name: 'contact',
title: 'Contact details'
})
)
// V2 should not find by name
expect(model.getSection('personal')).toBeUndefined()
expect(model.getSection('nonexistent')).toBeUndefined()
})
})
})
9 changes: 8 additions & 1 deletion src/server/plugins/engine/models/FormModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {
type Engine,
type FormDefinition,
type List,
type Page
type Page,
type Section
} from '@defra/forms-model'
import { add, format } from 'date-fns'
import { Parser, type Value } from 'expr-eval-fork'
Expand Down Expand Up @@ -321,6 +322,12 @@ export class FormModel {
: this.lists.find((list) => list.id === nameOrId)
}

getSection(nameOrId: string): Section | undefined {
return this.schemaVersion === SchemaVersion.V1
? this.sections.find((section) => section.name === nameOrId)
: this.sections.find((section) => section.id === nameOrId)
}

/**
* Form context for the current page
*/
Expand Down
6 changes: 3 additions & 3 deletions src/server/plugins/engine/pageControllers/PageController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ export class PageController {
this.events = pageDef.events

// Resolve section
this.section = model.sections.find(
(section) => section.name === pageDef.section
)
if (pageDef.section) {
this.section = model.getSection(pageDef.section)
}

// Resolve condition
if (pageDef.condition) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1007,11 +1007,13 @@ describe('QuestionPageController V2', () => {

it('returns the page section', () => {
expect(controller1).toHaveProperty('section', undefined)
expect(controller2).toHaveProperty('section', {
name: 'marriage',
title: 'Your marriage',
hideTitle: false
})
expect(controller2.section).toEqual(
expect.objectContaining({
name: 'marriage',
title: 'Your marriage',
hideTitle: false
})
)
})
})

Expand Down
6 changes: 4 additions & 2 deletions test/form/definitions/conditions-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const V2 = /** @satisfies {FormDefinition} */ ({
}
],
condition: '6c9e2f4a-1d7b-5e8c-3f6a-9e2d5b7c4f1a',
section: 'marriage',
section: 'a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d',
next: []
},
{
Expand All @@ -154,8 +154,10 @@ export const V2 = /** @satisfies {FormDefinition} */ ({
lists: [],
sections: [
{
id: 'a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d',
name: 'marriage',
title: 'Your marriage'
title: 'Your marriage',
hideTitle: false
}
],
conditions: [
Expand Down
Loading