"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("@testing-library/react");
var useKeyboardNavigation_1 = require("./useKeyboardNavigation");
describe('useKeyboardNavigation hook', function () {
    var items = [
        { titleItemIndex: 0, item: { item: { data: 'item1' }, groupId: 'group1' }, index: 0 },
        { titleItemIndex: 0, item: { item: { data: 'item2' }, groupId: 'group2' }, index: 1 },
        { titleItemIndex: 0, item: { item: { data: 'item3', groupId: 'group3' } }, index: 2 },
        { titleItemIndex: 3, item: { item: { data: 'item4', groupId: 'group4' } }, index: 3 },
        { titleItemIndex: 3, item: { item: { data: 'item5', groupId: 'group5' } }, index: 4 }
    ];
    var defaultProps = {
        items: items,
        open: true,
        onSelectFocusedItem: jest.fn(),
        selectedItems: [],
        onClose: jest.fn()
    };
    var setUp = function (props) {
        if (props === void 0) { props = {}; }
        return (0, react_1.renderHook)(useKeyboardNavigation_1.useKeyboardNavigation, { initialProps: __assign(__assign({}, defaultProps), props) });
    };
    var pressKey = function (handleKeyDown, key) {
        (0, react_1.act)(function () {
            handleKeyDown({ key: key, preventDefault: function () { } });
        });
    };
    beforeEach(function () {
        jest.clearAllMocks();
    });
    it('should init correctly', function () {
        var result = setUp().result;
        expect(result.current.focusIndex).toBeNull();
        expect(result.current.handleKeyDown).toBeDefined();
    });
    it('should decrease focusIndex correctly', function () {
        var result = setUp().result;
        pressKey(result.current.handleKeyDown, 'ArrowUp');
        expect(result.current.focusIndex).toBe(4);
        pressKey(result.current.handleKeyDown, 'ArrowUp');
        expect(result.current.focusIndex).toBe(2);
        pressKey(result.current.handleKeyDown, 'ArrowUp');
        expect(result.current.focusIndex).toBe(1);
        pressKey(result.current.handleKeyDown, 'ArrowUp');
        expect(result.current.focusIndex).toBe(4);
    });
    it('should increase focusIndex correctly', function () {
        var result = setUp().result;
        pressKey(result.current.handleKeyDown, 'ArrowDown');
        expect(result.current.focusIndex).toBe(1);
        pressKey(result.current.handleKeyDown, 'ArrowDown');
        expect(result.current.focusIndex).toBe(2);
        pressKey(result.current.handleKeyDown, 'ArrowDown');
        expect(result.current.focusIndex).toBe(4);
        pressKey(result.current.handleKeyDown, 'ArrowDown');
        expect(result.current.focusIndex).toBe(1);
    });
    it('should reset focusIndex when selectedItems changed', function () {
        var _a = setUp(), result = _a.result, rerender = _a.rerender;
        pressKey(result.current.handleKeyDown, 'ArrowDown');
        expect(result.current.focusIndex).toBe(1);
        rerender(__assign(__assign({}, defaultProps), { selectedItems: [] }));
        expect(result.current.focusIndex).toBeNull();
    });
    it('should reset focusIndex when open changed', function () {
        var _a = setUp(), result = _a.result, rerender = _a.rerender;
        pressKey(result.current.handleKeyDown, 'ArrowDown');
        expect(result.current.focusIndex).toBe(1);
        rerender(__assign(__assign({}, defaultProps), { open: false }));
        expect(result.current.focusIndex).toBeNull();
    });
    it('should reset focusIndex when items changed', function () {
        var _a = setUp(), result = _a.result, rerender = _a.rerender;
        pressKey(result.current.handleKeyDown, 'ArrowDown');
        expect(result.current.focusIndex).toBe(1);
        rerender(__assign(__assign({}, defaultProps), { items: items.slice() }));
        expect(result.current.focusIndex).toBeNull();
    });
    it('should call onSelectFocusedItem on Enter', function () {
        var result = setUp().result;
        pressKey(result.current.handleKeyDown, 'ArrowDown');
        pressKey(result.current.handleKeyDown, 'Enter');
        expect(defaultProps.onSelectFocusedItem).toHaveBeenCalledWith({
            item: { data: 'item2' },
            groupData: { item: { data: 'item2' }, groupId: 'group2' }
        });
    });
    it('should not call onSelectFocusedItem on Enter if focusIndex is null', function () {
        var result = setUp().result;
        expect(result.current.focusIndex).toBeNull();
        pressKey(result.current.handleKeyDown, 'Enter');
        expect(defaultProps.onSelectFocusedItem).not.toHaveBeenCalled();
    });
    it('should not call onSelectFocusedItem on Enter if item.notSelectable is true', function () {
        var items = [
            {
                titleItemIndex: 0,
                item: { item: { data: 'item1', notSelectable: true }, groupId: 'group1' },
                index: 0
            },
            { titleItemIndex: 0, item: { item: { data: 'item2', notSelectable: true }, groupId: 'group2' }, index: 1 }
        ];
        var result = setUp({ items: items }).result;
        pressKey(result.current.handleKeyDown, 'ArrowDown');
        pressKey(result.current.handleKeyDown, 'Enter');
        expect(defaultProps.onSelectFocusedItem).not.toHaveBeenCalled();
    });
    it('should restore index after change items to item with moveTo', function () {
        var _a = setUp(), result = _a.result, rerender = _a.rerender;
        pressKey(result.current.handleKeyDown, 'ArrowDown');
        pressKey(result.current.handleKeyDown, 'Enter');
        var newItems = items.slice();
        newItems[1].item['moveTo'] = true;
        rerender(__assign(__assign({}, defaultProps), { items: newItems }));
        expect(defaultProps.onSelectFocusedItem).toHaveBeenCalledWith({
            item: { data: 'item2' },
            groupData: { item: { data: 'item2' }, groupId: 'group2', moveTo: true }
        });
    });
    it('should call onClose after tab click', function () {
        var result = setUp().result;
        pressKey(result.current.handleKeyDown, 'Tab');
        expect(defaultProps.onClose).toHaveBeenCalled();
    });
});
