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);
};
import { renderHook, act } from '@testing-library/react';
import * as dataHelpers from './dataHelpers';
import { useCollapsibleTableRows } from './useCollapsibleTableRows';
describe('useCollapsibleTableRows tests', function () {
    var rowsData = [
        {
            'n1.a': [[1, 2], [3]],
            c: [7, 8, 9, 10],
            'n1.n2.b': [[], [[4, 5], [6]]],
            'n1.d': []
        }
    ];
    var columnsData = [
        { id: 'n1.a', nestedPath: ['n1'], label: 'columnA' },
        { id: 'n1.n2.b', nestedPath: ['n1', 'n2'], label: 'columnB' },
        { id: 'c', label: 'columnC' },
        { id: 'n1.d', nestedPath: ['n1'], label: 'columnD' }
    ];
    var renderRowCell = function (_a) {
        var cell = _a.cell;
        return cell.values;
    };
    var getRowCellHeight = function () { return 48; };
    var initialProps = {
        rowsData: rowsData,
        columnsData: columnsData,
        renderRowCell: renderRowCell,
        getRowCellHeight: getRowCellHeight,
        maxRowValuesCount: 1
    };
    var getCollapsibleTableRows = function () {
        return renderHook(function (props) { return useCollapsibleTableRows(props); }, { initialProps: initialProps });
    };
    beforeAll(function () {
        jest.spyOn(dataHelpers, 'getTableRowsData');
        jest.spyOn(dataHelpers, 'getIsRowCollapsibleFromMaxValuesCount');
    });
    beforeEach(function () {
        dataHelpers.getTableRowsData.mockClear();
        dataHelpers.getIsRowCollapsibleFromMaxValuesCount.mockClear();
    });
    it('should return collapsed table rows data by default', function () {
        var result = getCollapsibleTableRows().result;
        var getMaxRowValuesCount = dataHelpers.getTableRowsData.mock.calls[0][4];
        expect(result.current.tableRowsData).toEqual(dataHelpers.getTableRowsData.mock.results[0].value);
        expect(getMaxRowValuesCount(0)).toBe(1);
    });
    it('should return correct toggleRowCollapse function for context', function () {
        var result = getCollapsibleTableRows().result;
        act(function () {
            result.current.collapseContextValue.toggleRowCollapse(0);
        });
        var getMaxRowValuesCount = dataHelpers.getTableRowsData.mock.calls[1][4];
        expect(result.current.tableRowsData).toEqual(dataHelpers.getTableRowsData.mock.results[1].value);
        expect(getMaxRowValuesCount(0)).toBe(Infinity);
        act(function () {
            result.current.collapseContextValue.toggleRowCollapse(0);
        });
        getMaxRowValuesCount = dataHelpers.getTableRowsData.mock.calls[2][4];
        expect(result.current.tableRowsData).toEqual(dataHelpers.getTableRowsData.mock.results[2].value);
        expect(getMaxRowValuesCount(0)).toBe(1);
    });
    it('should return correct getIsRowCollapsible function for context', function () {
        var _a = getCollapsibleTableRows(), result = _a.result, rerender = _a.rerender;
        expect(result.current.collapseContextValue.getIsRowCollapsible(rowsData[0])).toBe(true);
        expect(dataHelpers.getIsRowCollapsibleFromMaxValuesCount).toHaveBeenCalledWith(columnsData, rowsData[0], 1, undefined);
        rerender(__assign(__assign({}, initialProps), { maxRowValuesCount: 4 }));
        expect(result.current.collapseContextValue.getIsRowCollapsible(rowsData[0])).toBe(false);
    });
    it('should return correct getIsRowCollapsed function for context', function () {
        var result = getCollapsibleTableRows().result;
        expect(result.current.collapseContextValue.getIsRowCollapsed(0)).toBe(true);
        act(function () {
            result.current.collapseContextValue.toggleRowCollapse(0);
        });
        expect(result.current.collapseContextValue.getIsRowCollapsed(0)).toBe(false);
    });
    it('should not collapse expanded row on data change if it is still collapsible', function () {
        var _a = getCollapsibleTableRows(), result = _a.result, rerender = _a.rerender;
        act(function () {
            result.current.collapseContextValue.toggleRowCollapse(0);
        });
        expect(result.current.collapseContextValue.getIsRowCollapsed(0)).toBe(false);
        rerender(__assign(__assign({}, initialProps), { rowsData: [
                {
                    'n1.a': [[1, 2], [3]],
                    'n1.n2.b': [[], [[4, 5], [6]]],
                    'n1.d': []
                }
            ], columnsData: [
                { id: 'n1.a', nestedPath: ['n1'], label: 'columnA' },
                { id: 'n1.n2.b', nestedPath: ['n1', 'n2'], label: 'columnB' },
                { id: 'n1.d', nestedPath: ['n1'], label: 'columnD' }
            ] }));
        expect(result.current.collapseContextValue.getIsRowCollapsed(0)).toBe(false);
    });
    it('should collapse expanded row on data change if it is not collapsible any more', function () {
        var _a = getCollapsibleTableRows(), result = _a.result, rerender = _a.rerender;
        act(function () {
            result.current.collapseContextValue.toggleRowCollapse(0);
        });
        expect(result.current.collapseContextValue.getIsRowCollapsed(0)).toBe(false);
        rerender(__assign(__assign({}, initialProps), { rowsData: [
                {
                    'n1.d': []
                }
            ], columnsData: [{ id: 'n1.d', nestedPath: ['n1'], label: 'columnD' }] }));
        expect(result.current.collapseContextValue.getIsRowCollapsed(0)).toBe(true);
    });
    it('should collapse expanded row on data change if its raw value changed', function () {
        var _a = getCollapsibleTableRows(), result = _a.result, rerender = _a.rerender;
        act(function () {
            result.current.collapseContextValue.toggleRowCollapse(0);
        });
        expect(result.current.collapseContextValue.getIsRowCollapsed(0)).toBe(false);
        rerender(__assign(__assign({}, initialProps), { rowsData: [
                {
                    rawValue: { c: [7, 8, 9, 10], n1: { a: [[1, 2], [3]] }, n2: { b: [[], [[4, 5], [6]]] }, d: [] },
                    'n1.a': [[1, 2], [3]],
                    c: [7, 8, 9, 10],
                    'n1.n2.b': [[], [[4, 5], [6]]],
                    'n1.d': []
                }
            ] }));
        expect(result.current.collapseContextValue.getIsRowCollapsed(0)).toBe(true);
    });
});
