2021-04-12 13:32:25 +00:00
|
|
|
import React from "react";
|
2022-02-18 21:30:50 +00:00
|
|
|
import { fireEvent, render, screen } from "@testing-library/react";
|
2022-09-12 15:10:10 +00:00
|
|
|
|
2022-12-14 18:56:56 +00:00
|
|
|
import { renderWithSetup } from "test/test-utils";
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
import UserSettingsForm from "components/forms/UserSettingsForm";
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("UserSettingsForm - component", () => {
|
2017-02-13 23:19:00 +00:00
|
|
|
const defaultProps = {
|
2022-02-18 21:30:50 +00:00
|
|
|
handleSubmit: jest.fn(),
|
|
|
|
|
onCancel: jest.fn(),
|
2017-02-13 23:19:00 +00:00
|
|
|
};
|
|
|
|
|
|
2022-02-18 21:30:50 +00:00
|
|
|
it("renders correctly", () => {
|
|
|
|
|
render(<UserSettingsForm {...defaultProps} />);
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
screen.getByRole("textbox", { name: /email \(required\)/i })
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
expect(
|
|
|
|
|
screen.getByRole("textbox", { name: /full name \(required\)/i })
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByRole("button", { name: "Update" })).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByRole("button", { name: "Cancel" })).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should pass validation checks for input fields", async () => {
|
|
|
|
|
render(<UserSettingsForm {...defaultProps} />);
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2022-02-18 21:30:50 +00:00
|
|
|
// when
|
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Update" }));
|
|
|
|
|
// then
|
|
|
|
|
expect(defaultProps.handleSubmit).not.toHaveBeenCalled();
|
|
|
|
|
expect(
|
|
|
|
|
await screen.findByText("Email field must be completed")
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
expect(
|
|
|
|
|
await screen.findByText("Full name field must be completed")
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should throw validation error when invalid email is entered", async () => {
|
2022-09-12 15:10:10 +00:00
|
|
|
const { user } = renderWithSetup(
|
|
|
|
|
<UserSettingsForm {...{ ...defaultProps, smtpConfigured: true }} />
|
|
|
|
|
);
|
2022-02-18 21:30:50 +00:00
|
|
|
|
2022-09-12 15:10:10 +00:00
|
|
|
await user.type(
|
2022-02-18 21:30:50 +00:00
|
|
|
screen.getByRole("textbox", { name: /email \(required\)/i }),
|
|
|
|
|
"invalid-email"
|
|
|
|
|
);
|
2022-09-12 15:10:10 +00:00
|
|
|
await user.click(screen.getByRole("button", { name: "Update" }));
|
|
|
|
|
|
|
|
|
|
const emailError = screen.getByText("invalid-email is not a valid email");
|
|
|
|
|
|
2022-02-18 21:30:50 +00:00
|
|
|
expect(defaultProps.handleSubmit).not.toHaveBeenCalled();
|
2022-09-12 15:10:10 +00:00
|
|
|
expect(emailError).toBeInTheDocument();
|
2016-12-13 15:24:58 +00:00
|
|
|
});
|
|
|
|
|
|
2022-09-12 15:10:10 +00:00
|
|
|
it("calls the handleSubmit props with form data", async () => {
|
2021-04-12 13:32:25 +00:00
|
|
|
const expectedFormData = {
|
|
|
|
|
email: "email@example.com",
|
|
|
|
|
name: "Jim Example",
|
|
|
|
|
};
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2022-09-12 15:10:10 +00:00
|
|
|
const { user } = renderWithSetup(
|
|
|
|
|
<UserSettingsForm {...{ ...defaultProps, smtpConfigured: true }} />
|
|
|
|
|
);
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2022-09-12 15:10:10 +00:00
|
|
|
await user.type(
|
2022-02-18 21:30:50 +00:00
|
|
|
screen.getByRole("textbox", { name: /email \(required\)/i }),
|
|
|
|
|
expectedFormData.email
|
|
|
|
|
);
|
2022-09-12 15:10:10 +00:00
|
|
|
await user.type(
|
2022-02-18 21:30:50 +00:00
|
|
|
screen.getByRole("textbox", { name: /full name \(required\)/i }),
|
|
|
|
|
expectedFormData.name
|
|
|
|
|
);
|
2022-09-12 15:10:10 +00:00
|
|
|
await user.click(screen.getByRole("button", { name: "Update" }));
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2022-02-18 21:30:50 +00:00
|
|
|
expect(defaultProps.handleSubmit).toHaveBeenCalledWith(expectedFormData);
|
2016-12-13 15:24:58 +00:00
|
|
|
});
|
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("initializes the form with the users data", () => {
|
|
|
|
|
const user = {
|
|
|
|
|
email: "email@example.com",
|
|
|
|
|
name: "Jim Example",
|
|
|
|
|
};
|
2017-02-13 23:19:00 +00:00
|
|
|
const props = { ...defaultProps, formData: user };
|
2016-12-13 15:24:58 +00:00
|
|
|
|
2022-02-18 21:30:50 +00:00
|
|
|
render(<UserSettingsForm {...props} />);
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
screen.getByRole("textbox", { name: /email \(required\)/i })
|
|
|
|
|
).toHaveValue(user.email);
|
|
|
|
|
expect(
|
|
|
|
|
screen.getByRole("textbox", { name: /full name \(required\)/i })
|
|
|
|
|
).toHaveValue(user.name);
|
2016-12-13 15:24:58 +00:00
|
|
|
});
|
|
|
|
|
});
|