2021-04-12 13:32:25 +00:00
|
|
|
import React from "react";
|
2022-09-12 15:10:10 +00:00
|
|
|
import { render, screen } from "@testing-library/react";
|
|
|
|
|
|
2022-12-14 18:56:56 +00:00
|
|
|
import { renderWithSetup } from "test/test-utils";
|
2016-11-16 16:58:25 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
import AdminDetails from "components/forms/RegistrationForm/AdminDetails";
|
2016-11-16 16:58:25 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("AdminDetails - form", () => {
|
2022-04-08 14:35:57 +00:00
|
|
|
const onSubmitSpy = jest.fn();
|
|
|
|
|
it("renders", () => {
|
|
|
|
|
render(<AdminDetails handleSubmit={onSubmitSpy} />);
|
|
|
|
|
|
2022-12-16 18:32:10 +00:00
|
|
|
expect(screen.getByLabelText("Password")).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByLabelText("Confirm password")).toBeInTheDocument();
|
2022-04-08 14:35:57 +00:00
|
|
|
expect(
|
|
|
|
|
screen.getByRole("textbox", { name: "Full name" })
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByRole("textbox", { name: "Email" })).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByRole("button", { name: "Next" })).toBeInTheDocument();
|
2016-11-16 16:58:25 +00:00
|
|
|
});
|
|
|
|
|
|
2022-09-12 15:10:10 +00:00
|
|
|
it("validates missing fields", async () => {
|
|
|
|
|
const { user } = renderWithSetup(
|
|
|
|
|
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await user.click(screen.getByRole("button", { name: "Next" }));
|
2022-04-08 14:35:57 +00:00
|
|
|
|
|
|
|
|
expect(onSubmitSpy).not.toHaveBeenCalled();
|
|
|
|
|
expect(screen.getByText("Email must be present")).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText("Password must be present")).toBeInTheDocument();
|
|
|
|
|
expect(
|
|
|
|
|
screen.getByText("Password confirmation must be present")
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
expect(screen.getByText("Full name must be present")).toBeInTheDocument();
|
2016-11-16 16:58:25 +00:00
|
|
|
});
|
|
|
|
|
|
2022-09-12 15:10:10 +00:00
|
|
|
it("validates the email field", async () => {
|
|
|
|
|
const { user } = renderWithSetup(
|
|
|
|
|
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
|
|
|
|
|
);
|
2022-04-08 14:35:57 +00:00
|
|
|
|
2022-09-12 15:10:10 +00:00
|
|
|
await user.type(
|
2022-04-08 14:35:57 +00:00
|
|
|
screen.getByRole("textbox", { name: "Email" }),
|
|
|
|
|
"invalid-email"
|
|
|
|
|
);
|
2022-09-12 15:10:10 +00:00
|
|
|
await user.click(screen.getByRole("button", { name: "Next" }));
|
2022-04-08 14:35:57 +00:00
|
|
|
// then
|
|
|
|
|
expect(onSubmitSpy).not.toHaveBeenCalled();
|
|
|
|
|
expect(screen.getByText("Email must be a valid email")).toBeInTheDocument();
|
2016-11-16 16:58:25 +00:00
|
|
|
});
|
|
|
|
|
|
2022-09-12 15:10:10 +00:00
|
|
|
it("validates the password fields match", async () => {
|
|
|
|
|
const { user } = renderWithSetup(
|
|
|
|
|
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
|
|
|
|
|
);
|
|
|
|
|
|
2022-12-16 18:32:10 +00:00
|
|
|
await user.type(screen.getByLabelText("Password"), "p@ssw0rd");
|
|
|
|
|
await user.type(screen.getByLabelText("Confirm password"), "password123");
|
2022-09-12 15:10:10 +00:00
|
|
|
await user.click(screen.getByRole("button", { name: "Next" }));
|
2022-04-08 14:35:57 +00:00
|
|
|
// then
|
|
|
|
|
expect(onSubmitSpy).not.toHaveBeenCalled();
|
|
|
|
|
expect(
|
|
|
|
|
screen.getByText("Password confirmation does not match password")
|
|
|
|
|
).toBeInTheDocument();
|
2016-11-16 16:58:25 +00:00
|
|
|
});
|
|
|
|
|
|
2025-08-26 21:59:05 +00:00
|
|
|
it("validates the password is not too long", async () => {
|
|
|
|
|
const { user } = renderWithSetup(
|
|
|
|
|
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await user.type(
|
|
|
|
|
screen.getByLabelText("Password"),
|
|
|
|
|
"asasasasasasasasasasasasasasasasasasasasasasasas1!"
|
|
|
|
|
);
|
|
|
|
|
await user.type(
|
|
|
|
|
screen.getByLabelText("Confirm password"),
|
|
|
|
|
"asasasasasasasasasasasasasasasasasasasasasasasas1!"
|
|
|
|
|
);
|
|
|
|
|
await user.click(screen.getByRole("button", { name: "Next" }));
|
|
|
|
|
// then
|
|
|
|
|
expect(onSubmitSpy).not.toHaveBeenCalled();
|
|
|
|
|
expect(
|
|
|
|
|
screen.getByText("Password is over the character limit")
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-12 15:10:10 +00:00
|
|
|
it("validates the password field", async () => {
|
|
|
|
|
const { user } = renderWithSetup(
|
|
|
|
|
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-26 21:59:05 +00:00
|
|
|
await user.type(screen.getByLabelText("Password"), "invalidpassw0rd");
|
|
|
|
|
await user.type(
|
|
|
|
|
screen.getByLabelText("Confirm password"),
|
|
|
|
|
"invalidpassw0rd"
|
|
|
|
|
);
|
2022-09-12 15:10:10 +00:00
|
|
|
await user.click(screen.getByRole("button", { name: "Next" }));
|
2022-04-08 14:35:57 +00:00
|
|
|
// then
|
|
|
|
|
expect(onSubmitSpy).not.toHaveBeenCalled();
|
|
|
|
|
expect(
|
|
|
|
|
screen.getByText("Password must meet the criteria below")
|
|
|
|
|
).toBeInTheDocument();
|
|
|
|
|
});
|
2016-11-16 16:58:25 +00:00
|
|
|
|
2022-09-12 15:10:10 +00:00
|
|
|
it("submits the form when valid", async () => {
|
|
|
|
|
const { user } = renderWithSetup(
|
|
|
|
|
<AdminDetails handleSubmit={onSubmitSpy} currentPage />
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await user.type(
|
2022-04-08 14:35:57 +00:00
|
|
|
screen.getByRole("textbox", { name: "Email" }),
|
|
|
|
|
"hi@gnar.dog"
|
|
|
|
|
);
|
2022-12-16 18:32:10 +00:00
|
|
|
await user.type(screen.getByLabelText("Password"), "password123#");
|
|
|
|
|
await user.type(screen.getByLabelText("Confirm password"), "password123#");
|
2022-09-12 15:10:10 +00:00
|
|
|
await user.type(
|
2022-04-08 14:35:57 +00:00
|
|
|
screen.getByRole("textbox", { name: "Full name" }),
|
|
|
|
|
"Gnar Dog"
|
|
|
|
|
);
|
2022-09-12 15:10:10 +00:00
|
|
|
await user.click(screen.getByRole("button", { name: "Next" }));
|
2022-04-08 14:35:57 +00:00
|
|
|
// then
|
|
|
|
|
expect(onSubmitSpy).toHaveBeenCalledWith({
|
|
|
|
|
email: "hi@gnar.dog",
|
|
|
|
|
name: "Gnar Dog",
|
2022-05-18 17:03:00 +00:00
|
|
|
password: "password123#",
|
|
|
|
|
password_confirmation: "password123#",
|
2016-11-16 16:58:25 +00:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|