"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 react_1 = __importDefault(require("react"));
require("@testing-library/jest-dom");
const react_core_1 = require("@patternfly/react-core");
const react_2 = require("@testing-library/react");
const ChatbotConversationHistoryDropdown_1 = __importDefault(require("./ChatbotConversationHistoryDropdown"));
describe('ChatbotConversationHistoryDropdown', () => {
    const onSelect = jest.fn();
    const menuItems = (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement(react_core_1.DropdownItem, null, "Rename"),
        react_1.default.createElement(react_core_1.DropdownItem, null, "Delete")));
    it('should render the dropdown', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems, menuClassName: "custom-class" }));
        expect(react_2.screen.queryByRole('menuitem', { name: /Conversation options/i })).toBeInTheDocument();
    });
    it('should display the dropdown menuItems', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems }));
        const toggle = react_2.screen.queryByRole('menuitem', { name: /Conversation options/i });
        expect(toggle).toBeInTheDocument();
        react_2.fireEvent.click(toggle);
        (0, react_2.waitFor)(() => {
            expect(react_2.screen.getByText('Rename')).toBeInTheDocument();
            expect(react_2.screen.getByText('Delete')).toBeInTheDocument();
        });
    });
    it('should invoke onSelect callback when menuitem is clicked', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems, onSelect: onSelect }));
        const toggle = react_2.screen.queryByRole('menuitem', { name: /Conversation options/i });
        react_2.fireEvent.click(toggle);
        react_2.fireEvent.click(react_2.screen.getByText('Rename'));
        expect(onSelect).toHaveBeenCalled();
    });
    it('should toggle the dropdown when menuitem is clicked', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems, onSelect: onSelect }));
        const toggle = react_2.screen.queryByRole('menuitem', { name: /Conversation options/i });
        react_2.fireEvent.click(toggle);
        react_2.fireEvent.click(react_2.screen.getByText('Delete'));
        expect(onSelect).toHaveBeenCalled();
        expect(react_2.screen.queryByText('Delete')).not.toBeInTheDocument();
    });
    it('should close the dropdown when user clicks outside', () => {
        (0, react_2.render)(react_1.default.createElement(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems, onSelect: onSelect }));
        const toggle = react_2.screen.queryByRole('menuitem', { name: /Conversation options/i });
        react_2.fireEvent.click(toggle);
        expect(react_2.screen.queryByText('Delete')).toBeInTheDocument();
        react_2.fireEvent.click(toggle.parentElement);
        expect(react_2.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_2.render)(react_1.default.createElement(ChatbotConversationHistoryDropdown_1.default, { menuItems: menuItems, label: "Actions dropdown" }));
        const toggle = react_2.screen.queryByRole('menuitem', { name: /Actions dropdown/i });
        (0, react_2.fireEvent)(toggle, new MouseEvent('mouseenter', {
            bubbles: false,
            cancelable: false
        }));
        yield (0, react_2.waitFor)(() => {
            expect(react_2.screen.queryByText('Actions dropdown')).toBeInTheDocument();
        });
    }));
});
