Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,36 @@ describe('Submission helpers', () => {
}
])
})

it('should send empty string for optional GeospatialField', () => {
const mockGeospatialField = Object.create(
GeospatialField.prototype
) as GeospatialField
mockGeospatialField.name = 'geospatial'
mockGeospatialField.options = { required: false }

const items = [
{
name: 'geospatial',
label: 'Site features',
field: mockGeospatialField,
state: {
geospatial: undefined
} as FormSubmissionState
}
] as unknown as DetailItemField[]

const result = buildMainRecords(items)

expect(result).toHaveLength(1)
expect(result).toEqual([
{
name: 'geospatial',
title: 'Site features',
value: ''
}
])
})
})

describe('buildRepeaterRecords', () => {
Expand Down Expand Up @@ -383,5 +413,51 @@ describe('Submission helpers', () => {
}
])
})

it('should send empty string for optional GeospatialField', () => {
const mockGeospatialField = Object.create(
GeospatialField.prototype
) as GeospatialField
mockGeospatialField.name = 'geospatial'
mockGeospatialField.options = { required: false }

const items = [
{
name: 'features',
label: 'Site features repeater',
subItems: [
[
{
name: 'geospatial',
label: 'Site features',
field: mockGeospatialField,
state: {
geospatial: undefined
} as FormSubmissionState
} as unknown as DetailItemField[]
]
]
}
] as unknown as DetailItemField[]

const result = buildRepeaterRecords(items)

expect(result).toHaveLength(1)
expect(result).toEqual([
{
name: 'features',
title: 'Site features repeater',
value: [
[
{
name: 'geospatial',
title: 'Site features',
value: ''
}
]
]
}
])
})
})
})
29 changes: 18 additions & 11 deletions src/server/plugins/engine/pageControllers/helpers/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ export function buildMainRecords(items: DetailItem[]): SubmitRecord[] {
} else if (item.field instanceof GeospatialField) {
// Stringify of GeoJSON is done here rather than inside `getContextValueFromState`
// so we don't incur the overhead of JSON.stringify on every request when building context
const value = item.field.getFormValueFromState(item.state)
records.push({
name: item.name,
title: item.label,
value: JSON.stringify(item.field.getFormValueFromState(item.state))
value: value === undefined ? '' : JSON.stringify(value)
})
} else {
records.push({
Expand Down Expand Up @@ -109,18 +110,24 @@ export function buildRepeaterRecords(
name: item.name,
title: item.label,
value: item.subItems.map((detailItems) =>
detailItems.map((subItem) => ({
name: subItem.name,
title: subItem.label,
value:
detailItems.map((subItem) => {
let value

if (subItem.field instanceof GeospatialField) {
// Stringify of GeoJSON is done here rather than inside `getContextValueFromState`
// so we don't incur the overhead of JSON.stringify on every request when building context
subItem.field instanceof GeospatialField
? JSON.stringify(
subItem.field.getFormValueFromState(subItem.state)
)
: getAnswer(subItem.field, subItem.state, { format: 'data' })
}))
const formValue = subItem.field.getFormValueFromState(subItem.state)
value = formValue === undefined ? '' : JSON.stringify(formValue)
} else {
value = getAnswer(subItem.field, subItem.state, { format: 'data' })
}

return {
name: subItem.name,
title: subItem.label,
value
}
})
)
}))
}
Loading