Skip to content

Commit e7e6c5c

Browse files
committed
fix: tests and added more
1 parent d6aca17 commit e7e6c5c

File tree

2 files changed

+55
-17
lines changed

2 files changed

+55
-17
lines changed

packages/backend/src/apps/formsg/__tests__/triggers/new-submission.test.ts

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ describe('new submission trigger', () => {
2626
attr: 'name',
2727
},
2828
},
29+
headerFieldId: {
30+
question: 'Section Header',
31+
fieldType: 'section',
32+
order: 2,
33+
},
2934
},
3035
verifiedSubmitterInfo: {
3136
uinFin: 'S1234567B',
@@ -64,10 +69,12 @@ describe('new submission trigger', () => {
6469
expect(metadata.fields.textFieldId.question.label).toEqual('Question 1')
6570
})
6671

67-
it('changes the answer label to "Response #n"', async () => {
72+
it('changes the answer label to "1. What is your name?"', async () => {
6873
const metadata = await trigger.getDataOutMetadata(executionStep)
6974

70-
expect(metadata.fields.textFieldId.answer.label).toEqual('Response 1')
75+
expect(metadata.fields.textFieldId.answer.label).toEqual(
76+
'1. What is your name?',
77+
)
7178
})
7279

7380
it('positions the answer after the question', async () => {
@@ -78,20 +85,29 @@ describe('new submission trigger', () => {
7885
)
7986
})
8087

81-
it('sets label and order to null if question number is undefined', async () => {
88+
it('should computes order for questions and answers even if order is not provided', async () => {
8289
const fields = executionStep.dataOut.fields as IJSONObject
83-
fields.textFieldId = {
84-
question: 'What is your name?',
85-
answer: 'herp derp',
86-
fieldType: 'textField',
90+
delete fields.textFieldId
91+
// generate a few fields
92+
for (let i = 0; i < 10; i++) {
93+
fields[`textFieldId${i + 1}`] = {
94+
question: `What is your name? ${i}`,
95+
answer: 'herp derp',
96+
fieldType: 'textField',
97+
order: i < 5 ? i + 1 : null,
98+
}
8799
}
88100

89101
const metadata = await trigger.getDataOutMetadata(executionStep)
90102

91-
expect(metadata.fields.textFieldId.question.order).toBeNull()
92-
expect(metadata.fields.textFieldId.answer.order).toBeNull()
93-
expect(metadata.fields.textFieldId.question.label).toBeNull()
94-
expect(metadata.fields.textFieldId.answer.label).toBeNull()
103+
expect(metadata.fields.textFieldId1.question.order).toBe(1)
104+
expect(metadata.fields.textFieldId1.answer.order).toBe(1.1)
105+
expect(metadata.fields.textFieldId2.question.order).toBe(2)
106+
expect(metadata.fields.textFieldId2.answer.order).toBe(2.1)
107+
expect(metadata.fields.textFieldId5.question.order).toBe(5)
108+
expect(metadata.fields.textFieldId5.answer.order).toBe(5.1)
109+
expect(metadata.fields.textFieldId6.question.order).toBe(6)
110+
expect(metadata.fields.textFieldId6.answer.order).toBe(6.1)
95111
})
96112

97113
it('sets a label for SingPass verified NRIC/FIN', async () => {
@@ -170,6 +186,20 @@ describe('new submission trigger', () => {
170186
expect(metadata.fields.fileFieldId.answer.label).toEqual('Attach a file.')
171187
})
172188

189+
it('collapses header fields', async () => {
190+
const metadata = await trigger.getDataOutMetadata(executionStep)
191+
expect(
192+
metadata.fields.headerFieldId.question.isCollapsedByDefault,
193+
).toEqual(true)
194+
})
195+
196+
it('collapses question variables', async () => {
197+
const metadata = await trigger.getDataOutMetadata(executionStep)
198+
expect(metadata.fields.textFieldId.question.isCollapsedByDefault).toEqual(
199+
true,
200+
)
201+
})
202+
173203
it('hides attachment questions', async () => {
174204
executionStep.dataOut.fields = {
175205
fileFieldId: {
@@ -277,7 +307,8 @@ describe('new submission trigger for answer array fields', () => {
277307
answerArray: ['lunch', 'dinner'],
278308
},
279309
textFieldId2: {
280-
question: 'What are your hobbies? When do you do them?',
310+
question:
311+
'What are your hobbies? When do you do them? (activity, time)',
281312
fieldType: 'table',
282313
order: 2,
283314
answerArray: [
@@ -327,7 +358,7 @@ describe('new submission trigger for answer array fields', () => {
327358
// type will be array instead of text!
328359
expect(array).toEqual({
329360
type: 'array',
330-
label: 'Response 1',
361+
label: '1. Have you had your meals?',
331362
order: 1.1,
332363
})
333364
})
@@ -345,8 +376,10 @@ describe('new submission trigger for answer array fields', () => {
345376
for (let i = 0; i < array.length; i++) {
346377
const nestedArray = array[i]
347378
for (let j = 0; j < array.length; j++) {
348-
expect(nestedArray[j].label).toEqual(
349-
`Response 2, Row ${i + 1} Column ${j + 1}`,
379+
expect(nestedArray[j].label).toBe(
380+
`2. Row ${i + 1} ${
381+
j === 0 ? 'activity' : 'time'
382+
} - What are your hobbies? When do you do them?`,
350383
)
351384
}
352385
}
@@ -370,7 +403,9 @@ describe('new submission trigger for answer array fields', () => {
370403

371404
expect(addressMetadata).toHaveLength(5)
372405
ADDRESS_LABELS.forEach((label, index) => {
373-
expect(addressMetadata[index].label).toEqual(`Response 4, ${label}`)
406+
expect(addressMetadata[index].label).toEqual(
407+
`4.${index + 1}. ${label} - What is your address?`,
408+
)
374409
})
375410
})
376411

@@ -379,7 +414,9 @@ describe('new submission trigger for answer array fields', () => {
379414
const addressMetadata = metadata.fields.addressFieldPartial
380415
.answerArray as IDataOutMetadatum[]
381416
ADDRESS_LABELS.forEach((label, index) => {
382-
expect(addressMetadata[index].label).toEqual(`Response 5, ${label}`)
417+
expect(addressMetadata[index].label).toEqual(
418+
`5.${index + 1}. ${label} - What is your address?`,
419+
)
383420
})
384421
})
385422
})

packages/frontend/src/components/AttachmentSuggestions/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export function reformatToCheckboxVariables(
138138
size,
139139
updatedAt,
140140
uploaded: true,
141+
isCollapsedByDefault: false,
141142
}
142143
})
143144
}

0 commit comments

Comments
 (0)