Skip to content

Commit

Permalink
Throw error if invalid nonOptionalErrorMsg, add tests for the same
Browse files Browse the repository at this point in the history
  • Loading branch information
amitbadala committed Nov 7, 2023
1 parent a14b2da commit ee682a7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
10 changes: 10 additions & 0 deletions examples/for-tests/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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") {
Expand Down
6 changes: 5 additions & 1 deletion lib/build/emailpassword-shared4.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion lib/ts/recipe/emailpassword/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined> => {
// 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";
Expand Down
26 changes: 26 additions & 0 deletions test/end-to-end/signup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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",
Expand All @@ -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";
}
});
});
});

Expand Down

0 comments on commit ee682a7

Please sign in to comment.