2021-04-12 13:32:25 +00:00
|
|
|
import React from "react";
|
2022-03-21 16:27:50 +00:00
|
|
|
import { fireEvent, render, screen } from "@testing-library/react";
|
2016-12-29 20:27:43 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
import ConfirmInviteForm from "components/forms/ConfirmInviteForm";
|
2022-03-21 16:27:50 +00:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2016-12-29 20:27:43 +00:00
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("ConfirmInviteForm - component", () => {
|
2020-12-01 18:15:12 +00:00
|
|
|
const handleSubmitSpy = jest.fn();
|
2021-04-12 13:32:25 +00:00
|
|
|
const inviteToken = "abc123";
|
2016-12-29 20:27:43 +00:00
|
|
|
const formData = { invite_token: inviteToken };
|
2021-04-12 13:32:25 +00:00
|
|
|
|
|
|
|
it("renders", () => {
|
2022-03-21 16:27:50 +00:00
|
|
|
render(
|
|
|
|
<ConfirmInviteForm formData={formData} handleSubmit={handleSubmitSpy} />
|
|
|
|
);
|
|
|
|
expect(
|
|
|
|
screen.getByRole("textbox", { name: "Full name" })
|
|
|
|
).toBeInTheDocument();
|
|
|
|
expect(screen.getByLabelText("Password")).toBeInTheDocument();
|
|
|
|
expect(screen.getByLabelText("Confirm password")).toBeInTheDocument();
|
|
|
|
expect(screen.getByRole("button", { name: "Submit" })).toBeInTheDocument();
|
2016-12-29 20:27:43 +00:00
|
|
|
});
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("renders the base error", () => {
|
|
|
|
const baseError = "Unable to authenticate the current user";
|
2022-03-21 16:27:50 +00:00
|
|
|
render(
|
2021-04-12 13:32:25 +00:00
|
|
|
<ConfirmInviteForm
|
|
|
|
serverErrors={{ base: baseError }}
|
2022-03-21 16:27:50 +00:00
|
|
|
handleSubmit={handleSubmitSpy}
|
2021-04-12 13:32:25 +00:00
|
|
|
/>
|
|
|
|
);
|
2017-01-06 00:01:17 +00:00
|
|
|
|
2022-03-21 16:27:50 +00:00
|
|
|
expect(screen.getByText(baseError)).toBeInTheDocument();
|
2017-01-06 00:01:17 +00:00
|
|
|
});
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
it("calls the handleSubmit prop with the invite_token when valid", () => {
|
2022-03-21 16:27:50 +00:00
|
|
|
render(
|
|
|
|
<ConfirmInviteForm formData={formData} handleSubmit={handleSubmitSpy} />
|
|
|
|
);
|
|
|
|
// when
|
|
|
|
userEvent.type(
|
|
|
|
screen.getByRole("textbox", { name: "Full name" }),
|
|
|
|
"Gnar Dog"
|
|
|
|
);
|
|
|
|
userEvent.type(screen.getByLabelText("Password"), "p@ssw0rd");
|
|
|
|
userEvent.type(screen.getByLabelText("Confirm password"), "p@ssw0rd");
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Submit" }));
|
|
|
|
// then
|
2016-12-29 20:27:43 +00:00
|
|
|
expect(handleSubmitSpy).toHaveBeenCalledWith({
|
|
|
|
...formData,
|
2021-04-12 13:32:25 +00:00
|
|
|
name: "Gnar Dog",
|
|
|
|
password: "p@ssw0rd",
|
|
|
|
password_confirmation: "p@ssw0rd",
|
2016-12-29 20:27:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("name input", () => {
|
2022-03-21 16:27:50 +00:00
|
|
|
it("validates the field must be present", async () => {
|
|
|
|
render(
|
|
|
|
<ConfirmInviteForm formData={formData} handleSubmit={handleSubmitSpy} />
|
|
|
|
);
|
|
|
|
// when
|
|
|
|
userEvent.type(screen.getByRole("textbox", { name: "Full name" }), "");
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Submit" }));
|
|
|
|
// then
|
|
|
|
expect(
|
|
|
|
await screen.findByText("Full name must be present")
|
|
|
|
).toBeInTheDocument();
|
2016-12-29 20:27:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("password input", () => {
|
2022-03-21 16:27:50 +00:00
|
|
|
it("validates the field must be present", async () => {
|
|
|
|
render(
|
|
|
|
<ConfirmInviteForm formData={formData} handleSubmit={handleSubmitSpy} />
|
|
|
|
);
|
|
|
|
// when
|
|
|
|
userEvent.type(screen.getByLabelText("Password"), "");
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Submit" }));
|
|
|
|
// then
|
|
|
|
expect(
|
|
|
|
await screen.findByText("Password must be present")
|
|
|
|
).toBeInTheDocument();
|
2016-12-29 20:27:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-04-12 13:32:25 +00:00
|
|
|
describe("password_confirmation input", () => {
|
2022-03-21 16:27:50 +00:00
|
|
|
it("validates the password_confirmation matches the password", async () => {
|
|
|
|
render(
|
|
|
|
<ConfirmInviteForm formData={formData} handleSubmit={handleSubmitSpy} />
|
|
|
|
);
|
|
|
|
|
|
|
|
// when
|
|
|
|
userEvent.type(screen.getByLabelText("Password"), "p@ssw0rd");
|
|
|
|
userEvent.type(
|
|
|
|
screen.getByLabelText("Confirm password"),
|
|
|
|
"another password"
|
|
|
|
);
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Submit" }));
|
|
|
|
// then
|
|
|
|
expect(
|
|
|
|
await screen.findByText("Password confirmation does not match password")
|
|
|
|
).toBeInTheDocument();
|
2016-12-29 20:27:43 +00:00
|
|
|
});
|
|
|
|
|
2022-03-21 16:27:50 +00:00
|
|
|
it("validates the field must be present", async () => {
|
|
|
|
render(
|
|
|
|
<ConfirmInviteForm formData={formData} handleSubmit={handleSubmitSpy} />
|
|
|
|
);
|
|
|
|
// when
|
|
|
|
userEvent.type(screen.getByLabelText("Confirm password"), "");
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Submit" }));
|
|
|
|
// then
|
|
|
|
expect(
|
|
|
|
await screen.findByText("Password confirmation must be present")
|
|
|
|
).toBeInTheDocument();
|
2016-12-29 20:27:43 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|