Skip to content

Commit

Permalink
ON-42451 # formElementsService.getRootElementValueById() to support r…
Browse files Browse the repository at this point in the history
…etrieving submission data from nested forms
  • Loading branch information
mymattcarroll committed Aug 5, 2024
1 parent c20f305 commit 7820691
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Changed

- `formElementsService.getRootElementValueById()` to support retrieving submission data from nested forms

### Changed

- check if repeatable set element value is an empty array

## [6.3.0] - 2024-06-21
Expand Down
42 changes: 31 additions & 11 deletions src/submissionService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,40 @@ export * from './replaceCustomValues'
export function getRootElementValueById(
formElementId: string,
formElements: FormTypes.FormElement[],
submission: SubmissionTypes.S3SubmissionData['submission'],
submission: SubmissionTypes.S3SubmissionData['submission'] | undefined,
): unknown {
for (const formElement of formElements) {
if (formElement.type === 'page' || formElement.type === 'section') {
const value = getRootElementValueById(
formElementId,
formElement.elements,
submission,
)
if (value !== undefined) {
return value
switch (formElement.type) {
case 'section':
case 'page': {
const value = getRootElementValueById(
formElementId,
formElement.elements,
submission,
)
if (value !== undefined) {
return value
}
break
}
case 'form': {
const value = getRootElementValueById(
formElementId,
formElement.elements || [],
submission?.[
formElement.name
] as SubmissionTypes.S3SubmissionData['submission'],
)
if (value !== undefined) {
return value
}
break
}
default: {
if (formElement.id === formElementId) {
return submission?.[formElement.name]
}
}
} else if (formElement.id === formElementId) {
return submission[formElement.name]
}
}
}
38 changes: 34 additions & 4 deletions tests/formElementsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getRootElementValueById } from '../src/submissionService'
import forms from './inject-forms-fixtures/valid-forms.json'
import invalidForms from './inject-forms-fixtures/invalid-forms.json'

describe('findPaymentElementValue()', () => {
describe('getRootElementValueById()', () => {
const createTextElement = (name: string) => {
const textElement: FormTypes.TextElement = {
id: name,
Expand Down Expand Up @@ -68,6 +68,20 @@ describe('findPaymentElementValue()', () => {
requiresAllConditionallyShowPredicates: false,
}
}
const createFormElement = (
id: string,
elements: FormTypes.FormElement[],
): FormTypes.FormFormElement => {
return {
id,
name: id,
type: 'form',
elements,
conditionallyShow: false,
formId: 1,
requiresAllConditionallyShowPredicates: false,
}
}
const elements: FormTypes.FormElement[] = [
createPageElement('Page1', [
createTextElement('A'),
Expand All @@ -86,6 +100,9 @@ describe('findPaymentElementValue()', () => {
]),
createTextElement('C'),
createNumberElement('D'),
createFormElement('E', [
createFormElement('E_A', [createTextElement('E_A_A')]),
]),
]),
createPageElement('Page2', [createTextElement('Page2Text')]),
]
Expand All @@ -101,9 +118,14 @@ describe('findPaymentElementValue()', () => {
C: 'CText',
D: 5,
Page2Text: 'Page 2 Text',
E: {
E_A: {
E_A_A: 'Nested Form Text',
},
},
}

test('should return a nested element value within a submission object', () => {
test('should return an element value within a submission object', () => {
const elementNameAndId = 'D'
const value = getRootElementValueById(
elementNameAndId,
Expand All @@ -121,8 +143,16 @@ describe('findPaymentElementValue()', () => {
)
expect(value).toBe('B_C_C_AText')
})

test('`getPaymentValueFromPath` should return undefined when given an invalid element id', () => {
test('should return a nested element value in a nested form within a submission object', () => {
const elementNameAndId = 'E_A_A'
const value = getRootElementValueById(
elementNameAndId,
elements,
submission,
)
expect(value).toBe('Nested Form Text')
})
test('should return undefined when given an invalid element id', () => {
const value = getRootElementValueById('abc123', elements, submission)
expect(value).toBe(undefined)
})
Expand Down

0 comments on commit 7820691

Please sign in to comment.