import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { TextArea } from ".";
const CLASS_NAME = "mobius-text-area";
describe("TextArea", () => {
it("should render without error", () => {
const view = render();
expect(view).toBeTruthy();
});
it("should call onChange with the updated value when the user types", async () => {
const callback = vi.fn();
const { getByLabelText } = render(
,
);
await userEvent.type(getByLabelText("First name"), "John");
expect(callback).toHaveBeenCalledWith(
expect.objectContaining({
target: screen.getByLabelText("First name"),
}),
);
});
it("focuses correctly via keyboard", async () => {
const callbackFirstName = vi.fn();
const callbackLastName = vi.fn();
render(
<>
>,
);
await userEvent.tab();
await userEvent.type(document.activeElement!, "John");
expect(callbackFirstName).toHaveBeenCalledWith(
expect.objectContaining({
target: screen.getByLabelText("First name"),
}),
);
await userEvent.tab();
await userEvent.type(document.activeElement!, "Doe");
expect(callbackLastName).toHaveBeenCalledWith(
expect.objectContaining({
target: screen.getByLabelText("Last name"),
}),
);
});
describe("with errors", () => {
it("should render the error", () => {
const { getByText } = render(
,
);
expect(getByText("Required")).toBeInTheDocument();
});
it("should set aria-invalid to true", () => {
const { getByLabelText } = render(
,
);
expect(getByLabelText("Message")).toHaveAccessibleDescription("Required");
});
it("should set the correct aria-describedby attribute", () => {
const { getByLabelText } = render(
,
);
expect(getByLabelText("Message")).toHaveAccessibleDescription("Required");
});
});
describe("uses correct class names", () => {
it("uses Mobius class", () => {
render();
expect(screen.getByTestId("test").closest("div")).toHaveClass("mobius");
});
it("uses base Mobius class", () => {
render();
expect(screen.getByTestId("test").closest("div")).toHaveClass(CLASS_NAME);
});
it("includes custom class name", () => {
render(
,
);
expect(screen.getByTestId("test").closest("div")).toHaveClass("my-class");
expect(screen.getByTestId("test").closest("div")).toHaveClass(CLASS_NAME);
});
it("includes disabled class name", () => {
render();
expect(screen.getByTestId("test")).toHaveClass("--is-disabled");
});
it("includes valid class name", () => {
render();
expect(screen.getByTestId("test")).toHaveClass("--is-valid");
});
it("includes invalid class name", () => {
render();
expect(screen.getByTestId("test")).toHaveClass("--is-invalid");
});
});
describe("when disabled", () => {
it("has disabled attribute set on textarea field", () => {
render();
expect(screen.getByTestId("test")).toBeDisabled();
});
});
});