From bb553c553c7c8d7267ef43b8a87a90f5782a9eff Mon Sep 17 00:00:00 2001 From: Farhaan Bukhsh Date: Mon, 21 Oct 2024 18:17:36 +0530 Subject: [PATCH] fix: Fixes the saving pop-up to appear Moves the array updation statement and introduces two ways of updating the grading statement. Signed-off-by: Farhaan Bukhsh --- .../grading-scale/GradingScale.jsx | 58 ++++++++++--------- .../grading-scale/GradingScale.test.jsx | 29 ++++++++++ .../components/GradingScaleSegment.jsx | 2 +- 3 files changed, 61 insertions(+), 28 deletions(-) diff --git a/src/grading-settings/grading-scale/GradingScale.jsx b/src/grading-settings/grading-scale/GradingScale.jsx index 2e610cbc42..d4d7d21dfb 100644 --- a/src/grading-settings/grading-scale/GradingScale.jsx +++ b/src/grading-settings/grading-scale/GradingScale.jsx @@ -55,40 +55,44 @@ const GradingScale = ({ }, [gradingSegments, letters]); const addNewGradingSegment = () => { - setGradingSegments(prevSegments => { + setGradingSegments((prevSegments) => { + let updatedGradingSegment = []; if (prevSegments.length >= 5) { const segSize = MAXIMUM_SCALE_LENGTH / (prevSegments.length + 1); - return Array.from({ length: prevSegments.length + 1 }).map((_, i) => ( - { - current: 100 - i * segSize, - previous: 100 - (i + 1) * segSize, - } - )); - } - const firstSegment = prevSegments[prevSegments.length - 1]; - const secondSegment = prevSegments[prevSegments.length - 2]; - const newCurrentValue = Math.ceil((secondSegment.current - secondSegment.previous) / 2); - - const newSegment = { - current: (firstSegment.current + newCurrentValue), - previous: firstSegment.current, - }; + updatedGradingSegment = Array.from({ + length: prevSegments.length + 1, + }).map((_, i) => ({ + current: 100 - i * segSize, + previous: 100 - (i + 1) * segSize, + })); + } else { + const firstSegment = prevSegments[prevSegments.length - 1]; + const secondSegment = prevSegments[prevSegments.length - 2]; + const newCurrentValue = Math.ceil( + (secondSegment.current - secondSegment.previous) / 2, + ); + + const newSegment = { + current: firstSegment.current + newCurrentValue, + previous: firstSegment.current, + }; - const updatedSecondSegment = { - ...secondSegment, - previous: (firstSegment.current + newCurrentValue), - }; + const updatedSecondSegment = { + ...secondSegment, + previous: firstSegment.current + newCurrentValue, + }; + updatedGradingSegment = [ + ...prevSegments.slice(0, prevSegments.length - 2), + updatedSecondSegment, + newSegment, + firstSegment, + ]; + } showSavePrompt(true); setShowSuccessAlert(false); setOverrideInternetConnectionAlert(false); - - return [ - ...prevSegments.slice(0, prevSegments.length - 2), - updatedSecondSegment, - newSegment, - firstSegment, - ]; + return updatedGradingSegment; }); const nextIndex = (letters.length % defaultGradeDesignations.length); diff --git a/src/grading-settings/grading-scale/GradingScale.test.jsx b/src/grading-settings/grading-scale/GradingScale.test.jsx index f311c1757f..276ad07ec2 100644 --- a/src/grading-settings/grading-scale/GradingScale.test.jsx +++ b/src/grading-settings/grading-scale/GradingScale.test.jsx @@ -123,4 +123,33 @@ describe('', () => { expect(segmentInputs[1]).toHaveValue('Test'); }); }); + + it('should render GradingScale component with more than 5 grades', async () => { + const { getAllByTestId } = render( + + + , + ); + await waitFor(() => { + const addNewSegmentBtn = getAllByTestId('grading-scale-btn-add-segment'); + expect(addNewSegmentBtn[0]).toBeInTheDocument(); + fireEvent.click(addNewSegmentBtn[0]); + const segments = getAllByTestId('grading-scale-segment-number'); + // Calculation is based on 100/6 i.e A, B, C, D, E, F which comes to 16.666666666666657 + expect(segments[0].textContent).toEqual('83.33333333333333 - 100'); + expect(segments[6].textContent).toEqual('0 - 15.666666666666657'); + }); + }); }); diff --git a/src/grading-settings/grading-scale/components/GradingScaleSegment.jsx b/src/grading-settings/grading-scale/components/GradingScaleSegment.jsx index 364814768a..62d78e5644 100644 --- a/src/grading-settings/grading-scale/components/GradingScaleSegment.jsx +++ b/src/grading-settings/grading-scale/components/GradingScaleSegment.jsx @@ -49,7 +49,7 @@ const GradingScaleSegment = ({ disabled={idx === gradingSegments.length} /> )} - + {gradingSegments[idx === 0 ? 0 : idx - 1]?.previous} - {value === 100 ? value : value - 1}