-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSelectField.test.ts
More file actions
288 lines (239 loc) · 8.11 KB
/
SelectField.test.ts
File metadata and controls
288 lines (239 loc) · 8.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { ComponentType, type SelectFieldComponent } from '@defra/forms-model'
import { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'
import { SelectField } from '~/src/server/plugins/engine/components/SelectField.js'
import {
getAnswer,
type Field
} from '~/src/server/plugins/engine/components/helpers.js'
import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
import {
listNumber,
listNumberExamples,
listString,
listStringExamples
} from '~/test/fixtures/list.js'
import definition from '~/test/form/definitions/blank.js'
import { getFormData, getFormState } from '~/test/helpers/component-helpers.js'
describe.each([
{
component: {
title: 'String list',
name: 'myComponent',
type: ComponentType.SelectField,
list: 'listString',
options: {}
} satisfies SelectFieldComponent,
options: {
list: listString,
examples: listStringExamples,
allow: ['1', '2', '3', '4']
}
},
{
component: {
title: 'Number list',
name: 'myComponent',
type: ComponentType.SelectField,
list: 'listNumber',
options: {}
} satisfies SelectFieldComponent,
options: {
list: listNumber,
examples: listNumberExamples,
allow: [1, 2, 3, 4]
}
}
])('SelectField: $component.title', ({ component: def, options }) => {
const updated = structuredClone(definition)
updated.lists = [options.list]
let model: FormModel
let collection: ComponentCollection
let field: Field
beforeEach(() => {
model = new FormModel(updated, {
basePath: 'test'
})
collection = new ComponentCollection([def], { model })
field = collection.fields[0]
})
describe('Defaults', () => {
describe('Schema', () => {
it('uses component title as label', () => {
const { formSchema } = collection
const { keys } = formSchema.describe()
expect(keys).toHaveProperty(
'myComponent',
expect.objectContaining({
flags: expect.objectContaining({
label: def.title
})
})
)
})
it('uses component name as keys', () => {
const { formSchema } = collection
const { keys } = formSchema.describe()
expect(field.keys).toEqual(['myComponent'])
expect(field.collection).toBeUndefined()
for (const key of field.keys) {
expect(keys).toHaveProperty(key)
}
})
it('is required by default', () => {
const { formSchema } = collection
const { keys } = formSchema.describe()
expect(keys).toHaveProperty(
'myComponent',
expect.objectContaining({
flags: expect.objectContaining({
presence: 'required'
})
})
)
})
it('is optional when configured', () => {
const collectionOptional = new ComponentCollection(
[{ ...def, options: { required: false } }],
{ model }
)
const { formSchema } = collectionOptional
const { keys } = formSchema.describe()
expect(keys).toHaveProperty(
'myComponent',
expect.objectContaining({
allow: expect.arrayContaining([''])
})
)
const result = collectionOptional.validate(getFormData(''))
expect(result.errors).toBeUndefined()
})
it('is configured with select options', () => {
const { formSchema } = collection
const { keys } = formSchema.describe()
expect(keys).toHaveProperty(
'myComponent',
expect.objectContaining({
allow: options.allow,
type: options.list.type
})
)
})
it.each([...options.allow])(
'accepts valid list item (value: %s)',
(value) => {
const result = collection.validate(getFormData(value))
expect(result.errors).toBeUndefined()
}
)
it('adds errors for empty value', () => {
const result = collection.validate(getFormData())
expect(result.errors).toEqual([
expect.objectContaining({
text: `Select ${def.title.toLowerCase()}`
})
])
})
it('adds errors for invalid values', () => {
const result1 = collection.validate(getFormData('invalid'))
const result2 = collection.validate(
// @ts-expect-error - Allow invalid param for test
getFormData({ unknown: 'invalid' })
)
expect(result1.errors).toBeTruthy()
expect(result2.errors).toBeTruthy()
})
})
describe('State', () => {
it.each([...options.examples])('returns text from state', (item) => {
const state1 = getFormState(item.value)
const state2 = getFormState(null)
const answer1 = getAnswer(field, state1)
const answer2 = getAnswer(field, state2)
expect(answer1).toBe(item.text)
expect(answer2).toBe('')
})
it.each([...options.examples])('returns payload from state', (item) => {
const state1 = getFormState(item.value)
const state2 = getFormState(null)
const payload1 = field.getFormDataFromState(state1)
const payload2 = field.getFormDataFromState(state2)
expect(payload1).toEqual(getFormData(item.value))
expect(payload2).toEqual(getFormData())
})
it.each([...options.examples])('returns value from state', (item) => {
const state1 = getFormState(item.value)
const state2 = getFormState(null)
const value1 = field.getFormValueFromState(state1)
const value2 = field.getFormValueFromState(state2)
expect(value1).toEqual(item.value)
expect(value2).toBeUndefined()
})
it.each([...options.examples])(
'returns context for conditions and form submission',
(item) => {
const state1 = getFormState(item.value)
const state2 = getFormState(null)
const value1 = field.getContextValueFromState(state1)
const value2 = field.getContextValueFromState(state2)
expect(value1).toEqual(item.value)
expect(value2).toBeNull()
}
)
it.each([...options.examples])('returns state from payload', (item) => {
const payload1 = getFormData(item.value)
const payload2 = getFormData()
const value1 = field.getStateFromValidForm(payload1)
const value2 = field.getStateFromValidForm(payload2)
expect(value1).toEqual(getFormState(item.value))
expect(value2).toEqual(getFormState(null))
})
})
describe('View model', () => {
it('sets Nunjucks component defaults', () => {
const item = options.examples[0]
const viewModel = field.getViewModel(getFormData(item.value))
expect(viewModel).toEqual(
expect.objectContaining({
label: { text: def.title },
name: 'myComponent',
id: 'myComponent',
value: item.value
})
)
})
it.each([...options.examples])(
'sets Nunjucks component select options',
(item) => {
const viewModel = field.getViewModel(getFormData(item.value))
expect(viewModel.items?.[0]).toMatchObject({
value: '' // First item is always empty
})
expect(viewModel.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
text: item.text,
value: item.value,
selected: true
})
])
)
}
)
})
describe('Select options', () => {
it('returns select options', () => {
expect(field).toHaveProperty('items', options.list.items)
})
it('returns select options matching type', () => {
expect(field).toHaveProperty('values', expect.arrayContaining([]))
})
it('returns empty items when missing', () => {
const model = new FormModel(definition, {
basePath: 'test'
})
const { items } = new SelectField(def, { model })
expect(items).toEqual([])
})
})
})
})