fix: check for if an answer changes to false (#250)

This commit is contained in:
Kristin Aoki
2023-02-23 10:02:22 -05:00
committed by GitHub
parent f18353e5fc
commit 76aae38d3a
2 changed files with 15 additions and 3 deletions

View File

@@ -206,12 +206,19 @@ export const typeRowHooks = ({
updateField,
updateAnswer,
}) => {
const clearPreviouslySelectedAnswers = () => {
answers.forEach(answer => {
if (answer.correct) {
updateAnswer({ ...answer, correct: false });
}
});
};
const onClick = () => {
// Dropdown problems can only have one correct answer. When there is more than one correct answer
// from a previous problem type, the correct attribute for selected answers need to be set to false.
if (typeKey === ProblemTypeKeys.DROPDOWN) {
if (correctAnswerCount > 1) {
answers.forEach(answer => {
updateAnswer({ ...answer, correct: false });
});
clearPreviouslySelectedAnswers();
}
}
if (blockTitle === ProblemTypes[problemType].title) {

View File

@@ -263,6 +263,10 @@ describe('Problem settings hooks', () => {
{
correct: true,
id: 'b',
},
{
correct: false,
id: 'c',
}];
output = hooks.typeRowHooks({
answers,
@@ -278,6 +282,7 @@ describe('Problem settings hooks', () => {
expect(setBlockTitle).toHaveBeenCalledWith('Dropdown');
expect(updateAnswer).toHaveBeenNthCalledWith(1, { ...answers[0], correct: false });
expect(updateAnswer).toHaveBeenNthCalledWith(2, { ...answers[1], correct: false });
expect(updateAnswer).not.toHaveBeenNthCalledWith(3, { ...answers[2], correct: false });
expect(updateField).toHaveBeenCalledWith({ problemType: typekey });
});
});