import React from "react"; import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import DropdownWrapper, { CustomOptionType } from "./DropdownWrapper"; const sampleOptions: CustomOptionType[] = [ { label: "Option 1", value: "option1", tooltipContent: "Tooltip 1", helpText: "Help text 1", }, { label: "Option 2", value: "option2", tooltipContent: "Tooltip 2", helpText: "Help text 2", }, ]; describe("DropdownWrapper Component", () => { const mockOnChange = jest.fn(); beforeEach(() => { jest.clearAllMocks(); }); test("renders with help text", () => { render( ); expect(screen.getByText(/test dropdown/i)).toBeInTheDocument(); expect(screen.getByText(/this is a help text/i)).toBeInTheDocument(); }); test("calls onChange when an option is selected", async () => { render( ); // Open the dropdown await userEvent.click(screen.getByText(/option 1/i)); // Select Option 2 await userEvent.click(screen.getByText(/option 2/i)); expect(mockOnChange).toHaveBeenCalledWith({ helpText: "Help text 2", label: "Option 2", tooltipContent: "Tooltip 2", value: "option2", }); }); test("renders error message when provided", () => { render( ); expect(screen.getByText(/this is an error message/i)).toBeInTheDocument(); }); test("displays no options message when no options are available", async () => { render( ); // Open dropdown await userEvent.click(screen.getByText(/choose option/i)); expect(screen.getByText(/no results found/i)).toBeInTheDocument(); }); test("doesn't render selected value when variant is button", async () => { const buttonText = "Click me"; render( ); // Check if the button text is rendered expect(screen.getByText(buttonText)).toBeInTheDocument(); // Open the dropdown await userEvent.click(screen.getByText(buttonText)); // Select Option 2 await userEvent.click(screen.getByText(/option 2/i)); // Check if the button text is still rendered and not replaced by the selected option expect(screen.getByText(buttonText)).toBeInTheDocument(); expect(screen.queryByText(/option 2/i)).not.toBeInTheDocument(); }); });