var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
import '@testing-library/jest-dom';
import { DropdownItem } from '@patternfly/react-core';
import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import ChatbotConversationHistoryDropdown from './ChatbotConversationHistoryDropdown';
describe('ChatbotConversationHistoryDropdown', () => {
    const onSelect = jest.fn();
    const menuItems = (_jsxs(_Fragment, { children: [_jsx(DropdownItem, { children: "Rename" }), _jsx(DropdownItem, { children: "Delete" })] }));
    it('should render the dropdown', () => {
        render(_jsx(ChatbotConversationHistoryDropdown, { menuItems: menuItems, menuClassName: "custom-class" }));
        expect(screen.queryByRole('menuitem', { name: /Conversation options/i })).toBeInTheDocument();
    });
    it('should display the dropdown menuItems', () => {
        render(_jsx(ChatbotConversationHistoryDropdown, { menuItems: menuItems }));
        const toggle = screen.queryByRole('menuitem', { name: /Conversation options/i });
        expect(toggle).toBeInTheDocument();
        fireEvent.click(toggle);
        waitFor(() => {
            expect(screen.getByText('Rename')).toBeInTheDocument();
            expect(screen.getByText('Delete')).toBeInTheDocument();
        });
    });
    it('should invoke onSelect callback when menuitem is clicked', () => {
        render(_jsx(ChatbotConversationHistoryDropdown, { menuItems: menuItems, onSelect: onSelect }));
        const toggle = screen.queryByRole('menuitem', { name: /Conversation options/i });
        fireEvent.click(toggle);
        fireEvent.click(screen.getByText('Rename'));
        expect(onSelect).toHaveBeenCalled();
    });
    it('should toggle the dropdown when menuitem is clicked', () => {
        render(_jsx(ChatbotConversationHistoryDropdown, { menuItems: menuItems, onSelect: onSelect }));
        const toggle = screen.queryByRole('menuitem', { name: /Conversation options/i });
        fireEvent.click(toggle);
        fireEvent.click(screen.getByText('Delete'));
        expect(onSelect).toHaveBeenCalled();
        expect(screen.queryByText('Delete')).not.toBeInTheDocument();
    });
    it('should close the dropdown when user clicks outside', () => {
        render(_jsx(ChatbotConversationHistoryDropdown, { menuItems: menuItems, onSelect: onSelect }));
        const toggle = screen.queryByRole('menuitem', { name: /Conversation options/i });
        fireEvent.click(toggle);
        expect(screen.queryByText('Delete')).toBeInTheDocument();
        fireEvent.click(toggle.parentElement);
        expect(screen.queryByText('Delete')).not.toBeInTheDocument();
    });
    it('should show the tooltip when the user hovers over the toggle button', () => __awaiter(void 0, void 0, void 0, function* () {
        render(_jsx(ChatbotConversationHistoryDropdown, { menuItems: menuItems, label: "Actions dropdown" }));
        const toggle = screen.queryByRole('menuitem', { name: /Actions dropdown/i });
        fireEvent(toggle, new MouseEvent('mouseenter', {
            bubbles: false,
            cancelable: false
        }));
        yield waitFor(() => {
            expect(screen.queryByText('Actions dropdown')).toBeInTheDocument();
        });
    }));
    it('should be able to set a custom aria-label', () => {
        render(_jsx(ChatbotConversationHistoryDropdown, { menuItems: menuItems, "aria-label": "Custom conversation options" }));
        expect(screen.queryByRole('menuitem', { name: /Custom conversation options/i })).toBeInTheDocument();
    });
});
