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
52 changes: 52 additions & 0 deletions src/server/plugins/engine/components/DeclarationField.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,58 @@ describe('DeclarationField', () => {
)
})

it('sets Nunjucks component to true when checked from save-anbd-exit', () => {
def = {
...def,
hint: 'Please read and confirm the following'
} satisfies DeclarationFieldComponent

collection = new ComponentCollection([def], { model })
field = collection.fields[0]
const viewModel = field.getViewModel(getFormData(['true', 'unchecked']))

expect(viewModel).toEqual(
expect.objectContaining({
hint: {
text: 'Please read and confirm the following'
},
items: [
{
value: 'true',
text: 'I understand and agree',
checked: true
}
]
})
)
})

it('sets Nunjucks component to false when unchecked from save-anbd-exit', () => {
def = {
...def,
hint: 'Please read and confirm the following'
} satisfies DeclarationFieldComponent

collection = new ComponentCollection([def], { model })
field = collection.fields[0]
const viewModel = field.getViewModel(getFormData(['unchecked']))

expect(viewModel).toEqual(
expect.objectContaining({
hint: {
text: 'Please read and confirm the following'
},
items: [
{
value: 'true',
text: 'I understand and agree',
checked: false
}
]
})
)
})

it('sets Nunjucks component value when posted', () => {
def = {
...def,
Expand Down
6 changes: 5 additions & 1 deletion src/server/plugins/engine/components/DeclarationField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ export class DeclarationField extends FormComponent {
}
}

const payloadValue = payload[this.name]
const isChecked =
payload[this.name] === 'true' || payload[this.name] === true
payloadValue === 'true' ||
payloadValue === true ||
(Array.isArray(payloadValue) && payloadValue.some((x) => x === 'true'))

return {
...viewModel,
hint: hint ? { text: hint } : undefined,
Expand Down
Loading