import React, { useState } from "react"; import { render, screen, fireEvent, waitFor } from "@testing-library/react"; import { describe, it, expect, vi } from "vitest"; import ToggleGroup from "../../app/components/ToggleGroup"; // Test component for form integration const TestForm = () => { const [selectedToggle, setSelectedToggle] = useState("left"); return (
setSelectedToggle("left")} > Left Option setSelectedToggle("middle")} > Middle Option setSelectedToggle("right")} > Right Option
); }; // Dynamic component for prop changes const DynamicToggleGroup = ({ position, state, showText }) => { return ( Dynamic Content ); }; describe("ToggleGroup Integration", () => { it("handles form submission", async () => { const handleSubmit = vi.fn(); render(
{}}> First Option {}}> Second Option {}}> Third Option
); const submitButton = screen.getByRole("button", { name: "Submit" }); fireEvent.click(submitButton); expect(handleSubmit).toHaveBeenCalledTimes(1); }); it("handles keyboard navigation between toggle groups", () => { render(); const toggleGroups = screen.getAllByRole("button"); // Focus first toggle group toggleGroups[0].focus(); expect(toggleGroups[0]).toHaveFocus(); // Test keyboard navigation fireEvent.keyDown(toggleGroups[0], { key: "Tab" }); // Note: Tab navigation behavior depends on browser implementation }); it("handles dynamic prop changes", () => { const { rerender } = render( ); let toggleGroup = screen.getByRole("button"); expect(toggleGroup).toHaveClass( "rounded-l-[var(--measures-radius-medium)]", "rounded-r-none" ); expect(toggleGroup).toHaveTextContent("Dynamic Content"); rerender( ); toggleGroup = screen.getByRole("button"); expect(toggleGroup).toHaveClass("rounded-none"); expect(toggleGroup).toHaveClass("bg-[var(--color-magenta-magenta100)]"); expect(toggleGroup).toHaveTextContent("Dynamic Content"); }); it("handles multiple toggle groups in form", () => { render(); const toggleGroups = screen.getAllByRole("button"); expect(toggleGroups).toHaveLength(3); // Test clicking different toggle groups fireEvent.click(toggleGroups[0]); fireEvent.click(toggleGroups[1]); fireEvent.click(toggleGroups[2]); }); it("handles state changes", async () => { const { rerender } = render(); const toggleGroups = screen.getAllByRole("button"); // Initially, left should be selected expect(toggleGroups[0]).toHaveClass("bg-[var(--color-magenta-magenta100)]"); // Click middle toggle fireEvent.click(toggleGroups[1]); await waitFor(() => { expect(toggleGroups[1]).toHaveClass( "bg-[var(--color-magenta-magenta100)]" ); }); }); it("handles content changes", () => { const { rerender } = render( Initial Content ); let toggleGroup = screen.getByRole("button"); expect(toggleGroup).toHaveTextContent("Initial Content"); rerender(Updated Content); toggleGroup = screen.getByRole("button"); expect(toggleGroup).toHaveTextContent("Updated Content"); rerender(Hidden Content); toggleGroup = screen.getByRole("button"); expect(toggleGroup).toHaveTextContent("Hidden Content"); }); it("handles performance with many toggle groups", () => { const ManyToggleGroups = () => { const [selected, setSelected] = useState(0); return (
{Array.from({ length: 10 }, (_, i) => ( setSelected(i)} > Option {i + 1} ))}
); }; render(); const toggleGroups = screen.getAllByRole("button"); expect(toggleGroups).toHaveLength(10); // Test clicking different toggle groups fireEvent.click(toggleGroups[5]); expect(toggleGroups[5]).toHaveClass("bg-[var(--color-magenta-magenta100)]"); }); it("handles rapid state changes", async () => { const { rerender } = render(); const toggleGroups = screen.getAllByRole("button"); // Rapidly change states for (let i = 0; i < 5; i++) { fireEvent.click(toggleGroups[i % 3]); await waitFor(() => { expect(toggleGroups[i % 3]).toHaveClass( "bg-[var(--color-magenta-magenta100)]" ); }); } }); it("handles mixed content types", () => { render(
Text Only Icon Only Text Only
); const toggleGroups = screen.getAllByRole("button"); expect(toggleGroups[0]).toHaveTextContent("Text Only"); expect(toggleGroups[1]).toHaveTextContent("Icon Only"); expect(toggleGroups[2]).toHaveTextContent("Text Only"); }); });