"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"));
const react_2 = require("@testing-library/react");
require("@testing-library/jest-dom");
const ResponseActions_1 = __importDefault(require("./ResponseActions"));
const user_event_1 = __importDefault(require("@testing-library/user-event"));
const react_icons_1 = require("@patternfly/react-icons");
const Message_1 = __importDefault(require("../Message"));
const ALL_ACTIONS = [
    { type: 'positive', label: 'Good response', clickedLabel: 'Response recorded' },
    { type: 'negative', label: 'Bad response', clickedLabel: 'Response recorded' },
    { type: 'copy', label: 'Copy', clickedLabel: 'Copied' },
    { type: 'share', label: 'Share', clickedLabel: 'Shared' },
    { type: 'listen', label: 'Listen', clickedLabel: 'Listening' }
];
const CUSTOM_ACTIONS = [
    {
        regenerate: {
            ariaLabel: 'Regenerate',
            clickedAriaLabel: 'Regenerated',
            onClick: jest.fn(),
            tooltipContent: 'Regenerate',
            clickedTooltipContent: 'Regenerated',
            icon: react_1.default.createElement(react_icons_1.RedoIcon, null)
        },
        download: {
            ariaLabel: 'Download',
            clickedAriaLabel: 'Downloaded',
            onClick: jest.fn(),
            tooltipContent: 'Download',
            clickedTooltipContent: 'Downloaded',
            icon: react_1.default.createElement(react_icons_1.DownloadIcon, null)
        },
        info: {
            ariaLabel: 'Info',
            onClick: jest.fn(),
            tooltipContent: 'Info',
            icon: react_1.default.createElement(react_icons_1.InfoCircleIcon, null)
        }
    }
];
describe('ResponseActions', () => {
    afterEach(() => {
        jest.clearAllMocks();
    });
    it('should handle click within group of buttons correctly', () => __awaiter(void 0, void 0, void 0, function* () {
        (0, react_2.render)(react_1.default.createElement(ResponseActions_1.default, { actions: {
                positive: { onClick: jest.fn() },
                negative: { onClick: jest.fn() },
                copy: { onClick: jest.fn() },
                share: { onClick: jest.fn() },
                listen: { onClick: jest.fn() }
            } }));
        const goodBtn = react_2.screen.getByRole('button', { name: 'Good response' });
        const badBtn = react_2.screen.getByRole('button', { name: 'Bad response' });
        const copyBtn = react_2.screen.getByRole('button', { name: 'Copy' });
        const shareBtn = react_2.screen.getByRole('button', { name: 'Share' });
        const listenBtn = react_2.screen.getByRole('button', { name: 'Listen' });
        const buttons = [goodBtn, badBtn, copyBtn, shareBtn, listenBtn];
        buttons.forEach((button) => {
            expect(button).toBeTruthy();
        });
        yield user_event_1.default.click(goodBtn);
        expect(react_2.screen.getByRole('button', { name: 'Response recorded' })).toHaveClass('pf-chatbot__button--response-action-clicked');
        let unclickedButtons = buttons.filter((button) => button !== goodBtn);
        unclickedButtons.forEach((button) => {
            expect(button).not.toHaveClass('pf-chatbot__button--response-action-clicked');
        });
        yield user_event_1.default.click(badBtn);
        expect(react_2.screen.getByRole('button', { name: 'Response recorded' })).toHaveClass('pf-chatbot__button--response-action-clicked');
        unclickedButtons = buttons.filter((button) => button !== badBtn);
        unclickedButtons.forEach((button) => {
            expect(button).not.toHaveClass('pf-chatbot__button--response-action-clicked');
        });
    }));
    it('should handle click outside of group of buttons correctly', () => __awaiter(void 0, void 0, void 0, function* () {
        // using message just so we have something outside the group that's rendered
        (0, react_2.render)(react_1.default.createElement(Message_1.default, { name: "Bot", role: "bot", avatar: "", content: "I updated your account with those settings. You're ready to set up your first dashboard!", actions: {
                positive: {},
                negative: {}
            } }));
        const goodBtn = react_2.screen.getByRole('button', { name: 'Good response' });
        const badBtn = react_2.screen.getByRole('button', { name: 'Bad response' });
        expect(goodBtn).toBeTruthy();
        expect(badBtn).toBeTruthy();
        yield user_event_1.default.click(goodBtn);
        expect(react_2.screen.getByRole('button', { name: 'Response recorded' })).toHaveClass('pf-chatbot__button--response-action-clicked');
        expect(badBtn).not.toHaveClass('pf-chatbot__button--response-action-clicked');
        yield user_event_1.default.click(badBtn);
        expect(react_2.screen.getByRole('button', { name: 'Response recorded' })).toHaveClass('pf-chatbot__button--response-action-clicked');
        expect(goodBtn).not.toHaveClass('pf-chatbot__button--response-action-clicked');
        yield user_event_1.default.click(react_2.screen.getByText("I updated your account with those settings. You're ready to set up your first dashboard!"));
        expect(goodBtn).not.toHaveClass('pf-chatbot__button--response-action-clicked');
        expect(badBtn).not.toHaveClass('pf-chatbot__button--response-action-clicked');
    }));
    it('should render buttons correctly', () => {
        ALL_ACTIONS.forEach(({ type, label }) => {
            (0, react_2.render)(react_1.default.createElement(ResponseActions_1.default, { actions: { [type]: { onClick: jest.fn() } } }));
            expect(react_2.screen.getByRole('button', { name: label })).toBeTruthy();
        });
    });
    it('should be able to call onClick correctly', () => __awaiter(void 0, void 0, void 0, function* () {
        ALL_ACTIONS.forEach((_a) => __awaiter(void 0, [_a], void 0, function* ({ type, label }) {
            const spy = jest.fn();
            (0, react_2.render)(react_1.default.createElement(ResponseActions_1.default, { actions: { [type]: { onClick: spy } } }));
            yield user_event_1.default.click(react_2.screen.getByRole('button', { name: label }));
            expect(spy).toHaveBeenCalledTimes(1);
        }));
    }));
    it('should swap clicked and non-clicked aria labels on click', () => __awaiter(void 0, void 0, void 0, function* () {
        ALL_ACTIONS.forEach((_a) => __awaiter(void 0, [_a], void 0, function* ({ type, label, clickedLabel }) {
            (0, react_2.render)(react_1.default.createElement(ResponseActions_1.default, { actions: { [type]: { onClick: jest.fn() } } }));
            expect(react_2.screen.getByRole('button', { name: label })).toBeTruthy();
            yield user_event_1.default.click(react_2.screen.getByRole('button', { name: label }));
            expect(react_2.screen.getByRole('button', { name: clickedLabel })).toBeTruthy();
        }));
    }));
    it('should swap clicked and non-clicked tooltips on click', () => __awaiter(void 0, void 0, void 0, function* () {
        ALL_ACTIONS.forEach((_a) => __awaiter(void 0, [_a], void 0, function* ({ type, label, clickedLabel }) {
            (0, react_2.render)(react_1.default.createElement(ResponseActions_1.default, { actions: { [type]: { onClick: jest.fn() } } }));
            expect(react_2.screen.getByRole('button', { name: label })).toBeTruthy();
            yield user_event_1.default.click(react_2.screen.getByRole('button', { name: label }));
            expect(react_2.screen.getByRole('tooltip', { name: clickedLabel })).toBeTruthy();
        }));
    }));
    it('should be able to change aria labels', () => {
        const actions = [
            { type: 'positive', ariaLabel: 'Thumbs up' },
            { type: 'negative', ariaLabel: 'Thumbs down' },
            { type: 'copy', ariaLabel: 'Copy the message' },
            { type: 'share', ariaLabel: 'Share it with friends' },
            { type: 'listen', ariaLabel: 'Listen up' }
        ];
        actions.forEach(({ type, ariaLabel }) => {
            (0, react_2.render)(react_1.default.createElement(ResponseActions_1.default, { actions: { [type]: { onClick: jest.fn(), ariaLabel } } }));
            expect(react_2.screen.getByRole('button', { name: ariaLabel })).toBeTruthy();
        });
    });
    it('should be able to disable buttons', () => {
        ALL_ACTIONS.forEach(({ type, label }) => {
            (0, react_2.render)(react_1.default.createElement(ResponseActions_1.default, { actions: { [type]: { onClick: jest.fn(), isDisabled: true } } }));
            expect(react_2.screen.getByRole('button', { name: label })).toBeDisabled();
        });
    });
    it('should be able to add class to buttons', () => {
        ALL_ACTIONS.forEach(({ type, label }) => {
            (0, react_2.render)(react_1.default.createElement(ResponseActions_1.default, { actions: { [type]: { onClick: jest.fn(), className: 'test' } } }));
            expect(react_2.screen.getByRole('button', { name: label })).toHaveClass('test');
        });
    });
    it('should be able to add custom actions', () => {
        CUSTOM_ACTIONS.forEach((action) => {
            const key = Object.keys(action)[0];
            (0, react_2.render)(react_1.default.createElement(ResponseActions_1.default, { actions: {
                    [key]: {
                        tooltipContent: action[key].tooltipContent,
                        onClick: action[key].onClick,
                        // doing this just because it's easier to test without a regex for the button name
                        ariaLabel: action[key].ariaLabel.toLowerCase(),
                        icon: action[key].icon
                    }
                } }));
            expect(react_2.screen.getByRole('button', { name: key })).toBeTruthy();
        });
    });
});
