mirror of
https://github.com/ansible/awx.git
synced 2026-05-20 07:17:40 -02:30
Merge pull request #6988 from AlexSCorey/6946-SurveyFormValidationError
6946 survey form validation error Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
@@ -18,6 +18,13 @@ export default function SurveyQuestionAdd({ survey, updateSurvey }) {
|
|||||||
);
|
);
|
||||||
return;
|
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];
|
const newSpec = survey.spec ? survey.spec.concat(question) : [question];
|
||||||
await updateSurvey(newSpec);
|
await updateSurvey(newSpec);
|
||||||
history.push(match.url.replace('/add', ''));
|
history.push(match.url.replace('/add', ''));
|
||||||
|
|||||||
@@ -39,6 +39,13 @@ export default function SurveyQuestionEdit({ survey, updateSurvey }) {
|
|||||||
if (questionIndex === -1) {
|
if (questionIndex === -1) {
|
||||||
throw new Error('Question not found in spec');
|
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([
|
await updateSurvey([
|
||||||
...survey.spec.slice(0, questionIndex),
|
...survey.spec.slice(0, questionIndex),
|
||||||
formData,
|
formData,
|
||||||
|
|||||||
@@ -73,12 +73,15 @@ function SurveyQuestionForm({
|
|||||||
}) {
|
}) {
|
||||||
const defaultIsNotAvailable = choices => {
|
const defaultIsNotAvailable = choices => {
|
||||||
return defaultValue => {
|
return defaultValue => {
|
||||||
if (!choices.includes(defaultValue)) {
|
let errorMessage;
|
||||||
return i18n._(
|
const found = [...defaultValue].every(dA => choices.indexOf(dA) > -1);
|
||||||
|
|
||||||
|
if (!found) {
|
||||||
|
errorMessage = i18n._(
|
||||||
t`Default choice must be answered from the choices listed.`
|
t`Default choice must be answered from the choices listed.`
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return undefined;
|
return errorMessage;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -225,4 +225,67 @@ describe('<SurveyQuestionForm />', () => {
|
|||||||
wrapper.find('FormField#question-default input').prop('type')
|
wrapper.find('FormField#question-default input').prop('type')
|
||||||
).toEqual('number');
|
).toEqual('number');
|
||||||
});
|
});
|
||||||
|
test('should not throw validation error', async () => {
|
||||||
|
let wrapper;
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
wrapper = mountWithContexts(
|
||||||
|
<SurveyQuestionForm
|
||||||
|
question={question}
|
||||||
|
handleSubmit={noop}
|
||||||
|
handleCancel={noop}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
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(
|
||||||
|
<SurveyQuestionForm
|
||||||
|
question={question}
|
||||||
|
handleSubmit={noop}
|
||||||
|
handleCancel={noop}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
});
|
||||||
|
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.');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user