"use strict";
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());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const jsx_runtime_1 = require("react/jsx-runtime");
require("@testing-library/jest-dom");
const react_core_1 = require("@patternfly/react-core");
const react_1 = require("@testing-library/react");
const ChatbotConversationHistoryDropdown_1 = __importDefault(require("./ChatbotConversationHistoryDropdown"));
describe('ChatbotConversationHistoryDropdown', () => {
    const onSelect = jest.fn();
    const menuItems = ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(react_core_1.DropdownItem, { children: "Rename" }), (0, jsx_runtime_1.jsx)(react_core_1.DropdownItem, { children: "Delete" })] }));
    it('should render the dropdown', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems, menuClassName: "custom-class" }));
        expect(react_1.screen.queryByRole('menuitem', { name: /Conversation options/i })).toBeInTheDocument();
    });
    it('should display the dropdown menuItems', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems }));
        const toggle = react_1.screen.queryByRole('menuitem', { name: /Conversation options/i });
        expect(toggle).toBeInTheDocument();
        react_1.fireEvent.click(toggle);
        (0, react_1.waitFor)(() => {
            expect(react_1.screen.getByText('Rename')).toBeInTheDocument();
            expect(react_1.screen.getByText('Delete')).toBeInTheDocument();
        });
    });
    it('should invoke onSelect callback when menuitem is clicked', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems, onSelect: onSelect }));
        const toggle = react_1.screen.queryByRole('menuitem', { name: /Conversation options/i });
        react_1.fireEvent.click(toggle);
        react_1.fireEvent.click(react_1.screen.getByText('Rename'));
        expect(onSelect).toHaveBeenCalled();
    });
    it('should toggle the dropdown when menuitem is clicked', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems, onSelect: onSelect }));
        const toggle = react_1.screen.queryByRole('menuitem', { name: /Conversation options/i });
        react_1.fireEvent.click(toggle);
        react_1.fireEvent.click(react_1.screen.getByText('Delete'));
        expect(onSelect).toHaveBeenCalled();
        expect(react_1.screen.queryByText('Delete')).not.toBeInTheDocument();
    });
    it('should close the dropdown when user clicks outside', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems, onSelect: onSelect }));
        const toggle = react_1.screen.queryByRole('menuitem', { name: /Conversation options/i });
        react_1.fireEvent.click(toggle);
        expect(react_1.screen.queryByText('Delete')).toBeInTheDocument();
        react_1.fireEvent.click(toggle.parentElement);
        expect(react_1.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* () {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems, label: "Actions dropdown" }));
        const toggle = react_1.screen.queryByRole('menuitem', { name: /Actions dropdown/i });
        (0, react_1.fireEvent)(toggle, new MouseEvent('mouseenter', {
            bubbles: false,
            cancelable: false
        }));
        yield (0, react_1.waitFor)(() => {
            expect(react_1.screen.queryByText('Actions dropdown')).toBeInTheDocument();
        });
    }));
    it('should be able to set a custom aria-label', () => {
        (0, react_1.render)((0, jsx_runtime_1.jsx)(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems, "aria-label": "Custom conversation options" }));
        expect(react_1.screen.queryByRole('menuitem', { name: /Custom conversation options/i })).toBeInTheDocument();
    });
});
