fixes erroneously invalidating responses

This commit is contained in:
Alex Corey
2020-05-11 12:13:07 -04:00
parent 1ca3d1fe3b
commit 9360d3fabc
2 changed files with 71 additions and 3 deletions

View File

@@ -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;
};
};

View File

@@ -225,4 +225,67 @@ describe('<SurveyQuestionForm />', () => {
wrapper.find('FormField#question-default input').prop('type')
).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.');
});
});