From 9360d3fabc34bd7611a90416b12254718ed467f8 Mon Sep 17 00:00:00 2001 From: Alex Corey Date: Mon, 11 May 2020 12:13:07 -0400 Subject: [PATCH 1/2] fixes erroneously invalidating responses --- .../Template/Survey/SurveyQuestionForm.jsx | 11 +++- .../Survey/SurveyQuestionForm.test.jsx | 63 +++++++++++++++++++ 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx index 3ff84fc23d..cec6d73be5 100644 --- a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx +++ b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx @@ -68,12 +68,17 @@ function SurveyQuestionForm({ }) { const defaultIsNotAvailable = choices => { return defaultValue => { - if (!choices.includes(defaultValue)) { - return i18n._( + const answerChoices = new Set(choices); + const defaultAnswers = new Set(defaultValue); + let errorMessage; + const found = [...defaultAnswers].every(dA => answerChoices.has(dA)); + + if (!found) { + errorMessage = i18n._( t`Default choice must be answered from the choices listed.` ); } - return undefined; + return errorMessage; }; }; diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.test.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.test.jsx index fb7111415f..6a73fe278c 100644 --- a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.test.jsx +++ b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.test.jsx @@ -225,4 +225,67 @@ describe('', () => { wrapper.find('FormField#question-default input').prop('type') ).toEqual('number'); }); + test('should not throw validation error', async () => { + let wrapper; + + act(() => { + wrapper = mountWithContexts( + + ); + }); + await selectType(wrapper, 'multiselect'); + await act(async () => + wrapper.find('TextArea#question-options').prop('onChange')('a \n b', { + target: { value: 'a \n b', name: 'choices' }, + }) + ); + await act(async () => + wrapper.find('TextArea#question-default').prop('onChange')('b \n a', { + target: { value: 'b \n a', name: 'default' }, + }) + ); + wrapper.find('FormField#question-default').prop('validate')('b \n a', {}); + wrapper.update(); + expect( + wrapper + .find('FormGroup[fieldId="question-default"]') + .prop('helperTextInvalid') + ).toBe(undefined); + }); + + test('should throw validation error', async () => { + let wrapper; + + act(() => { + wrapper = mountWithContexts( + + ); + }); + await selectType(wrapper, 'multiselect'); + await act(async () => + wrapper.find('TextArea#question-options').prop('onChange')('a \n b', { + target: { value: 'a \n b', name: 'choices' }, + }) + ); + await act(async () => + wrapper.find('TextArea#question-default').prop('onChange')('c', { + target: { value: 'c', name: 'default' }, + }) + ); + wrapper.find('FormField#question-default').prop('validate')('c', {}); + wrapper.update(); + expect( + wrapper + .find('FormGroup[fieldId="question-default"]') + .prop('helperTextInvalid') + ).toBe('Default choice must be answered from the choices listed.'); + }); }); From 10ccb57062a2942981b6c6cc4640bdc148de88e7 Mon Sep 17 00:00:00 2001 From: Alex Corey Date: Mon, 11 May 2020 12:13:40 -0400 Subject: [PATCH 2/2] Fixes empty chip issue when user adds empty new line. --- .../src/screens/Template/Survey/SurveyQuestionAdd.jsx | 7 +++++++ .../src/screens/Template/Survey/SurveyQuestionEdit.jsx | 7 +++++++ .../src/screens/Template/Survey/SurveyQuestionForm.jsx | 4 +--- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx index 009c295bf7..22cb8f93a4 100644 --- a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx +++ b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionAdd.jsx @@ -18,6 +18,13 @@ export default function SurveyQuestionAdd({ survey, updateSurvey }) { ); return; } + if (question.type === 'multiselect') { + question.default = question.default + .split('\n') + .filter(v => v !== '' || '\n') + .map(v => v.trim()) + .join('\n'); + } const newSpec = survey.spec ? survey.spec.concat(question) : [question]; await updateSurvey(newSpec); history.push(match.url.replace('/add', '')); diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.jsx index 9c6ac6193a..eefa133be1 100644 --- a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.jsx +++ b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionEdit.jsx @@ -39,6 +39,13 @@ export default function SurveyQuestionEdit({ survey, updateSurvey }) { if (questionIndex === -1) { throw new Error('Question not found in spec'); } + if (formData.type === 'multiselect') { + formData.default = formData.default + .split('\n') + .filter(v => v !== '' || '\n') + .map(v => v.trim()) + .join('\n'); + } await updateSurvey([ ...survey.spec.slice(0, questionIndex), formData, diff --git a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx index cec6d73be5..49de1838e6 100644 --- a/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx +++ b/awx/ui_next/src/screens/Template/Survey/SurveyQuestionForm.jsx @@ -68,10 +68,8 @@ function SurveyQuestionForm({ }) { const defaultIsNotAvailable = choices => { return defaultValue => { - const answerChoices = new Set(choices); - const defaultAnswers = new Set(defaultValue); let errorMessage; - const found = [...defaultAnswers].every(dA => answerChoices.has(dA)); + const found = [...defaultValue].every(dA => choices.indexOf(dA) > -1); if (!found) { errorMessage = i18n._(