From ee682a793f0dc35553c24e2a849ab5a061007e58 Mon Sep 17 00:00:00 2001 From: amitbadala Date: Tue, 7 Nov 2023 17:24:06 +0530 Subject: [PATCH] Throw error if invalid nonOptionalErrorMsg, add tests for the same --- examples/for-tests/src/App.js | 10 ++++++++++ lib/build/emailpassword-shared4.js | 6 +++++- lib/ts/recipe/emailpassword/utils.ts | 6 +++++- test/end-to-end/signup.test.js | 26 ++++++++++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/examples/for-tests/src/App.js b/examples/for-tests/src/App.js index 798d87edd..7c546a35d 100644 --- a/examples/for-tests/src/App.js +++ b/examples/for-tests/src/App.js @@ -279,6 +279,12 @@ const incorrectFormFields = [ return "Please check Terms and conditions"; }, }, + { + id: "city", + label: "Your city", + optional: false, + nonOptionalErrorMsg: "", // empty string should throw error + }, ]; const customFields = [ @@ -713,6 +719,10 @@ function getFormFields() { // since page-error blocks all the other errors // use this filter to test specific error return incorrectFormFields.filter(({ id }) => id === "terms"); + } else if (localStorage.getItem("INCORRECT_NON_OPTIONAL_ERROR_MSG") === "YES") { + return incorrectFormFields.filter(({ id }) => id === "city"); + } else if (localStorage.getItem("INCORRECT_GETDEFAULT") === "YES") { + return incorrectFormFields.filter(({ id }) => id === "country"); } return incorrectFormFields; } else if (localStorage.getItem("SHOW_CUSTOM_FIELDS_WITH_DEFAULT_VALUES") === "YES") { diff --git a/lib/build/emailpassword-shared4.js b/lib/build/emailpassword-shared4.js index 6e27da1e8..6c3b1bd6d 100644 --- a/lib/build/emailpassword-shared4.js +++ b/lib/build/emailpassword-shared4.js @@ -376,6 +376,10 @@ function mergeFormFields(defaultFormFields, userFormFields) { } function getFormattedFormField(field) { var _this = this; + // Fields with the 'nonOptionalErrorMsg' property must have a valid message defined + if (field.optional === false && field.nonOptionalErrorMsg === "") { + throw new Error("nonOptionalErrorMsg for field ".concat(field.id, " cannot be empty")); + } return genericComponentOverrideContext.__assign(genericComponentOverrideContext.__assign({}, field), { validate: function (value) { return genericComponentOverrideContext.__awaiter(_this, void 0, void 0, function () { @@ -384,7 +388,7 @@ function getFormattedFormField(field) { case 0: // Absent or not optional empty field if (value === "" && field.optional === false) { - if (field.nonOptionalErrorMsg !== undefined && field.nonOptionalErrorMsg !== "") { + if (field.nonOptionalErrorMsg !== undefined) { return [2 /*return*/, field.nonOptionalErrorMsg]; } return [2 /*return*/, "ERROR_NON_OPTIONAL"]; diff --git a/lib/ts/recipe/emailpassword/utils.ts b/lib/ts/recipe/emailpassword/utils.ts index 8edc3b0e5..eba354ba4 100644 --- a/lib/ts/recipe/emailpassword/utils.ts +++ b/lib/ts/recipe/emailpassword/utils.ts @@ -338,12 +338,16 @@ export function mergeFormFields( } export function getFormattedFormField(field: NormalisedFormField): NormalisedFormField { + // Fields with the 'nonOptionalErrorMsg' property must have a valid message defined + if (field.optional === false && field.nonOptionalErrorMsg === "") { + throw new Error(`nonOptionalErrorMsg for field ${field.id} cannot be empty`); + } return { ...field, validate: async (value: any): Promise => { // Absent or not optional empty field if (value === "" && field.optional === false) { - if (field.nonOptionalErrorMsg !== undefined && field.nonOptionalErrorMsg !== "") { + if (field.nonOptionalErrorMsg !== undefined) { return field.nonOptionalErrorMsg; } return "ERROR_NON_OPTIONAL"; diff --git a/test/end-to-end/signup.test.js b/test/end-to-end/signup.test.js index d0494e405..9b90f704a 100644 --- a/test/end-to-end/signup.test.js +++ b/test/end-to-end/signup.test.js @@ -706,6 +706,7 @@ describe("SuperTokens SignUp", function () { }); it("Check if incorrect getDefaultValue throws error", async function () { + await page.evaluate(() => window.localStorage.setItem("INCORRECT_GETDEFAULT", "YES")); let pageErrorMessage = ""; page.on("pageerror", (err) => { pageErrorMessage = err.message; @@ -724,6 +725,7 @@ describe("SuperTokens SignUp", function () { }); it("Check if non-string params to onChange throws error", async function () { + await page.evaluate(() => window.localStorage.removeItem("INCORRECT_GETDEFAULT")); await page.evaluate(() => window.localStorage.setItem("INCORRECT_ONCHANGE", "YES")); await page.reload({ waitUntil: "domcontentloaded", @@ -745,6 +747,30 @@ describe("SuperTokens SignUp", function () { `Expected "${expectedErrorMessage}" to be included in page-error` ); }); + + it("Check if empty string for nonOptionalErrorMsg throws error", async function () { + const expectedErrorMessage = "nonOptionalErrorMsg for field city cannot be empty"; + let pageErrorMessage = ""; + page.on("pageerror", (err) => { + pageErrorMessage = err.message; + }); + + await page.evaluate(() => window.localStorage.removeItem("INCORRECT_GETDEFAULT")); + await page.evaluate(() => window.localStorage.removeItem("INCORRECT_ONCHANGE")); + await page.evaluate(() => window.localStorage.setItem("INCORRECT_NON_OPTIONAL_ERROR_MSG", "YES")); + await page.reload({ + waitUntil: "domcontentloaded", + }); + + if (pageErrorMessage !== "") { + assert( + pageErrorMessage.includes(expectedErrorMessage), + `Expected "${expectedErrorMessage}" to be included in page-error` + ); + } else { + throw "Empty nonOptionalErrorMsg should throw error"; + } + }); }); });