From c2b67429d342370478521028f55ad2fa459785c5 Mon Sep 17 00:00:00 2001
From: connorhaugh <49422820+connorhaugh@users.noreply.github.com>
Date: Thu, 30 Mar 2023 10:34:45 -0400
Subject: [PATCH] feat: add answer range (#291)
* feat: add answer range
* feat: add margin for doropdown
* fix: improve test converage
---
.../AnswerWidget/AnswerOption.jsx | 68 +++++--
.../AnswerWidget/AnswerOption.test.jsx | 12 ++
.../AnswerWidget/AnswersContainer.jsx | 51 ++++-
.../AnswerWidget/AnswersContainer.test.jsx | 72 ++++++++
.../__snapshots__/AnswerOption.test.jsx.snap | 94 ++++++++++
.../AnswersContainer.test.jsx.snap | 174 ++++++++++++++++++
.../EditProblemView/AnswerWidget/index.jsx | 2 +-
.../EditProblemView/AnswerWidget/messages.js | 17 ++
.../settingsComponents/Tolerance/index.jsx | 17 +-
.../Tolerance/index.test.jsx | 41 ++++-
.../__snapshots__/index.test.jsx.snap | 2 +-
.../components/EditProblemView/index.jsx | 2 +-
.../ProblemEditor/data/OLXParser.js | 4 +-
.../data/mockData/olxTestData.js | 2 +
src/editors/data/redux/problem/reducers.js | 35 ++++
.../data/redux/problem/reducers.test.js | 55 ++++++
16 files changed, 606 insertions(+), 42 deletions(-)
diff --git a/src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/AnswerOption.jsx b/src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/AnswerOption.jsx
index 46c88184c..6deba0cbd 100644
--- a/src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/AnswerOption.jsx
+++ b/src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/AnswerOption.jsx
@@ -8,7 +8,7 @@ import {
Form,
} from '@edx/paragon';
import { FeedbackOutline, DeleteOutline } from '@edx/paragon/icons';
-import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
+import { FormattedMessage, injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import messages from './messages';
import { selectors } from '../../../../../data/redux';
import { answerOptionProps } from '../../../../../data/services/cms/types';
@@ -38,6 +38,53 @@ export const AnswerOption = ({
const setSelectedFeedback = hooks.setSelectedFeedback({ answer, hasSingleAnswer, dispatch });
const setUnselectedFeedback = hooks.setUnselectedFeedback({ answer, hasSingleAnswer, dispatch });
const { isFeedbackVisible, toggleFeedback } = hooks.useFeedback(answer);
+
+ const getInputArea = () => {
+ if ([ProblemTypeKeys.SINGLESELECT, ProblemTypeKeys.MULTISELECT].includes(problemType)) {
+ return (
+
{defaultMessage}
), @@ -54,6 +55,77 @@ describe('AnswersContainer', () => { )).toMatchSnapshot(); }); }); + test('snapshot: numeric problems: answer range/answer select button: empty', () => { + act(() => { + const emptyAnswerProps = { + problemType: ProblemTypeKeys.NUMERIC, + answers: [], + updateField: jest.fn(), + addAnswer: jest.fn(), + addAnswerRange: jest.fn(), + }; + expect(shallow( +You can specify optional feedback like this, which appears after this answer is submitted.
', + isAnswerRange: false, tolerance: '5', }, { id: 'B', title: '200', correct: true, + isAnswerRange: false, selectedFeedback: 'You can specify optional feedback like this, which appears after this answer is submitted.
', }, ], diff --git a/src/editors/data/redux/problem/reducers.js b/src/editors/data/redux/problem/reducers.js index 38aafa91e..5642f81e2 100644 --- a/src/editors/data/redux/problem/reducers.js +++ b/src/editors/data/redux/problem/reducers.js @@ -85,8 +85,24 @@ const problem = createSlice({ deleteAnswer: (state, { payload }) => { const { id, correct } = payload; if (state.answers.length <= 1) { + if (state.answers.length > 0 && state.answers[0].isAnswerRange) { + return { + ...state, + correctAnswerCount: 1, + answers: [{ + id: 'A', + title: '', + selectedFeedback: '', + unselectedFeedback: '', + correct: state.problemType === ProblemTypeKeys.NUMERIC, + isAnswerRange: false, + }, + ], + }; + } return state; } + let { correctAnswerCount } = state; if (correct) { correctAnswerCount -= 1; @@ -115,6 +131,7 @@ const problem = createSlice({ selectedFeedback: '', unselectedFeedback: '', correct: state.problemType === ProblemTypeKeys.NUMERIC, + isAnswerRange: false, }; let { correctAnswerCount } = state; if (state.problemType === ProblemTypeKeys.NUMERIC) { @@ -131,6 +148,24 @@ const problem = createSlice({ answers, }; }, + addAnswerRange: (state) => { + // As you may only have one answer range at a time, overwrite the answer object. + const newOption = { + id: 'A', + title: '', + selectedFeedback: '', + unselectedFeedback: '', + correct: state.problemType === ProblemTypeKeys.NUMERIC, + isAnswerRange: true, + }; + const correctAnswerCount = 1; + return { + ...state, + correctAnswerCount, + answers: [newOption], + }; + }, + updateSettings: (state, { payload }) => ({ ...state, settings: { diff --git a/src/editors/data/redux/problem/reducers.test.js b/src/editors/data/redux/problem/reducers.test.js index 2b3b003b7..4466eb852 100644 --- a/src/editors/data/redux/problem/reducers.test.js +++ b/src/editors/data/redux/problem/reducers.test.js @@ -56,6 +56,7 @@ describe('problem reducer', () => { correct: false, selectedFeedback: '', title: '', + isAnswerRange: false, unselectedFeedback: '', }; expect(reducer(testingState, actions.addAnswer(answer))).toEqual({ @@ -91,6 +92,7 @@ describe('problem reducer', () => { correct: false, selectedFeedback: '', title: '', + isAnswerRange: false, unselectedFeedback: '', }; it('sets answers', () => { @@ -116,6 +118,26 @@ describe('problem reducer', () => { }); }); }); + + describe('addAnswerRange', () => { + const answerRange = { + id: 'A', + correct: true, + selectedFeedback: '', + title: '', + isAnswerRange: true, + unselectedFeedback: '', + }; + it('sets answerRange', () => { + expect(reducer({ ...testingState, problemType: ProblemTypeKeys.NUMERIC }, actions.addAnswerRange())).toEqual({ + ...testingState, + correctAnswerCount: 1, + problemType: ProblemTypeKeys.NUMERIC, + answers: [answerRange], + }); + }); + }); + describe('updateAnswer', () => { it('sets answers, as well as setting the correctAnswerCount ', () => { const answer = { id: 'A', correct: true }; @@ -162,6 +184,39 @@ describe('problem reducer', () => { }], }); }); + it('if you delete an answer range, it will be replaced with a blank answer', () => { + const answer = { + id: 'A', + correct: true, + selectedFeedback: '', + title: '', + isAnswerRange: false, + unselectedFeedback: '', + }; + const answerRange = { + id: 'A', + correct: false, + selectedFeedback: '', + title: '', + isAnswerRange: true, + unselectedFeedback: '', + }; + + expect(reducer( + { + ...testingState, + problemType: ProblemTypeKeys.NUMERIC, + correctAnswerCount: 1, + answers: [{ ...answerRange }], + }, + actions.deleteAnswer(answer), + )).toEqual({ + ...testingState, + problemType: ProblemTypeKeys.NUMERIC, + correctAnswerCount: 1, + answers: [{ ...answer }], + }); + }); }); }); });