-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGeospatialField.test.ts
More file actions
380 lines (326 loc) · 11 KB
/
GeospatialField.test.ts
File metadata and controls
380 lines (326 loc) · 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import {
ComponentType,
type GeospatialFieldComponent
} from '@defra/forms-model'
import { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js'
import { type GeospatialField } from '~/src/server/plugins/engine/components/GeospatialField.js'
import {
validSingleState,
validState
} from '~/src/server/plugins/engine/components/helpers/__stubs__/geospatial.js'
import {
getAnswer,
type Field
} from '~/src/server/plugins/engine/components/helpers/components.js'
import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
import { type GeospatialState } from '~/src/server/plugins/engine/types.js'
import definition from '~/test/form/definitions/blank.js'
import { getFormData, getFormState } from '~/test/helpers/component-helpers.js'
describe('GeospatialField', () => {
let model: FormModel
beforeEach(() => {
model = new FormModel(definition, {
basePath: 'test'
})
})
describe('Defaults', () => {
let def: GeospatialFieldComponent
let collection: ComponentCollection
let field: Field
beforeEach(() => {
def = {
title: 'Example geospatial title',
shortDescription: 'Example geospatial',
name: 'myComponent',
type: ComponentType.GeospatialField,
options: {}
} satisfies GeospatialFieldComponent
collection = new ComponentCollection([def], { model })
field = collection.fields[0]
})
describe('Schema', () => {
it('uses component short description as label', () => {
const { formSchema } = collection
const { keys } = formSchema.describe()
expect(keys).toHaveProperty(
'myComponent',
expect.objectContaining({
flags: expect.objectContaining({
label: 'Example geospatial'
})
})
)
})
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 result = collectionOptional.validate(getFormData('[]'))
expect(result.errors).toBeUndefined()
const result2 = collectionOptional.validate(getFormData([]))
expect(result2.errors).toBeUndefined()
})
it('accepts valid values', () => {
const result1 = collection.validate(getFormData(validState))
expect(result1.errors).toBeUndefined()
})
it('adds errors for empty value', () => {
const result = collection.validate(getFormData(''))
expect(result.errors).toEqual([
expect.objectContaining({
text: 'Example geospatial must contain at least 1 items'
})
])
})
it('adds errors for empty value given no short description', () => {
def = {
title: 'Example geospatial title',
name: 'myComponent',
type: ComponentType.GeospatialField,
options: {}
} satisfies GeospatialFieldComponent
collection = new ComponentCollection([def], { model })
const result = collection.validate(getFormData(''))
expect(result.errors).toEqual([
expect.objectContaining({
text: 'Example geospatial title must contain at least 1 items'
})
])
})
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('returns text from single feature state', () => {
const state1 = getFormState(validSingleState)
const state2 = getFormState(null)
const answer1 = getAnswer(field, state1)
const answer2 = getAnswer(field, state2)
expect(answer1).toBe('Added 1 location')
expect(answer2).toBe('')
})
it('returns text from multiple features state', () => {
const state1 = getFormState(validState)
const state2 = getFormState(null)
const answer1 = getAnswer(field, state1)
const answer2 = getAnswer(field, state2)
expect(answer1).toBe('Added 4 locations')
expect(answer2).toBe('')
})
it('returns payload from state', () => {
const state1 = getFormState(validState)
const state2 = getFormState(null)
const payload1 = field.getFormDataFromState(state1)
const payload2 = field.getFormDataFromState(state2)
expect(payload1).toEqual(getFormData(validState))
expect(payload2).toEqual(getFormData())
})
it('returns value from state', () => {
const state1 = getFormState(validState)
const state2 = getFormState(null)
const value1 = field.getFormValueFromState(state1)
const value2 = field.getFormValueFromState(state2)
expect(value1).toBe(validState)
expect(value2).toBeUndefined()
})
it('returns context for conditions and form submission', () => {
const state1 = getFormState(validState)
const state2 = getFormState(null)
const value1 = field.getContextValueFromState(state1)
const value2 = field.getContextValueFromState(state2)
const { id: id1 } = validState[0]
const { id: id2 } = validState[1]
const { id: id3 } = validState[2]
const { id: id4 } = validState[3]
expect(value1).toEqual([id1, id2, id3, id4])
expect(value2).toBeNull()
})
it('returns state from payload', () => {
const payload1 = getFormData(validState)
const payload2 = getFormData()
const value1 = field.getStateFromValidForm(payload1)
const value2 = field.getStateFromValidForm(payload2)
expect(value1).toEqual(getFormState(validState))
expect(value2).toEqual(getFormState(null))
})
})
describe('View model', () => {
it('sets Nunjucks component defaults', () => {
const viewModel = field.getViewModel(getFormData('Geospatial'))
expect(viewModel).toEqual(
expect.objectContaining({
label: { text: def.title },
name: 'myComponent',
id: 'myComponent',
value: 'Geospatial'
})
)
})
})
describe('AllPossibleErrors', () => {
it('should return errors', () => {
const errors = field.getAllPossibleErrors()
expect(errors.baseErrors).not.toBeEmpty()
expect(errors.advancedSettingsErrors).toBeEmpty()
})
})
})
describe('Validation', () => {
describe.each([
{
description: 'Required',
component: {
title: 'Example geospatial field',
name: 'myComponent',
type: ComponentType.GeospatialField,
options: {
required: true
}
} satisfies GeospatialFieldComponent,
assertions: [
{
input: getFormData([]),
output: {
value: getFormData([]),
errors: [
expect.objectContaining({
text: 'Example geospatial field must contain at least 1 items'
})
]
}
},
{
input: getFormData(),
output: {
value: getFormData(),
errors: [
expect.objectContaining({
text: 'Select example geospatial field'
})
]
}
},
{
input: getFormData(validSingleState),
output: {
value: getFormData(validSingleState)
}
},
{
input: getFormData(validState),
output: {
value: getFormData(validState)
}
}
]
},
{
description: 'Optional',
component: {
title: 'Example geospatial field',
name: 'myComponent',
type: ComponentType.GeospatialField,
options: {
required: false
}
} satisfies GeospatialFieldComponent,
assertions: [
{
input: getFormData([]),
output: {
value: getFormData([])
}
},
{
input: getFormData(),
output: {
value: getFormData(),
errors: [
expect.objectContaining({
text: 'Select example geospatial field'
})
]
}
}
]
}
])('$description', ({ component: def, assertions }) => {
let collection: ComponentCollection
beforeEach(() => {
collection = new ComponentCollection([def], { model })
})
it.each([...assertions])(
'validates custom example',
({ input, output }) => {
const result = collection.validate(input)
expect(result).toEqual(output)
}
)
})
it('getErrors formats description errors', () => {
const component = {
title: 'Example geospatial field',
name: 'myComponent',
type: ComponentType.GeospatialField,
options: {
required: true
}
} satisfies GeospatialFieldComponent
const collection = new ComponentCollection([component], { model })
const invalidSingleState: GeospatialState = [
{
type: 'Feature',
properties: {
coordinateGridReference: 'ST 00001',
centroidGridReference: 'ST 00001',
description: '' // Missing description should trigger error with href to description field and custom text
},
geometry: {
coordinates: [-2.5723699109417737, 53.2380485215034],
type: 'Point'
},
id: 'a'
}
]
const result = collection.validate(getFormData(invalidSingleState))
const geospatialField = collection.components.at(0) as GeospatialField
const errors = geospatialField.getErrors(result.errors)
expect(errors).toEqual([
expect.objectContaining({
name: 'description',
href: '#description_0',
text: 'Enter description for location 1'
})
])
})
})
})