From 431333dca5e43133ef235adc06792f9dc9154f51 Mon Sep 17 00:00:00 2001 From: Jez Barnsley Date: Mon, 12 Jan 2026 09:29:07 +0000 Subject: [PATCH 1/2] Fixed restoration of state from save and exit --- .../engine/components/DeclarationField.test.ts | 10 ++++++++++ .../plugins/engine/components/DeclarationField.ts | 12 ++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/server/plugins/engine/components/DeclarationField.test.ts b/src/server/plugins/engine/components/DeclarationField.test.ts index 5e46697b2..7b1df3927 100644 --- a/src/server/plugins/engine/components/DeclarationField.test.ts +++ b/src/server/plugins/engine/components/DeclarationField.test.ts @@ -392,6 +392,16 @@ describe('DeclarationField', () => { expect(field.getFormValue({})).toBeUndefined() }) }) + + describe('isDeclarationChecked', () => { + test('should return correct value', () => { + expect(DeclarationField.isDeclarationChecked(undefined)).toBe(false) + expect(DeclarationField.isDeclarationChecked(['true'])).toBe(true) + expect(DeclarationField.isDeclarationChecked([])).toBe(false) + expect(DeclarationField.isDeclarationChecked('true')).toBe(true) + expect(DeclarationField.isDeclarationChecked(true)).toBe(true) + }) + }) }) describe('Validation', () => { diff --git a/src/server/plugins/engine/components/DeclarationField.ts b/src/server/plugins/engine/components/DeclarationField.ts index 870466792..126e067b6 100644 --- a/src/server/plugins/engine/components/DeclarationField.ts +++ b/src/server/plugins/engine/components/DeclarationField.ts @@ -137,8 +137,8 @@ export class DeclarationField extends FormComponent { } } - const isChecked = - payload[this.name] === 'true' || payload[this.name] === true + const payloadValue = payload[this.name] + const isChecked = DeclarationField.isDeclarationChecked(payloadValue) return { ...viewModel, hint: hint ? { text: hint } : undefined, @@ -190,4 +190,12 @@ export class DeclarationField extends FormComponent { static isBool(value?: FormStateValue | FormState): value is boolean { return isFormValue(value) && typeof value === 'boolean' } + + static isDeclarationChecked(payloadValue: FormValue) { + return ( + payloadValue === 'true' || + payloadValue === true || + (Array.isArray(payloadValue) && payloadValue.some((x) => x === 'true')) + ) + } } From 12919ad509620b6312078f2c7cddb845916c1762 Mon Sep 17 00:00:00 2001 From: Jez Barnsley Date: Mon, 12 Jan 2026 09:53:29 +0000 Subject: [PATCH 2/2] Restructured --- .../components/DeclarationField.test.ts | 62 ++++++++++++++++--- .../engine/components/DeclarationField.ts | 14 ++--- 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/src/server/plugins/engine/components/DeclarationField.test.ts b/src/server/plugins/engine/components/DeclarationField.test.ts index 7b1df3927..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, @@ -392,16 +444,6 @@ describe('DeclarationField', () => { expect(field.getFormValue({})).toBeUndefined() }) }) - - describe('isDeclarationChecked', () => { - test('should return correct value', () => { - expect(DeclarationField.isDeclarationChecked(undefined)).toBe(false) - expect(DeclarationField.isDeclarationChecked(['true'])).toBe(true) - expect(DeclarationField.isDeclarationChecked([])).toBe(false) - expect(DeclarationField.isDeclarationChecked('true')).toBe(true) - expect(DeclarationField.isDeclarationChecked(true)).toBe(true) - }) - }) }) describe('Validation', () => { diff --git a/src/server/plugins/engine/components/DeclarationField.ts b/src/server/plugins/engine/components/DeclarationField.ts index 126e067b6..dcaa7a1cb 100644 --- a/src/server/plugins/engine/components/DeclarationField.ts +++ b/src/server/plugins/engine/components/DeclarationField.ts @@ -138,7 +138,11 @@ export class DeclarationField extends FormComponent { } const payloadValue = payload[this.name] - const isChecked = DeclarationField.isDeclarationChecked(payloadValue) + const isChecked = + payloadValue === 'true' || + payloadValue === true || + (Array.isArray(payloadValue) && payloadValue.some((x) => x === 'true')) + return { ...viewModel, hint: hint ? { text: hint } : undefined, @@ -190,12 +194,4 @@ export class DeclarationField extends FormComponent { static isBool(value?: FormStateValue | FormState): value is boolean { return isFormValue(value) && typeof value === 'boolean' } - - static isDeclarationChecked(payloadValue: FormValue) { - return ( - payloadValue === 'true' || - payloadValue === true || - (Array.isArray(payloadValue) && payloadValue.some((x) => x === 'true')) - ) - } }