import userEvent from "@testing-library/user-event"; import { render, screen, waitFor } from "@testing-library/react"; import React, { FormEvent, useState } from "react"; import { expect } from "vitest"; import { within } from "@testing-library/dom"; import { CunninghamProvider } from ":/components/Provider"; import { Select } from ":/components/Forms/Select/index"; import { expectMenuToBeClosed, expectMenuToBeOpen, expectOptions, expectOptionToBeDisabled, expectOptionToBeUnselected, expectSelectedOptions, } from ":/components/Forms/Select/test-utils"; import { Button } from ":/components/Button"; describe(" , ); const input = screen.getByRole("combobox", { name: "Cities", }); const menu: HTMLDivElement = screen.getByRole("listbox", { name: "Cities", }); const label = screen.getByText("Cities"); // Expect no options to be selected. expectSelectedOptions([]); // Make sure the label is set as placeholder. expect(Array.from(label.classList)).toContain("placeholder"); await user.click(input); // Make sure the menu is opened and options are rendered. expectMenuToBeOpen(menu); expect( screen.queryByRole("option", { name: "Paris" }), ).toBeInTheDocument(); // Make sure the option is not selected. const option: HTMLLIElement = screen.getByRole("option", { name: "London", }); expectOptionToBeUnselected(option); // Select an option. await user.click(option); // Make sure the option is selected. expectSelectedOptions(["London"]); // Make sure the menu stays open. expectMenuToBeOpen(menu); // Make sure the option is removed from the menu. expect( screen.queryByRole("option", { name: "London" }), ).not.toBeInTheDocument(); // Select other options. await user.click( screen.getByRole("option", { name: "Tokyo", }), ); await user.click( screen.getByRole("option", { name: "Panama", }), ); await waitFor(() => expectSelectedOptions(["London", "Tokyo", "Panama"])); // Clear one option. await user.click( within( screen.getByText("Panama").parentNode as HTMLDivElement, ).getByRole("button", { name: "Clear selection", }), ); await waitFor(() => expectSelectedOptions(["London", "Tokyo"])); // Clear one option. await user.click( within( screen.getByText("London").parentNode as HTMLDivElement, ).getByRole("button", { name: "Clear selection", }), ); await waitFor(() => expectSelectedOptions(["Tokyo"])); // Clear one option. await user.click( within( screen.getByText("Tokyo").parentNode as HTMLDivElement, ).getByRole("button", { name: "Clear selection", }), ); expectSelectedOptions([]); }); it("should clear all selected options", async () => { const user = userEvent.setup(); render( , ); expectSelectedOptions(["New York", "Tokyo"]); }); it("should select with defaultValue using value", async () => { render( setValue(e.target.value as string[])} /> ); }; render(); const input = screen.getByRole("combobox", { name: "Cities", }); // Make sure value is selected. screen.getByText('Value = ["london"]|'); // Change value. const user = userEvent.setup(); await user.click(input); // Make sure the option is removed from the menu. expect( screen.queryByRole("option", { name: "London" }), ).not.toBeInTheDocument(); // Select an option. await user.click( screen.getByRole("option", { name: "New York", }), ); // Make sure value is selected. screen.getByText('Value = ["london","new_york"]|'); // clear value. const button = screen.getByRole("button", { name: "Clear", }); await user.click(button); // Make sure value is cleared. screen.getByText("Value = []|"); }); it("renders disabled", async () => { render( , ); screen.getByText("This is a text"); }); it("renders with state=error", async () => { render( , ); expect( document.querySelector(".c__select.c__select--success"), ).toBeInTheDocument(); }); it("submits form data", async () => { let formData: any; const Wrapper = () => { const onSubmit = (e: FormEvent) => { e.preventDefault(); const data = new FormData(e.currentTarget); formData = { cities: data.getAll("cities"), }; }; return (
, ); screen.getByRole("combobox", { name: "Cities", }); // Make sure default value is rendered. expectSelectedOptions(["Paris"]); // Make sure the clear button is not rendered. expect( screen.queryByRole("button", { name: "Clear all selections", }), ).not.toBeInTheDocument(); }); it("is not possible to select disabled options", async () => { render( , ); // Make sure the input is accessible. screen.getByRole("combobox", { name: "Cities", }); const label = screen.getByText("Cities"); expect(Array.from(label.classList)).toContain("offscreen"); }); it("is possible to select again the last deleted item", async () => { render( , ); const input = screen.getByRole("combobox", { name: "Cities", }); // It returns the input. expect(input.tagName).toEqual("INPUT"); const menu: HTMLDivElement = screen.getByRole("listbox", { name: "Cities", }); expectMenuToBeClosed(menu); // Click on the input. await user.click(input); expectMenuToBeOpen(menu); expectOptions(["Paris", "Panama", "London", "New York", "Tokyo"]); // Select an option. const option: HTMLLIElement = screen.getByRole("option", { name: "New York", }); await user.click(option); expectMenuToBeOpen(menu); expectSelectedOptions(["New York"]); }); it("filters options when typing", async () => { const user = userEvent.setup(); render( , ); const input = screen.getByRole("combobox", { name: "Cities", }); // It returns the input. expect(input.tagName).toEqual("INPUT"); const menu: HTMLDivElement = screen.getByRole("listbox", { name: "Cities", }); expectMenuToBeClosed(menu); // Click on the input. await user.click(input); expectMenuToBeOpen(menu); expectOptions(["Paris", "Panama", "London", "New York", "Tokyo"]); // Verify that filtering works. await user.type(input, "Par"); expectOptions(["Paris"]); await user.type(input, "{enter}"); await waitFor(() => expectSelectedOptions(["Paris"])); }); it("remove last selected value on backspace", async () => { const user = userEvent.setup(); render( , ); expectSelectedOptions(["Panama", "London", "Tokyo"]); // Clear selection. const clearButton = screen.getByRole("button", { name: "Clear all selections", }); await user.click(clearButton); expectSelectedOptions([]); }); it("should select with defaultValue using label", async () => { render( , ); expectSelectedOptions(["New York", "Tokyo"]); }); it("should not select any value if there is not match", async () => { render( setValue(e.target.value as string[])} />
); }; render(); const input = screen.getByRole("combobox", { name: "Cities", }); // Make sure value is selected. screen.getByText('Value = ["london"]|'); expectSelectedOptions(["London"]); // Select one more value. const user = userEvent.setup(); await user.click(input); await waitFor(() => expectOptions(["Paris", "New York", "Tokyo"])); await user.type(input, "New"); await waitFor(() => expectOptions(["New York"])); await user.type(input, "{enter}}"); expectSelectedOptions(["London", "New York"]); screen.getByText('Value = ["london","new_york"]|'); // clear value. const button = screen.getByRole("button", { name: "Clear", }); await user.click(button); // Make sure value is cleared. screen.getByText("Value = []|"); }); it("renders disabled", async () => { render( , ); const input = screen.getByRole("combobox", { name: "Cities", }); // Expect no options to be selected. expectSelectedOptions([]); const user = userEvent.setup(); await user.click(input); await user.click( screen.getByRole("option", { name: "Paris", }), ); expectSelectedOptions(["Paris"]); expect( screen.queryByText("No options available"), ).not.toBeInTheDocument(); await user.click( screen.getByRole("option", { name: "London", }), ); expectSelectedOptions(["Paris", "London"]); screen.getByText("No options available"); }); }); });