import { render, screen } from "@testing-library/react"; import React from "react"; import userEvent from "@testing-library/user-event"; import { Switch } from ":/components/Forms/Switch/index"; describe("", () => { it("renders and can be checked", async () => { const user = userEvent.setup(); render(); const input: HTMLInputElement = screen.getByRole("checkbox", { name: "Newsletter", }); expect(input.checked).toEqual(false); await user.click(input); expect(input.checked).toEqual(true); }); it("renders with default value and can be unchecked", async () => { const user = userEvent.setup(); render(); const input: HTMLInputElement = screen.getByRole("checkbox", { name: "Newsletter", }); expect(input.checked).toEqual(true); await user.click(input); expect(input.checked).toEqual(false); }); it("renders disabled", async () => { render(); expect(screen.getByRole("checkbox", { name: "Newsletter" })).toBeDisabled(); // Click and expect the checkbox does not get checked const user = userEvent.setup(); const input: HTMLInputElement = screen.getByRole("checkbox", { name: "Newsletter", }); expect(input.checked).toEqual(false); await user.click(input); expect(input.checked).toEqual(false); }); it("renders with text", async () => { render(); screen.getByText("Text"); }); it("renders with state=success", async () => { render(); screen.getByText("Success text"); expect( document.querySelector(".c__field.c__field--success"), ).toBeInTheDocument(); }); it("renders with state=error", async () => { render(); screen.getByText("Error text"); expect( document.querySelector(".c__field.c__field--error"), ).toBeInTheDocument(); }); it("renders multiple", async () => { // make sure switching one does not switch the others. const user = userEvent.setup(); render(
, ); // expect all checkboxes to be unchecked const newsletter: HTMLInputElement = screen.getByRole("checkbox", { name: "Newsletter", }); const notifications: HTMLInputElement = screen.getByRole("checkbox", { name: "Notifications", }); const phone: HTMLInputElement = screen.getByRole("checkbox", { name: "Phone", }); expect(newsletter.checked).toEqual(false); expect(notifications.checked).toEqual(false); expect(phone.checked).toEqual(false); // Turn on only one checkbox. await user.click(newsletter); expect(newsletter.checked).toEqual(true); expect(notifications.checked).toEqual(false); expect(phone.checked).toEqual(false); // Turn off only one checkbox. await user.click(newsletter); expect(newsletter.checked).toEqual(false); expect(notifications.checked).toEqual(false); expect(phone.checked).toEqual(false); }); it("renders with label right", async () => { render(); const input: HTMLInputElement = screen.getByRole("checkbox", { name: "Newsletter", }); expect(input.closest(".c__switch")).toHaveClass("c__switch--right"); }); it("renders controlled", async () => { const Wrapper = () => { const [checked, setChecked] = React.useState(false); return (
Value: {JSON.stringify(checked)}.
setChecked(e.target.checked)} />
); }; render(); const input: HTMLInputElement = screen.getByRole("checkbox", { name: "Newsletter", }); expect(input.checked).toEqual(false); screen.queryByText("Value: false."); await userEvent.click(input); expect(input.checked).toEqual(true); screen.queryByText("Value: true."); await userEvent.click(input); expect(input.checked).toEqual(false); screen.queryByText("Value: false."); }); });