From 0cd6b6bac0400a7116c4957ffd7e3123c502f395 Mon Sep 17 00:00:00 2001 From: Farhanam76 Date: Mon, 11 Nov 2024 15:37:51 +0000 Subject: [PATCH 1/4] removed leading and trailing spaces in Qcodes and value --- eq-author/src/App/qcodes/QCodesTable/index.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/eq-author/src/App/qcodes/QCodesTable/index.js b/eq-author/src/App/qcodes/QCodesTable/index.js index b9238cdd24..d92bde906e 100644 --- a/eq-author/src/App/qcodes/QCodesTable/index.js +++ b/eq-author/src/App/qcodes/QCodesTable/index.js @@ -226,7 +226,7 @@ const Row = memo((props) => { }${listAnswerType === ANOTHER ? "-another" : ""}-test-input`} value={qCode || ""} // Ensure the input always has a value (empty string if qCode is null or undefined) onChange={(e) => setQcode(e.value)} - onBlur={() => handleBlur(qCode)} + onBlur={() => handleBlur(qCode.trim())} hasError={Boolean(errorMessage)} aria-label="QCode input field" /> @@ -249,7 +249,7 @@ const Row = memo((props) => { data-test={`${id}${secondary ? "-secondary" : ""}-test-input`} value={qCode} onChange={(e) => setQcode(e.value)} - onBlur={() => handleBlur(qCode)} + onBlur={() => handleBlur(qCode.trim())} hasError={Boolean(errorMessage)} aria-label="QCode input field" /> @@ -272,7 +272,7 @@ const Row = memo((props) => { data-test={`${id}-value-test-input`} value={value} onChange={(e) => setValue(e.value)} - onBlur={() => handleBlurOptionValue(value)} + onBlur={() => handleBlurOptionValue(value.trim())} hasError={Boolean(valueErrorMessage)} aria-label="Option Value input field" /> From 27479de6c329d3a4d255f0353ac3b755567ee800 Mon Sep 17 00:00:00 2001 From: Farhanam76 Date: Thu, 14 Nov 2024 07:42:36 +0000 Subject: [PATCH 2/4] changed the code to ensure there's no leading and trailng spaces in the function --- eq-author/src/App/qcodes/QCodesTable/index.js | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/eq-author/src/App/qcodes/QCodesTable/index.js b/eq-author/src/App/qcodes/QCodesTable/index.js index d92bde906e..fc19b796be 100644 --- a/eq-author/src/App/qcodes/QCodesTable/index.js +++ b/eq-author/src/App/qcodes/QCodesTable/index.js @@ -155,19 +155,25 @@ const Row = memo((props) => { const handleBlur = useCallback( (qCode) => { + const trimmedQcode = qCode.trim().replace(/\s+/g, " "); + setQcode(trimmedQcode); if (qCode !== initialQcode) { if (option) { - updateOption(mutationVariables({ id, qCode })); + updateOption(mutationVariables({ id, qCode: trimmedQcode })); } else if (listAnswerType === DRIVING) { // id represents the list collector page ID - updateListCollector(mutationVariables({ id, drivingQCode: qCode })); + updateListCollector( + mutationVariables({ id, drivingQCode: trimmedQcode }) + ); } else if (listAnswerType === ANOTHER) { - updateListCollector(mutationVariables({ id, anotherQCode: qCode })); + updateListCollector( + mutationVariables({ id, anotherQCode: trimmedQcode }) + ); } else { updateAnswer( mutationVariables({ id, - [secondary ? "secondaryQCode" : "qCode"]: qCode, + [secondary ? "secondaryQCode" : "qCode"]: trimmedQcode, }) ); } @@ -187,7 +193,9 @@ const Row = memo((props) => { const handleBlurOptionValue = useCallback( (value) => { - updateValue(mutationVariables({ id, value })); + const trimmedValue = value.trim().replace(/\s+/g, " "); + setValue(trimmedValue); + updateValue(mutationVariables({ id, value: trimmedValue })); }, [id, updateValue] ); @@ -226,7 +234,7 @@ const Row = memo((props) => { }${listAnswerType === ANOTHER ? "-another" : ""}-test-input`} value={qCode || ""} // Ensure the input always has a value (empty string if qCode is null or undefined) onChange={(e) => setQcode(e.value)} - onBlur={() => handleBlur(qCode.trim())} + onBlur={() => handleBlur(qCode)} hasError={Boolean(errorMessage)} aria-label="QCode input field" /> @@ -249,7 +257,7 @@ const Row = memo((props) => { data-test={`${id}${secondary ? "-secondary" : ""}-test-input`} value={qCode} onChange={(e) => setQcode(e.value)} - onBlur={() => handleBlur(qCode.trim())} + onBlur={() => handleBlur(qCode)} hasError={Boolean(errorMessage)} aria-label="QCode input field" /> @@ -272,7 +280,7 @@ const Row = memo((props) => { data-test={`${id}-value-test-input`} value={value} onChange={(e) => setValue(e.value)} - onBlur={() => handleBlurOptionValue(value.trim())} + onBlur={() => handleBlurOptionValue(value)} hasError={Boolean(valueErrorMessage)} aria-label="Option Value input field" /> From 44d1728010ae539fd71d43f2443ca199020933af Mon Sep 17 00:00:00 2001 From: Farhanam76 Date: Thu, 14 Nov 2024 10:52:20 +0000 Subject: [PATCH 3/4] added a test for option value and updated the qcode test to test the leading/trailing spaces --- .../src/App/qcodes/QCodesTable/index.test.js | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/eq-author/src/App/qcodes/QCodesTable/index.test.js b/eq-author/src/App/qcodes/QCodesTable/index.test.js index 7b51247d7b..a06a620b4d 100644 --- a/eq-author/src/App/qcodes/QCodesTable/index.test.js +++ b/eq-author/src/App/qcodes/QCodesTable/index.test.js @@ -724,14 +724,33 @@ describe("Qcode Table", () => { it("should save qCode for checkbox answer", () => { fireEvent.change(utils.getByTestId("checkbox-answer-id-test-input"), { - target: { value: "123" }, + target: { value: " test qcode " }, }); fireEvent.blur(utils.getByTestId("checkbox-answer-id-test-input")); expect(mock).toHaveBeenCalledWith({ variables: { - input: { id: "checkbox-answer-id", qCode: "123" }, + input: { id: "checkbox-answer-id", qCode: "test qcode" }, + }, + }); + }); + + it("should save option value for checkbox answer", () => { + fireEvent.change( + utils.getByTestId("checkbox-option-1-id-value-test-input"), + { + target: { value: " test option value " }, + } + ); + + fireEvent.blur( + utils.getByTestId("checkbox-option-1-id-value-test-input") + ); + + expect(mock).toHaveBeenCalledWith({ + variables: { + input: { id: "checkbox-option-1-id", value: "test option value" }, }, }); }); From e01f0c1b2801f3301a4420b5390c1a0ba9592192 Mon Sep 17 00:00:00 2001 From: Farhanam76 Date: Thu, 28 Nov 2024 10:16:27 +0000 Subject: [PATCH 4/4] updated the code to ensure it checks that value exist --- eq-author/src/App/qcodes/QCodesTable/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eq-author/src/App/qcodes/QCodesTable/index.js b/eq-author/src/App/qcodes/QCodesTable/index.js index fc19b796be..e97f1d7ef7 100644 --- a/eq-author/src/App/qcodes/QCodesTable/index.js +++ b/eq-author/src/App/qcodes/QCodesTable/index.js @@ -193,7 +193,7 @@ const Row = memo((props) => { const handleBlurOptionValue = useCallback( (value) => { - const trimmedValue = value.trim().replace(/\s+/g, " "); + const trimmedValue = value?.trim().replace(/\s+/g, " "); setValue(trimmedValue); updateValue(mutationVariables({ id, value: trimmedValue })); },