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.'); + }); });