import * as React from "react";
import {act, render} from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {Autocomplete, AutocompleteItem, AutocompleteSection} from "../src";
import {Modal, ModalContent, ModalBody, ModalHeader, ModalFooter} from "../../modal/src";
type Item = {
label: string;
value: string;
};
const itemsData: Item[] = [
{label: "Cat", value: "cat"},
{label: "Dog", value: "dog"},
{label: "Elephant", value: "elephant"},
{label: "Lion", value: "lion"},
{label: "Tiger", value: "tiger"},
{label: "Giraffe", value: "giraffe"},
{label: "Dolphin", value: "dolphin"},
{label: "Penguin", value: "penguin"},
{label: "Zebra", value: "zebra"},
{label: "Shark", value: "shark"},
{label: "Whale", value: "whale"},
{label: "Otter", value: "otter"},
{label: "Crocodile", value: "crocodile"},
];
const itemsSectionData = [
{
key: "mammals",
title: "Mammals",
children: [
{key: "lion", label: "Lion", value: "lion"},
{key: "tiger", label: "Tiger", value: "tiger"},
{key: "elephant", label: "Elephant", value: "elephant"},
],
},
{
key: "birds",
title: "Birds",
children: [
{key: "penguin", label: "Penguin", value: "penguin"},
{key: "ostrich", label: "Ostrich", value: "ostrich"},
{key: "peacock", label: "Peacock", value: "peacock"},
],
},
];
describe("Autocomplete", () => {
it("should render correctly", () => {
const wrapper = render(
Penguin
Zebra
Shark
,
);
expect(() => wrapper.unmount()).not.toThrow();
});
it("ref should be forwarded", () => {
const ref = React.createRef();
render(
Penguin
Zebra
Shark
,
);
expect(ref.current).not.toBeNull();
});
it("should render correctly (dynamic)", () => {
const wrapper = render(
{(item) => {item.label}}
,
);
expect(() => wrapper.unmount()).not.toThrow();
});
it("should render correctly with section (static)", () => {
const wrapper = render(
Penguin
Zebra
Shark
,
);
expect(() => wrapper.unmount()).not.toThrow();
});
it("should render correctly with section (dynamic)", () => {
const wrapper = render(
{(section) => (
aria-label={section.title}
items={section.children}
title={section.title}
>
{/* @ts-ignore TODO: fix section children types*/}
{(item: Item) => {item.label}}
)}
,
);
expect(() => wrapper.unmount()).not.toThrow();
});
it("should close dropdown when clicking outside autocomplete", async () => {
const wrapper = render(
Penguin
Zebra
Shark
,
);
const autocomplete = wrapper.getByTestId("close-when-clicking-outside-test");
// open the select dropdown
await act(async () => {
await userEvent.click(autocomplete);
});
// assert that the autocomplete dropdown is open
expect(autocomplete).toHaveAttribute("aria-expanded", "true");
// click outside the autocomplete component
await act(async () => {
await userEvent.click(document.body);
});
// assert that the autocomplete is closed
expect(autocomplete).toHaveAttribute("aria-expanded", "false");
});
it("should close dropdown when clicking outside autocomplete with modal open", async () => {
const wrapper = render(
Modal header
Penguin
Zebra
Shark
Modal footer
,
);
const autocomplete = wrapper.getByTestId("close-when-clicking-outside-test");
// open the autocomplete dropdown
await act(async () => {
await userEvent.click(autocomplete);
});
// assert that the autocomplete dropdown is open
expect(autocomplete).toHaveAttribute("aria-expanded", "true");
// click outside the autocomplete component
await act(async () => {
await userEvent.click(document.body);
});
// assert that the autocomplete dropdown is closed
expect(autocomplete).toHaveAttribute("aria-expanded", "false");
});
});