diff --git a/src/server/plugins/engine/components/DeclarationField.test.ts b/src/server/plugins/engine/components/DeclarationField.test.ts index 5e46697b2..c81a4ab79 100644 --- a/src/server/plugins/engine/components/DeclarationField.test.ts +++ b/src/server/plugins/engine/components/DeclarationField.test.ts @@ -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, diff --git a/src/server/plugins/engine/components/DeclarationField.ts b/src/server/plugins/engine/components/DeclarationField.ts index 870466792..dcaa7a1cb 100644 --- a/src/server/plugins/engine/components/DeclarationField.ts +++ b/src/server/plugins/engine/components/DeclarationField.ts @@ -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,