"use strict";
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 test_utilities_1 = require("../../test-utilities");
const Tooltip_1 = require("./Tooltip");
jest.useFakeTimers();
const children = 'This is a tooltip';
const defaultProps = {
    content: 'In case we need to contact you about your order',
};
describe('<Tooltip />', () => {
    it('renders the children within a button', () => {
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        expect(tooltip).toContainReactComponent('button', { children });
    });
    it('renders the tooltip content when the control is focused', () => {
        var _a;
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        (_a = tooltip.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onFocus');
        expect(tooltip).toContainReactComponent(Tooltip_1.TooltipContent);
    });
    it('renders the tooltip arrow when the control is focused', () => {
        var _a;
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        (_a = tooltip.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onFocus');
        expect(tooltip).toContainReactComponent(Tooltip_1.TooltipArrow);
    });
    it('does not render the tooltip content when the control is blurred', () => {
        var _a;
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        (_a = tooltip.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onBlur');
        expect(tooltip).not.toContainReactComponent(Tooltip_1.TooltipContent);
    });
    it('renders the tooltip content when the control is hovered', () => {
        var _a;
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        (_a = tooltip.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onMouseEnter');
        expect(tooltip).toContainReactComponent(Tooltip_1.TooltipContent);
    });
    it('does not render the tooltip content when the control loses hover', () => {
        var _a;
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        (_a = tooltip.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onMouseLeave');
        expect(tooltip).not.toContainReactComponent(Tooltip_1.TooltipContent);
    });
    it('toggles rendering the tooltip content when the control is clicked', () => {
        var _a, _b;
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        const fakeEvent = {
            preventDefault: jest.fn(),
        };
        (_a = tooltip.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onClick', fakeEvent);
        expect(tooltip).toContainReactComponent(Tooltip_1.TooltipContent);
        (_b = tooltip.find('button')) === null || _b === void 0 ? void 0 : _b.trigger('onClick', fakeEvent);
        tooltip.act(() => {
            jest.runAllTimers();
        });
        expect(tooltip).not.toContainReactComponent(Tooltip_1.TooltipContent);
    });
    it('toggles rendering the tooltip content when RETURN is pressed on the control', () => {
        var _a, _b;
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        const fakeEvent = {
            preventDefault: jest.fn(),
            key: 'Enter',
        };
        (_a = tooltip.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onKeyDown', fakeEvent);
        expect(tooltip).toContainReactComponent(Tooltip_1.TooltipContent);
        (_b = tooltip.find('button')) === null || _b === void 0 ? void 0 : _b.trigger('onKeyDown', fakeEvent);
        tooltip.act(() => {
            jest.runAllTimers();
        });
        expect(tooltip).not.toContainReactComponent(Tooltip_1.TooltipContent);
    });
    it('toggles rendering the tooltip content when SPACE is pressed on the control', () => {
        var _a, _b;
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        const fakeEvent = {
            preventDefault: jest.fn(),
            key: 'Space',
        };
        (_a = tooltip.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onKeyDown', fakeEvent);
        expect(tooltip).toContainReactComponent(Tooltip_1.TooltipContent);
        (_b = tooltip.find('button')) === null || _b === void 0 ? void 0 : _b.trigger('onKeyDown', fakeEvent);
        tooltip.act(() => {
            jest.runAllTimers();
        });
        expect(tooltip).not.toContainReactComponent(Tooltip_1.TooltipContent);
    });
    it('does not render the tooltip content when ESC is pressed on the control', () => {
        var _a;
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        const fakeEvent = {
            preventDefault: jest.fn(),
            key: 'Escape',
        };
        (_a = tooltip.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onKeyDown', fakeEvent);
        expect(tooltip).not.toContainReactComponent(Tooltip_1.TooltipContent);
    });
    it('does not render the tooltip content when a key other than SPACE or RETURN is pressed on the control', () => {
        var _a;
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        const fakeEvent = {
            preventDefault: jest.fn(),
            key: 'Backspace',
        };
        (_a = tooltip.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onKeyDown', fakeEvent);
        expect(tooltip).not.toContainReactComponent(Tooltip_1.TooltipContent);
    });
    it('renders a control button and tooltip with the required aria attributes when the tooltip is active', () => {
        var _a, _b;
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        (_a = tooltip.find('button')) === null || _a === void 0 ? void 0 : _a.trigger('onFocus');
        const tooltipControl = tooltip.find('button');
        const tooltipContent = (_b = tooltip.find(Tooltip_1.TooltipContent)) === null || _b === void 0 ? void 0 : _b.find('div');
        const tooltipContentId = (tooltipContent === null || tooltipContent === void 0 ? void 0 : tooltipContent.props).id;
        expect(tooltipContent).toHaveReactProps({ role: 'tooltip' });
        expect(tooltipControl).toHaveReactProps({
            'aria-pressed': true,
            'aria-controls': tooltipContentId,
            'aria-describedby': tooltipContentId,
        });
    });
    it('renders a control button with the required aria attributes when the tooltip is not active', () => {
        const tooltip = test_utilities_1.mountWithContext(<Tooltip_1.Tooltip {...defaultProps}>{children}</Tooltip_1.Tooltip>);
        expect(tooltip.find('button')).toHaveReactProps({
            'aria-pressed': false,
            'aria-controls': undefined,
            'aria-describedby': undefined,
        });
    });
});
