Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated email regex to support non-standard TLDs #87

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/Email/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ const Email = ({ fieldData, name, labelFor, ...wrapProps }) => {
{...register(name, {
required: isRequired && (errorMessage || strings.errors.required),
pattern: {
value: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
value:
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
message: getFieldError(
{ ...fieldData, inputMaskValue: true },
strings
Expand Down
87 changes: 56 additions & 31 deletions tests/components/fields/Email.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,48 @@ describe("Email field", () => {
expect(element).toBeInTheDocument();
});

it("submits form when value is correct", async () => {
const { container } = renderGravityForm({
data: {
gfForm: { formFields: { nodes: [field] } },
},
});
const emailCases = [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
];

describe("submits form when value is correct", () => {
test.each(emailCases)("given %p", async (email) => {
const { container } = renderGravityForm({
data: {
gfForm: { formFields: { nodes: [field] } },
},
});

fireEvent.input(screen.getByLabelText(/Email/i), {
target: {
value: "[email protected]",
},
});
await act(async () => {
fireEvent.submit(screen.getByRole("button"));
});
fireEvent.input(screen.getByLabelText(/Email/i), {
target: {
value: email,
},
});
await act(async () => {
fireEvent.submit(screen.getByRole("button"));
});

expect(submitGravityForm).toBeCalledWith({
id: mockFormData.gfForm.databaseId,
fieldValues: [
{
emailValues: {
value: "[email protected]",
expect(submitGravityForm).toBeCalledWith({
id: mockFormData.gfForm.databaseId,
fieldValues: [
{
emailValues: {
value: email,
},
id: field.id,
},
id: field.id,
},
],
});
],
});

expect(
container.querySelector(`.gravityform__error_message`)
).not.toBeInTheDocument();
expect(
container.querySelector(`.gravityform__error_message`)
).not.toBeInTheDocument();
});
});

describe("render errors", () => {
Expand All @@ -108,11 +119,27 @@ describe("Email field", () => {

expect(submitGravityForm).not.toBeCalled();
});
});

const invalidEmails = ["test", "[email protected]"];

it("should display invalid email error when value is not valid", async () => {
describe("should display invalid email error when value is not valid", () => {
let container;
let element;
beforeEach(() => {
const rendered = renderGravityForm({
data: {
gfForm: { formFields: { nodes: [field] } },
},
});
container = rendered.container;

element = container.querySelector(`#${fieldId}`);
});
test.each(invalidEmails)("given %p", async (email) => {
fireEvent.input(screen.getByLabelText(/Email/i), {
target: {
value: "test",
value: email,
},
});

Expand All @@ -126,8 +153,6 @@ describe("Email field", () => {

expect(submitGravityForm).not.toBeCalled();
});

// it("should display error when confirmation email is set and emails don't match", async () => {});
});

// renders confirmation email
Expand Down