Skip to content

Commit

Permalink
feat(short_text): add new field type (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
arszh authored and lirantal committed Nov 26, 2019
1 parent 02853c5 commit 628b7fd
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 13 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ The following form fields are currently supported to extract:
- Yes/No questions (`yes_no`)
- Opinion scale questions (`opinion_scale`)
- All variants of multiple choice questions, including an 'other' field, and single vs multiple answers (`multiple_choice`)

## Unsupported fields

- Free text questions (`short_text`)

# Related
Expand Down
22 changes: 20 additions & 2 deletions __tests__/Form/__fixtures__/formResponsesResult.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
},
"type": "choice",
"choice": {
"label": "Option 1"
"label": "Every couple of years or more"
}
},
{
Expand All @@ -73,6 +73,15 @@
"Sometimes, we do."
]
}
},
{
"field": {
"id": "tCDuMq4dJaIf",
"type": "short_text",
"ref": "500ea3ea-a37e-46da-aca4-01d294c22fb0"
},
"type": "text",
"text": "Bart"
}
]
},
Expand Down Expand Up @@ -127,7 +136,7 @@
},
"type": "choice",
"choice": {
"label": "Option 2"
"label": "At least once a Month"
}
},
{
Expand All @@ -142,6 +151,15 @@
"We always do"
]
}
},
{
"field": {
"id": "tCDuMq4dJaIf",
"type": "short_text",
"ref": "500ea3ea-a37e-46da-aca4-01d294c22fb0"
},
"type": "text",
"text": "Homer"
}
]
}
Expand Down
9 changes: 9 additions & 0 deletions __tests__/Form/__fixtures__/formResult.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,15 @@
"required": true
},
"type": "multiple_choice"
},
{
"id": "tCDuMq4dJaIf",
"title": "Question number 6?",
"ref": "500ea3ea-a37e-46da-aca4-01d294c22fb0",
"validations": {
"required": true
},
"type": "short_text"
}
],
"_links": {
Expand Down
59 changes: 53 additions & 6 deletions __tests__/Form/__snapshots__/form.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@
exports[`Form Fetch Form Responses Form responses full structure is normalized 1`] = `
Object {
"fields": Object {
"500ea3ea-a37e-46da-aca4-01d294c22fb0": Object {
"answers": Object {
"Bart": Array [
Object {
"metadata": Object {
"browser": "default",
"network_id": "3c3a3176ac",
"platform": "other",
"referer": "https://example.typeform.com/to/AbCeDeF",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
},
"response_id": "29862c13c9a919816a9c919162",
"submitted_at": "2018-10-19T12:56:33Z",
},
],
"Homer": Array [
Object {
"metadata": Object {
"browser": "default",
"network_id": "c13c1ac132",
"platform": "other",
"referer": "https://example.typeform.com/to/AbCeDeF",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
},
"response_id": "c1c13c1c1c132c213f13",
"submitted_at": "2018-10-20T12:41:55Z",
},
],
},
"id": "tCDuMq4dJaIf",
"properties": undefined,
"ref": "500ea3ea-a37e-46da-aca4-01d294c22fb0",
"title": "Question number 6?",
"totalRespondents": 2,
"type": "short_text",
},
"9a9a9a9c9-a5aa-1c1c-2c2c-c2c1c11c1": Object {
"answers": Object {
"Maybe most times we do": Array [],
Expand Down Expand Up @@ -83,12 +119,7 @@ Object {
},
"ac4ac4a-acac-a5a5-ac4a-ac5ac3ac5a5a": Object {
"answers": Object {
"At least once a Month": Array [],
"At least once a Quarter": Array [],
"At least once a year": Array [],
"Every couple of years or more": Array [],
"We don't": Array [],
"other": Array [
"At least once a Month": Array [
Object {
"metadata": Object {
"browser": "default",
Expand All @@ -101,6 +132,22 @@ Object {
"submitted_at": "2018-10-20T12:41:55Z",
},
],
"At least once a Quarter": Array [],
"At least once a year": Array [],
"Every couple of years or more": Array [
Object {
"metadata": Object {
"browser": "default",
"network_id": "3c3a3176ac",
"platform": "other",
"referer": "https://example.typeform.com/to/AbCeDeF",
"user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
},
"response_id": "29862c13c9a919816a9c919162",
"submitted_at": "2018-10-19T12:56:33Z",
},
],
"We don't": Array [],
},
"id": "CtPB5r54ScEN",
"properties": Object {
Expand Down
4 changes: 4 additions & 0 deletions src/Form/Adapters/formFieldsAdapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ formFieldsAnswersAdapters.set('multiple_choice', questionField => {
return answers
})

formFieldsAnswersAdapters.set('short_text', questionField => {
return {}
})

module.exports = formFieldsAnswersAdapters
12 changes: 12 additions & 0 deletions src/Form/Adapters/formResponsesAnswersAdapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@ formResponseAnswersAdapters.set('multiple_choice', (submission, answer) => {
}
})

formResponseAnswersAdapters.set('short_text', (submission, answer) => {
const answerSubmission = {
response_id: submission.response_id,
submitted_at: submission.submitted_at,
metadata: submission.metadata
}
return {
answerLabel: answer.text,
answerData: answerSubmission
}
})

module.exports = formResponseAnswersAdapters
3 changes: 1 addition & 2 deletions src/Form/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = class Form {
}

_getFormQuestions(form) {
const formTypesWhitelist = ['yes_no', 'opinion_scale', 'multiple_choice']
const formTypesWhitelist = ['yes_no', 'opinion_scale', 'multiple_choice', 'short_text']
let formQuestions = {}

formQuestions = form.fields
Expand Down Expand Up @@ -146,7 +146,6 @@ module.exports = class Form {
let {answerLabel, answerData} = adaptorResponse

if (form.fields[fieldReference].answers[answerLabel] === undefined) {
answerLabel = 'other'
form.fields[fieldReference].answers[answerLabel] = []
}

Expand Down

0 comments on commit 628b7fd

Please sign in to comment.