|
| 1 | +import { ComponentType, type HiddenFieldComponent } from '@defra/forms-model' |
| 2 | + |
| 3 | +import { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js' |
| 4 | +import { |
| 5 | + getAnswer, |
| 6 | + type Field |
| 7 | +} from '~/src/server/plugins/engine/components/helpers/components.js' |
| 8 | +import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js' |
| 9 | +import definition from '~/test/form/definitions/blank.js' |
| 10 | +import { getFormData, getFormState } from '~/test/helpers/component-helpers.js' |
| 11 | + |
| 12 | +describe('HiddenField', () => { |
| 13 | + let model: FormModel |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + model = new FormModel(definition, { |
| 17 | + basePath: 'test' |
| 18 | + }) |
| 19 | + }) |
| 20 | + |
| 21 | + describe('Defaults', () => { |
| 22 | + let def: HiddenFieldComponent |
| 23 | + let collection: ComponentCollection |
| 24 | + let field: Field |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + def = { |
| 28 | + title: 'Hidden field', |
| 29 | + name: 'myComponent', |
| 30 | + type: ComponentType.HiddenField, |
| 31 | + options: {} |
| 32 | + } satisfies HiddenFieldComponent |
| 33 | + |
| 34 | + collection = new ComponentCollection([def], { model }) |
| 35 | + field = collection.fields[0] |
| 36 | + }) |
| 37 | + |
| 38 | + describe('Schema', () => { |
| 39 | + it('uses component title as label as default', () => { |
| 40 | + const { formSchema } = collection |
| 41 | + const { keys } = formSchema.describe() |
| 42 | + |
| 43 | + expect(keys).toHaveProperty( |
| 44 | + 'myComponent', |
| 45 | + expect.objectContaining({ |
| 46 | + flags: expect.objectContaining({ |
| 47 | + label: 'Hidden field' |
| 48 | + }) |
| 49 | + }) |
| 50 | + ) |
| 51 | + }) |
| 52 | + |
| 53 | + it('uses component name as keys', () => { |
| 54 | + const { formSchema } = collection |
| 55 | + const { keys } = formSchema.describe() |
| 56 | + |
| 57 | + expect(field.keys).toEqual(['myComponent']) |
| 58 | + expect(field.collection).toBeUndefined() |
| 59 | + |
| 60 | + for (const key of field.keys) { |
| 61 | + expect(keys).toHaveProperty(key) |
| 62 | + } |
| 63 | + }) |
| 64 | + |
| 65 | + it('is required by default', () => { |
| 66 | + const { formSchema } = collection |
| 67 | + const { keys } = formSchema.describe() |
| 68 | + |
| 69 | + expect(keys).toHaveProperty( |
| 70 | + 'myComponent', |
| 71 | + expect.objectContaining({ |
| 72 | + flags: expect.objectContaining({ |
| 73 | + presence: 'required' |
| 74 | + }) |
| 75 | + }) |
| 76 | + ) |
| 77 | + }) |
| 78 | + it('accepts valid values', () => { |
| 79 | + const result1 = collection.validate(getFormData('Hidden value')) |
| 80 | + const result2 = collection.validate(getFormData('Hidden value 2')) |
| 81 | + |
| 82 | + expect(result1.errors).toBeUndefined() |
| 83 | + expect(result2.errors).toBeUndefined() |
| 84 | + }) |
| 85 | + |
| 86 | + it('adds errors for empty value', () => { |
| 87 | + const result = collection.validate(getFormData('')) |
| 88 | + |
| 89 | + expect(result.errors).toEqual([ |
| 90 | + expect.objectContaining({ |
| 91 | + text: 'Enter hidden field' |
| 92 | + }) |
| 93 | + ]) |
| 94 | + }) |
| 95 | + |
| 96 | + it('adds errors for invalid values', () => { |
| 97 | + const result1 = collection.validate(getFormData(['invalid'])) |
| 98 | + const result2 = collection.validate( |
| 99 | + // @ts-expect-error - Allow invalid param for test |
| 100 | + getFormData({ unknown: 'invalid' }) |
| 101 | + ) |
| 102 | + |
| 103 | + expect(result1.errors).toBeTruthy() |
| 104 | + expect(result2.errors).toBeTruthy() |
| 105 | + }) |
| 106 | + }) |
| 107 | + |
| 108 | + describe('State', () => { |
| 109 | + it('returns text from state', () => { |
| 110 | + const state1 = getFormState('Hidden field') |
| 111 | + const state2 = getFormState(null) |
| 112 | + |
| 113 | + const answer1 = getAnswer(field, state1) |
| 114 | + const answer2 = getAnswer(field, state2) |
| 115 | + |
| 116 | + expect(answer1).toBe('Hidden field') |
| 117 | + expect(answer2).toBe('') |
| 118 | + }) |
| 119 | + |
| 120 | + it('returns payload from state', () => { |
| 121 | + const state1 = getFormState('Hidden field') |
| 122 | + const state2 = getFormState(null) |
| 123 | + |
| 124 | + const payload1 = field.getFormDataFromState(state1) |
| 125 | + const payload2 = field.getFormDataFromState(state2) |
| 126 | + |
| 127 | + expect(payload1).toEqual(getFormData('Hidden field')) |
| 128 | + expect(payload2).toEqual(getFormData()) |
| 129 | + }) |
| 130 | + |
| 131 | + it('returns value from state', () => { |
| 132 | + const state1 = getFormState('Hidden field') |
| 133 | + const state2 = getFormState(null) |
| 134 | + |
| 135 | + const value1 = field.getFormValueFromState(state1) |
| 136 | + const value2 = field.getFormValueFromState(state2) |
| 137 | + |
| 138 | + expect(value1).toBe('Hidden field') |
| 139 | + expect(value2).toBeUndefined() |
| 140 | + }) |
| 141 | + |
| 142 | + it('returns context for conditions and form submission', () => { |
| 143 | + const state1 = getFormState('Hidden field') |
| 144 | + const state2 = getFormState(null) |
| 145 | + |
| 146 | + const value1 = field.getContextValueFromState(state1) |
| 147 | + const value2 = field.getContextValueFromState(state2) |
| 148 | + |
| 149 | + expect(value1).toBe('Hidden field') |
| 150 | + expect(value2).toBeNull() |
| 151 | + }) |
| 152 | + |
| 153 | + it('returns state from payload', () => { |
| 154 | + const payload1 = getFormData('Hidden field') |
| 155 | + const payload2 = getFormData() |
| 156 | + |
| 157 | + const value1 = field.getStateFromValidForm(payload1) |
| 158 | + const value2 = field.getStateFromValidForm(payload2) |
| 159 | + |
| 160 | + expect(value1).toEqual(getFormState('Hidden field')) |
| 161 | + expect(value2).toEqual(getFormState(null)) |
| 162 | + }) |
| 163 | + }) |
| 164 | + |
| 165 | + describe('View model', () => { |
| 166 | + it('sets Nunjucks component defaults', () => { |
| 167 | + const viewModel = field.getViewModel(getFormData('Hidden field')) |
| 168 | + |
| 169 | + expect(viewModel).toEqual( |
| 170 | + expect.objectContaining({ |
| 171 | + label: { text: def.title }, |
| 172 | + name: 'myComponent', |
| 173 | + id: 'myComponent', |
| 174 | + value: 'Hidden field' |
| 175 | + }) |
| 176 | + ) |
| 177 | + }) |
| 178 | + }) |
| 179 | + |
| 180 | + describe('AllPossibleErrors', () => { |
| 181 | + it('should return errors', () => { |
| 182 | + const errors = field.getAllPossibleErrors() |
| 183 | + expect(errors.baseErrors).not.toBeEmpty() |
| 184 | + expect(errors.advancedSettingsErrors).toBeEmpty() |
| 185 | + }) |
| 186 | + }) |
| 187 | + }) |
| 188 | +}) |
0 commit comments