diff --git a/package-lock.json b/package-lock.json index a4ed873e6..70a5863b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "hasInstallScript": true, "license": "SEE LICENSE IN LICENSE", "dependencies": { - "@defra/forms-model": "^3.0.432", + "@defra/forms-model": "^3.0.438", "@defra/hapi-tracing": "^1.0.0", "@elastic/ecs-pino-format": "^1.5.0", "@hapi/boom": "^10.0.1", @@ -2038,9 +2038,9 @@ } }, "node_modules/@defra/forms-model": { - "version": "3.0.432", - "resolved": "https://registry.npmjs.org/@defra/forms-model/-/forms-model-3.0.432.tgz", - "integrity": "sha512-EEWWStK/tAweis08ewj68R4hHzRLZckvbF3wg6UgVXSnO/UVjtrfGxJ0et+2ykuEWdGISkVVWXvYXSrinl6JlQ==", + "version": "3.0.438", + "resolved": "https://registry.npmjs.org/@defra/forms-model/-/forms-model-3.0.438.tgz", + "integrity": "sha512-3Vn6ZeTy/Oajx5t5vG5/xTn0UPnas0fdtTYOYTfNQuco12RJRtmLeXaOcHyzJqZpxvAmqWL0AKKEUv4iSSMoZg==", "license": "OGL-UK-3.0", "dependencies": { "marked": "^15.0.7", diff --git a/package.json b/package.json index 4b7164828..731102be5 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ }, "license": "SEE LICENSE IN LICENSE", "dependencies": { - "@defra/forms-model": "^3.0.432", + "@defra/forms-model": "^3.0.438", "@defra/hapi-tracing": "^1.0.0", "@elastic/ecs-pino-format": "^1.5.0", "@hapi/boom": "^10.0.1", diff --git a/src/server/plugins/engine/components/AutocompleteField.test.ts b/src/server/plugins/engine/components/AutocompleteField.test.ts index ac45b2fb0..eebb4a3dd 100644 --- a/src/server/plugins/engine/components/AutocompleteField.test.ts +++ b/src/server/plugins/engine/components/AutocompleteField.test.ts @@ -172,7 +172,7 @@ describe.each([ describe('State', () => { it.each([...options.examples])('returns text from state', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const answer1 = getAnswer(field, state1) @@ -183,7 +183,7 @@ describe.each([ }) it.each([...options.examples])('returns payload from state', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const payload1 = field.getFormDataFromState(state1) @@ -194,7 +194,7 @@ describe.each([ }) it.each([...options.examples])('returns value from state', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const value1 = field.getFormValueFromState(state1) @@ -207,7 +207,7 @@ describe.each([ it.each([...options.examples])( 'returns context for conditions and form submission', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const value1 = field.getContextValueFromState(state1) @@ -225,7 +225,7 @@ describe.each([ const value1 = field.getStateFromValidForm(payload1) const value2 = field.getStateFromValidForm(payload2) - expect(value1).toEqual(getFormState(item.state)) + expect(value1).toEqual(getFormState(item.value)) expect(value2).toEqual(getFormState(null)) }) }) diff --git a/src/server/plugins/engine/components/CheckboxesField.test.ts b/src/server/plugins/engine/components/CheckboxesField.test.ts index b34e7f5f7..9c33708cf 100644 --- a/src/server/plugins/engine/components/CheckboxesField.test.ts +++ b/src/server/plugins/engine/components/CheckboxesField.test.ts @@ -240,7 +240,7 @@ describe.each([ it.each([...options.examples])( 'returns text from state (single)', (item) => { - const state1 = getFormState([item.state]) + const state1 = getFormState([item.value]) const state2 = getFormState(null) const answer1 = getAnswer(field, state1) @@ -260,7 +260,7 @@ describe.each([ const item1 = options.examples[0] const item2 = options.examples[2] - const state = getFormState([item1.state, item2.state]) + const state = getFormState([item1.value, item2.value]) const answer = getAnswer(field, state) expect(answer).toBe(outdent` @@ -272,7 +272,7 @@ describe.each([ }) it.each([...options.examples])('returns payload from state', (item) => { - const state1 = getFormState([item.state]) + const state1 = getFormState([item.value]) const state2 = getFormState(null) const payload1 = field.getFormDataFromState(state1) @@ -283,7 +283,7 @@ describe.each([ }) it.each([...options.examples])('returns value from state', (item) => { - const state1 = getFormState([item.state]) + const state1 = getFormState([item.value]) const state2 = getFormState(null) const value1 = field.getFormValueFromState(state1) @@ -296,13 +296,13 @@ describe.each([ it.each([...options.examples])( 'returns context for conditions and form submission', (item) => { - const state1 = getFormState([item.state]) + const state1 = getFormState([item.value]) const state2 = getFormState(null) const value1 = field.getContextValueFromState(state1) const value2 = field.getContextValueFromState(state2) - expect(value1).toEqual([item.state]) + expect(value1).toEqual([item.value]) expect(value2).toEqual([]) } ) @@ -314,7 +314,7 @@ describe.each([ const value1 = field.getStateFromValidForm(payload1) const value2 = field.getStateFromValidForm(payload2) - expect(value1).toEqual(getFormState([item.state])) + expect(value1).toEqual(getFormState([item.value])) expect(value2).toEqual(getFormState(null)) }) }) diff --git a/src/server/plugins/engine/components/List.test.ts b/src/server/plugins/engine/components/List.test.ts index 85fd6ec93..dd188ceaa 100644 --- a/src/server/plugins/engine/components/List.test.ts +++ b/src/server/plugins/engine/components/List.test.ts @@ -52,18 +52,21 @@ describe('List', () => { it('returns list items', () => { expect(guidance).toHaveProperty('items', [ { + id: '52fc51fc-c75a-4b08-9c9e-6bd99b9bc49b', text: '1 day', value: 1, description: 'Valid for 24 hours from the start time that you select' }, { + id: '56b7b34f-23b3-4446-ac8e-b2443d18588e', text: '8 day', value: 8, description: 'Valid for 8 consecutive days from the start time that you select' }, { + id: '1af54fbc-eec2-4e1e-bd53-2415abf62677', text: '12 months', value: 365, description: diff --git a/src/server/plugins/engine/components/RadiosField.test.ts b/src/server/plugins/engine/components/RadiosField.test.ts index fbf2724d2..f0bb586c0 100644 --- a/src/server/plugins/engine/components/RadiosField.test.ts +++ b/src/server/plugins/engine/components/RadiosField.test.ts @@ -170,7 +170,7 @@ describe.each([ describe('State', () => { it.each([...options.examples])('returns text from state', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const answer1 = getAnswer(field, state1) @@ -181,7 +181,7 @@ describe.each([ }) it.each([...options.examples])('returns payload from state', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const payload1 = field.getFormDataFromState(state1) @@ -192,7 +192,7 @@ describe.each([ }) it.each([...options.examples])('returns value from state', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const value1 = field.getFormValueFromState(state1) @@ -205,7 +205,7 @@ describe.each([ it.each([...options.examples])( 'returns context for conditions and form submission', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const value1 = field.getContextValueFromState(state1) @@ -223,7 +223,7 @@ describe.each([ const value1 = field.getStateFromValidForm(payload1) const value2 = field.getStateFromValidForm(payload2) - expect(value1).toEqual(getFormState(item.state)) + expect(value1).toEqual(getFormState(item.value)) expect(value2).toEqual(getFormState(null)) }) }) diff --git a/src/server/plugins/engine/components/SelectField.test.ts b/src/server/plugins/engine/components/SelectField.test.ts index 903eb8c64..c6a2cda9b 100644 --- a/src/server/plugins/engine/components/SelectField.test.ts +++ b/src/server/plugins/engine/components/SelectField.test.ts @@ -171,7 +171,7 @@ describe.each([ describe('State', () => { it.each([...options.examples])('returns text from state', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const answer1 = getAnswer(field, state1) @@ -182,7 +182,7 @@ describe.each([ }) it.each([...options.examples])('returns payload from state', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const payload1 = field.getFormDataFromState(state1) @@ -193,7 +193,7 @@ describe.each([ }) it.each([...options.examples])('returns value from state', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const value1 = field.getFormValueFromState(state1) @@ -206,7 +206,7 @@ describe.each([ it.each([...options.examples])( 'returns context for conditions and form submission', (item) => { - const state1 = getFormState(item.state) + const state1 = getFormState(item.value) const state2 = getFormState(null) const value1 = field.getContextValueFromState(state1) @@ -224,7 +224,7 @@ describe.each([ const value1 = field.getStateFromValidForm(payload1) const value2 = field.getStateFromValidForm(payload2) - expect(value1).toEqual(getFormState(item.state)) + expect(value1).toEqual(getFormState(item.value)) expect(value2).toEqual(getFormState(null)) }) }) diff --git a/src/server/plugins/engine/pageControllers/QuestionPageController.test.ts b/src/server/plugins/engine/pageControllers/QuestionPageController.test.ts index b0700587e..01bebf312 100644 --- a/src/server/plugins/engine/pageControllers/QuestionPageController.test.ts +++ b/src/server/plugins/engine/pageControllers/QuestionPageController.test.ts @@ -443,6 +443,7 @@ describe('QuestionPageController', () => { expect(filtered[1].model.label?.text).toBe('Select from the list') expect(filtered[1].model.items).toEqual([ { + id: expect.any(String), checked: false, condition: 'isBarnOwl', selected: false, @@ -450,6 +451,7 @@ describe('QuestionPageController', () => { value: '1' }, { + id: expect.any(String), checked: false, condition: 'isBarnOwl', selected: false, @@ -499,6 +501,7 @@ describe('QuestionPageController', () => { expect(filtered[1].model.label?.text).toBe('Select from the list') expect(filtered[1].model.items).toEqual([ { + id: expect.any(String), checked: false, condition: 'notBarnOwl', selected: false, @@ -506,6 +509,7 @@ describe('QuestionPageController', () => { value: '3' }, { + id: expect.any(String), checked: false, condition: 'notBarnOwl', selected: false, diff --git a/test/condition/checkboxes.test.js b/test/condition/checkboxes.test.js index 42228c537..dfcbf4eda 100644 --- a/test/condition/checkboxes.test.js +++ b/test/condition/checkboxes.test.js @@ -70,7 +70,7 @@ describe('Checkboxes based conditions', () => { }) expect($checkbox).toBeInTheDocument() - expect($checkbox).toHaveAttribute('id', example.id) + expect($checkbox).toHaveAttribute('id', expect.any(String)) // id is now a uuid expect($checkbox).toHaveAttribute('name', example.name) expect($checkbox).toHaveAttribute('value', example.value) expect($checkbox).toHaveClass('govuk-checkboxes__input') diff --git a/test/condition/radios.test.js b/test/condition/radios.test.js index 98791f723..3730aecd9 100644 --- a/test/condition/radios.test.js +++ b/test/condition/radios.test.js @@ -70,7 +70,7 @@ describe('Radio based conditions', () => { }) expect($radio).toBeInTheDocument() - expect($radio).toHaveAttribute('id', example.id) + expect($radio).toHaveAttribute('id', expect.any(String)) // is now a uuid expect($radio).toHaveAttribute('name', example.name) expect($radio).toHaveAttribute('value', example.value) expect($radio).toHaveClass('govuk-radios__input') diff --git a/test/fixtures/list.js b/test/fixtures/list.js index 57ccf98f8..65985bd5c 100644 --- a/test/fixtures/list.js +++ b/test/fixtures/list.js @@ -1,90 +1,85 @@ +import { + createListFromFactory, + createListItemFactory +} from '~/test/form/factory.js' + /** * @satisfies {List} */ -export const listNumber = { +export const listNumber = createListFromFactory({ title: 'Example number list', name: 'listNumber', type: 'number', items: [ - { text: '1 point', value: 1 }, - { text: '2 points', value: 2 }, - { text: '3 points', value: 3 }, - { text: '4 points', value: 4 } + createListItemFactory({ text: '1 point', value: 1 }), + createListItemFactory({ text: '2 points', value: 2 }), + createListItemFactory({ text: '3 points', value: 3 }), + createListItemFactory({ text: '4 points', value: 4 }) ] -} +}) export const listNumberExamples = [ - { + createListItemFactory({ text: '1 point', - value: 1, - state: 1 - }, - { + value: 1 + }), + createListItemFactory({ text: '2 points', - value: 2, - state: 2 - }, - { + value: 2 + }), + createListItemFactory({ text: '3 points', - value: 3, - state: 3 - }, - { + value: 3 + }), + createListItemFactory({ text: '4 points', - value: 4, - state: 4 - } + value: 4 + }) ] /** * @satisfies {List} */ -export const listString = { +export const listString = createListFromFactory({ title: 'Example string list', name: 'listString', type: 'string', items: [ - { text: '1 hour', value: '1' }, - { text: '2 hours', value: '2' }, - { text: '3 hours', value: '3' }, - { text: '4 hours', value: '4' } + createListItemFactory({ text: '1 hour', value: '1' }), + createListItemFactory({ text: '2 hours', value: '2' }), + createListItemFactory({ text: '3 hours', value: '3' }), + createListItemFactory({ text: '4 hours', value: '4' }) ] -} +}) export const listStringExamples = [ - { + createListItemFactory({ text: '1 hour', - value: '1', - state: '1' - }, - { + value: '1' + }), + createListItemFactory({ text: '2 hours', - value: '2', - state: '2' - }, - { + value: '2' + }), + createListItemFactory({ text: '3 hours', - value: '3', - state: '3' - }, - { + value: '3' + }), + createListItemFactory({ text: '4 hours', - value: '4', - state: '4' - } + value: '4' + }) ] export const listYesNoExamples = [ - { + createListItemFactory({ text: 'Yes', - value: true, - state: true - }, - { + value: true + }), + createListItemFactory({ text: 'No', - value: false, - state: false - } + value: false + }) ] /** diff --git a/test/form/definitions/basic.js b/test/form/definitions/basic.js index 7488b548b..475c17315 100644 --- a/test/form/definitions/basic.js +++ b/test/form/definitions/basic.js @@ -4,6 +4,11 @@ import { ControllerType } from '@defra/forms-model' +import { + createListFromFactory, + createListItemFactory +} from '~/test/form/factory.js' + export default /** @satisfies {FormDefinition} */ ({ name: 'Basic', startPage: '/licence', @@ -68,30 +73,33 @@ export default /** @satisfies {FormDefinition} */ ({ ], conditions: [], lists: [ - { + createListFromFactory({ name: 'licenceLengthDays', title: 'Licence length (days)', type: 'number', items: [ - { + createListItemFactory({ + id: '52fc51fc-c75a-4b08-9c9e-6bd99b9bc49b', text: '1 day', value: 1, description: 'Valid for 24 hours from the start time that you select' - }, - { + }), + createListItemFactory({ + id: '56b7b34f-23b3-4446-ac8e-b2443d18588e', text: '8 day', value: 8, description: 'Valid for 8 consecutive days from the start time that you select' - }, - { + }), + createListItemFactory({ + id: '1af54fbc-eec2-4e1e-bd53-2415abf62677', text: '12 months', value: 365, description: '12-month licences are now valid for 365 days from their start date and can be purchased at any time during the year' - } + }) ] - } + }) ], outputEmail: 'enrique.chase@defra.gov.uk' }) diff --git a/test/form/factory.js b/test/form/factory.js new file mode 100644 index 000000000..49ba49d1d --- /dev/null +++ b/test/form/factory.js @@ -0,0 +1,34 @@ +/** + * Factory method for list Item + * @param {Partial} partialListItem + * @returns {Item} + */ +export function createListItemFactory(partialListItem) { + return { + id: '52fc51fc-c75a-4b08-9c9e-6bd99b9bc49b', + text: 'text', + value: 1, + description: 'Valid for 24 hours from the start time that you select', + ...partialListItem + } +} + +/** + * Factory method for List + * @param {Partial} partialList + * @returns {List} + */ +export function createListFromFactory(partialList) { + return { + id: '4ebe4ef5-bd3c-499b-a179-7e7e86b0dc6f', + name: 'licenceLengthDays', + title: 'Licence length (days)', + type: 'number', + items: [], + ...partialList + } +} + +/** + * @import { Item, List } from '@defra/forms-model' + */