fleet/frontend/components/forms/ConfirmInviteForm/ConfirmInviteForm.tests.tsx
jacobshandling 55fd95d760
UI – Updates to confirm invite flow (#25583)
## For #24486 

- Check invite validity before rendering form, error if invalid
- Use data returned from validity check to pre-populate form
- Remove dependence of flow on URL params other than token
- Remove other URL params from link generated in invite confirmation
email
- Refactor form from JS to TS
- Refactor form from class to functional components
- Cleanup unused logic
- Improve error handling

**Invalid invite**

![invalid](https://github.com/user-attachments/assets/c42c47ca-6a0d-4112-89ea-68b77e748d12)


**Valid invite**

![valid-login-flow](https://github.com/user-attachments/assets/f2b97306-a1bd-47be-9725-968a3c4ad8a8)



- [x] Changes file added for user-visible changes in `changes/`
- [x] Updated tests
- [ ] A detailed QA plan exists on the associated ticket (if it isn't
there, work with the product group's QA engineer to add it)
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2025-01-24 10:55:39 -08:00

131 lines
3.8 KiB
TypeScript

import React from "react";
import { render, screen } from "@testing-library/react";
import { renderWithSetup } from "test/test-utils";
import ConfirmInviteForm from "components/forms/ConfirmInviteForm";
describe("ConfirmInviteForm - component", () => {
const handleSubmitSpy = jest.fn();
const defaultFormData = { name: "Test User" };
it("renders", () => {
render(
<ConfirmInviteForm
defaultFormData={defaultFormData}
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();
});
it("renders the base error", () => {
const baseError = "Unable to authenticate the current user";
render(
<ConfirmInviteForm
ancestorError={baseError}
handleSubmit={handleSubmitSpy}
/>
);
expect(screen.getByText(baseError)).toBeInTheDocument();
});
it("calls the handleSubmit prop when valid", async () => {
const { user } = renderWithSetup(
<ConfirmInviteForm
defaultFormData={defaultFormData}
handleSubmit={handleSubmitSpy}
/>
);
await user.type(screen.getByLabelText("Password"), "p@ssw0rd");
await user.type(screen.getByLabelText("Confirm password"), "p@ssw0rd");
await user.click(screen.getByRole("button", { name: "Submit" }));
expect(handleSubmitSpy).toHaveBeenCalledWith({
...defaultFormData,
password: "p@ssw0rd",
password_confirmation: "p@ssw0rd",
});
});
describe("name input", () => {
it("validates the field must be present", async () => {
const { user } = renderWithSetup(
<ConfirmInviteForm
defaultFormData={{ ...defaultFormData, ...{ name: "" } }}
handleSubmit={handleSubmitSpy}
/>
);
await user.click(screen.getByRole("button", { name: "Submit" }));
expect(
await screen.findByText("Full name must be present")
).toBeInTheDocument();
});
});
describe("password input", () => {
it("validates the field must be present", async () => {
const { user } = renderWithSetup(
<ConfirmInviteForm
defaultFormData={defaultFormData}
handleSubmit={handleSubmitSpy}
/>
);
await user.click(screen.getByRole("button", { name: "Submit" }));
expect(
await screen.findByText("Password must be present")
).toBeInTheDocument();
});
});
describe("password_confirmation input", () => {
it("validates the password_confirmation matches the password", async () => {
const { user } = renderWithSetup(
<ConfirmInviteForm
defaultFormData={defaultFormData}
handleSubmit={handleSubmitSpy}
/>
);
await user.type(screen.getByLabelText("Password"), "p@ssw0rd");
await user.type(
screen.getByLabelText("Confirm password"),
"another password"
);
await user.click(screen.getByRole("button", { name: "Submit" }));
const passwordError = screen.getByText(
"Password confirmation does not match password"
);
expect(passwordError).toBeInTheDocument();
});
it("validates the field must be present", async () => {
const { user } = renderWithSetup(
<ConfirmInviteForm
defaultFormData={defaultFormData}
handleSubmit={handleSubmitSpy}
/>
);
await user.click(screen.getByRole("button", { name: "Submit" }));
const passwordError = screen.getByText(
"Password confirmation must be present"
);
expect(passwordError).toBeInTheDocument();
});
});
});