Skip to content

Commit

Permalink
fix: add tests for replacing dots with underscores
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkim-ogp authored and pregnantboy committed Dec 23, 2024
1 parent e878f0d commit da9ac46
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,98 @@ describe('decrypt form response', () => {
}),
)
})

it('should parse form fields and replace dots with underscores in keys', async () => {
mockDecryptedSubmission({
responses: [
{
_id: 'question1.field.answer',
fieldType: 'textarea',
question: 'What do you eat for breakfast?',
answer: 'i eat lorem dimsum for breakfast',
},
{
_id: 'question2.field.answer',
fieldType: 'mobile',
question: 'What is your mobile number?',
answer: '+6591234567',
},
],
})
await expect(decryptFormResponse($)).resolves.toEqual(true)
expect($.request.body).toEqual(
expect.objectContaining({
fields: {
question1_field_answer: {
fieldType: 'textarea',
question: 'What do you eat for breakfast?',
answer: 'i eat lorem dimsum for breakfast',
order: 1,
},
question2_field_answer: {
fieldType: 'mobile',
question: 'What is your mobile number?',
answer: '+6591234567',
order: 2,
},
},
}),
)
expect($.request.headers).toBeUndefined()
expect($.request.query).toBeUndefined()
})

it('should parse form fields and replace dots with underscores in keys', async () => {
mockDecryptedSubmission({
responses: [
{
_id: 'childrenbirthrecords.abc.childdateofbirth.0',
fieldType: 'children',
question: 'Child Date of birth',
answer: '31/03/2017',
},
{
_id: 'childrenbirthrecords.abc.childname.0',
fieldType: 'children',
question: 'Child Name',
answer: 'John Doe',
},
{
_id: 'question2.field.answer',
fieldType: 'mobile',
question: 'What is your mobile number?',
answer: '+6591234567',
},
],
})
await expect(decryptFormResponse($)).resolves.toEqual(true)
expect($.request.body).toEqual(
expect.objectContaining({
fields: {
childrenbirthrecords_abc_childdateofbirth_0: {
fieldType: 'children',
question: 'Child Date of birth',
answer: '31/03/2017',
order: 1,
},
childrenbirthrecords_abc_childname_0: {
fieldType: 'children',
question: 'Child Name',
answer: 'John Doe',
order: 2,
},
question2_field_answer: {
fieldType: 'mobile',
question: 'What is your mobile number?',
answer: '+6591234567',
order: 3,
},
},
}),
)
expect($.request.headers).toBeUndefined()
expect($.request.query).toBeUndefined()
})
})

describe('attachments', () => {
Expand Down
30 changes: 30 additions & 0 deletions packages/backend/src/helpers/__tests__/compute-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,34 @@ describe('compute parameters', () => {
const result = computeParameters(params, executionStep)
expect(result).toEqual(expected)
})

it('should process parameters with underscores in keys', () => {
const executionStep = [
{
stepId: randomStepID,
dataOut: {
childrenbirthrecords_abc_childdateofbirth_0: {
fieldType: 'children',
question: 'Child Date of birth',
answer: '31/03/2017',
order: 1,
},
childrenbirthrecords_abc_childname_0: {
fieldType: 'children',
question: 'Child Name',
answer: 'John Doe',
order: 2,
},
},
} as unknown as ExecutionStep,
]
const params = {
toSubstitute: `{{step.${randomStepID}.childrenbirthrecords_abc_childdateofbirth_0.answer}} {{step.${randomStepID}.childrenbirthrecords_abc_childname_0.answer}}`,
}
const expected = {
toSubstitute: `31/03/2017 John Doe`,
}
const result = computeParameters(params, executionStep)
expect(result).toEqual(expected)
})
})

0 comments on commit da9ac46

Please sign in to comment.