diff --git a/eq-author/src/App/qcodes/QCodesTable/index.js b/eq-author/src/App/qcodes/QCodesTable/index.js index b9238cdd24..e97f1d7ef7 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] ); 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" }, }, }); });