From 594e0e3dd421558c6e15c7066e4b12f0cc961389 Mon Sep 17 00:00:00 2001 From: Dima Alipov Date: Mon, 25 Mar 2024 21:35:57 +0200 Subject: [PATCH] fix: incorrect field validation on the grading page --- .../AssignmentSection.test.jsx | 43 +++++++++++++++---- .../assignments/AssignmentItem.jsx | 17 ++++---- .../assignment-section/utils/validation.js | 12 ++++-- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/grading-settings/assignment-section/AssignmentSection.test.jsx b/src/grading-settings/assignment-section/AssignmentSection.test.jsx index 1761470a67..d1462fa09e 100644 --- a/src/grading-settings/assignment-section/AssignmentSection.test.jsx +++ b/src/grading-settings/assignment-section/AssignmentSection.test.jsx @@ -9,7 +9,7 @@ const testObj = {}; const defaultAssignments = { type: 'Test type', - minCount: 1, + minCount: 2, dropCount: 1, shortLabel: 'TT', weight: 100, @@ -59,6 +59,20 @@ describe('', () => { fireEvent.change(assignmentShortLabelInput, { target: { value: '123' } }); expect(testObj.graders[0].shortLabel).toBe('123'); }); + it('checking correct assignmentTypeNameTitle value', () => { + const { getByTestId } = render(); + const assignmentShortLabelInput = getByTestId('assignment-type-name-input'); + expect(assignmentShortLabelInput.value).toBe('Test type'); + fireEvent.change(assignmentShortLabelInput, { target: { value: 'New Test Type' } }); + expect(testObj.graders[0].type).toBe('New Test Type'); + }); + it('checking invalid assignmentTypeNameTitle value', () => { + const { getByText, getByTestId } = render(); + const assignmentShortLabelInput = getByTestId('assignment-type-name-input'); + expect(assignmentShortLabelInput.value).toBe('Test type'); + fireEvent.change(assignmentShortLabelInput, { target: { value: ' ' } }); + expect(getByText(messages.assignmentTypeNameErrorMessage1.defaultMessage)).toBeInTheDocument(); + }); it('checking correct assignment weight of total grade value', async () => { const { getByTestId } = render(); await waitFor(() => { @@ -72,7 +86,7 @@ describe('', () => { const { getByTestId } = render(); await waitFor(() => { const assignmentTotalNumberInput = getByTestId('assignment-minCount-input'); - expect(assignmentTotalNumberInput.value).toBe('1'); + expect(assignmentTotalNumberInput.value).toBe('2'); fireEvent.change(assignmentTotalNumberInput, { target: { value: '123' } }); expect(testObj.graders[0].minCount).toBe(123); }); @@ -82,26 +96,39 @@ describe('', () => { await waitFor(() => { const assignmentNumberOfDroppableInput = getByTestId('assignment-dropCount-input'); expect(assignmentNumberOfDroppableInput.value).toBe('1'); - fireEvent.change(assignmentNumberOfDroppableInput, { target: { value: '2' } }); - expect(testObj.graders[0].dropCount).toBe(2); + fireEvent.change(assignmentNumberOfDroppableInput, { target: { value: '0' } }); + expect(testObj.graders[0].dropCount).toBe(0); }); }); - it('checking correct error msg if dropCount have negative number or empty string', async () => { + it('checking correct error msg if dropCount is empty or negative integer', async () => { const { getByText, getByTestId } = render(); await waitFor(() => { const assignmentNumberOfDroppableInput = getByTestId('assignment-dropCount-input'); expect(assignmentNumberOfDroppableInput.value).toBe('1'); - fireEvent.change(assignmentNumberOfDroppableInput, { target: { value: '-2' } }); - expect(getByText(messages.numberOfDroppableErrorMessage.defaultMessage)).toBeInTheDocument(); fireEvent.change(assignmentNumberOfDroppableInput, { target: { value: '' } }); expect(getByText(messages.numberOfDroppableErrorMessage.defaultMessage)).toBeInTheDocument(); + fireEvent.change(assignmentNumberOfDroppableInput, { target: { value: '-5' } }); + expect(getByText(messages.numberOfDroppableErrorMessage.defaultMessage)).toBeInTheDocument(); + }); + }); + it('checking correct error msg if dropCount more than minCount', async () => { + const { getByTestId } = render(); + const assignmentMinCountInput = getByTestId('assignment-minCount-input'); + expect(assignmentMinCountInput.value).toBe('2'); + const assignmentNumberOfDroppableInput = getByTestId('assignment-dropCount-input'); + expect(assignmentNumberOfDroppableInput.value).toBe('1'); + expect(assignmentNumberOfDroppableInput.classList).not.toContain('is-invalid'); + await waitFor(() => { + fireEvent.change(assignmentNumberOfDroppableInput, { target: { value: '50' } }); + const dI = getByTestId('assignment-dropCount-input'); + expect(dI.classList).toContain('is-invalid'); }); }); it('checking correct error msg if minCount have negative number or empty string', async () => { const { getByText, getByTestId } = render(); await waitFor(() => { const assignmentMinCountInput = getByTestId('assignment-minCount-input'); - expect(assignmentMinCountInput.value).toBe('1'); + expect(assignmentMinCountInput.value).toBe('2'); fireEvent.change(assignmentMinCountInput, { target: { value: '-2' } }); expect(getByText(messages.totalNumberErrorMessage.defaultMessage)).toBeInTheDocument(); fireEvent.change(assignmentMinCountInput, { target: { value: '' } }); diff --git a/src/grading-settings/assignment-section/assignments/AssignmentItem.jsx b/src/grading-settings/assignment-section/assignments/AssignmentItem.jsx index 68438c3adf..2b7fe5fc68 100644 --- a/src/grading-settings/assignment-section/assignments/AssignmentItem.jsx +++ b/src/grading-settings/assignment-section/assignments/AssignmentItem.jsx @@ -42,14 +42,15 @@ const AssignmentItem = ({ {descriptions} {errorEffort && ( - - {errorMsg} - - )} - {gradeField?.dropCount !== 0 && gradeField?.dropCount > gradeField?.minCount && ( - - {secondErrorMsg} - + gradeField?.dropCount ? ( + + {gradeField?.dropCount !== 0 && gradeField?.dropCount >= gradeField?.minCount ? secondErrorMsg : errorMsg} + + ) : ( + + {errorMsg} + + ) )} diff --git a/src/grading-settings/assignment-section/utils/validation.js b/src/grading-settings/assignment-section/utils/validation.js index 523a1c3806..9711d21719 100644 --- a/src/grading-settings/assignment-section/utils/validation.js +++ b/src/grading-settings/assignment-section/utils/validation.js @@ -51,10 +51,11 @@ export const validationAssignmentFields = ( assignmentDropCount, ) => { const courseGradingTypes = courseGraders?.map(grade => grade.type); + const minCountValue = courseGraders?.find(grade => grade.id === assignmentId).minCount; switch (assignmentName) { case assignmentType: - if (assignmentValue === '') { + if (assignmentValue.trim() === '') { updateAssignmentErrorList(assignmentName, assignmentId, setErrorList, setShowSavePrompt); return; } @@ -77,7 +78,7 @@ export const validationAssignmentFields = ( ); break; case weightOfTotalGrade: - if (assignmentValue < 0 || assignmentValue > 100 || assignmentValue === '-0') { + if (assignmentValue === '' || assignmentValue < 0 || assignmentValue > 100 || assignmentValue === '-0') { updateAssignmentErrorList( assignmentName, assignmentId, @@ -113,7 +114,12 @@ export const validationAssignmentFields = ( ); break; case assignmentDropCount: - if (assignmentValue < 0 || assignmentValue === '' || assignmentValue === '-0') { + if ( + assignmentValue >= minCountValue + || assignmentValue < 0 + || assignmentValue === '' + || assignmentValue === '-0' + ) { updateAssignmentErrorList( assignmentName, assignmentId,