"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);
};
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 __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
    return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (g && (g = 0, op[0] && (_ = 0)), _) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var react_2 = require("@testing-library/react");
var user_event_1 = __importDefault(require("@testing-library/user-event"));
var CollapseRowButton_1 = require("../CollapseRowButton");
var test_utils_1 = require("../test-utils");
var BasicTable_1 = require("./BasicTable");
var getRowByIndex = function (index) {
    var fixHeadAndFirstColumnContainer = react_2.screen.queryByTestId('fix-head-and-fix-column-container');
    return (0, react_2.within)(fixHeadAndFirstColumnContainer).queryAllByTestId("base-cell-".concat(index));
};
var getCellByIndexes = function (rowIndex, columnIndex) {
    var row = getRowByIndex(rowIndex);
    return row[columnIndex];
};
var FirstColumnRenderer = function (_a) {
    var value = _a.value, rowValue = _a.rowValue, rowIndex = _a.rowIndex, draggableProps = _a.draggableProps;
    return (react_1.default.createElement("div", null,
        react_1.default.createElement(CollapseRowButton_1.CollapseRowButton, { rowIndex: rowIndex, rowValue: rowValue }),
        react_1.default.createElement("div", __assign({}, draggableProps), "drag el"),
        react_1.default.createElement("div", null, value.name)));
};
var SecondColumnRenderer = function (_a) {
    var value = _a.value;
    return react_1.default.createElement("div", null, value.value);
};
var getRowCellRenderer = function (id) {
    switch (id) {
        case 'id1':
            return FirstColumnRenderer;
        case 'id2':
            return SecondColumnRenderer;
        default:
            return function () { return null; };
    }
};
var onHeadClickFn = jest.fn();
var HeadCellRenderer = function (_a) {
    var headCellData = _a.headCellData;
    return (react_1.default.createElement("div", null,
        react_1.default.createElement("button", { onClick: onHeadClickFn }, headCellData.label)));
};
var columnsData = [
    {
        autoResize: false,
        headCellRenderer: HeadCellRenderer,
        id: 'id1',
        initialWidth: 300,
        label: 'column 1',
        minWidth: 250,
        resizable: true,
        draggable: true,
        rowCellValueRenderer: getRowCellRenderer('id1'),
        sortable: false
    },
    {
        autoResize: false,
        headCellRenderer: function () { return 'second column'; },
        id: 'id2',
        initialWidth: 300,
        label: 'column 2',
        minWidth: 250,
        resizable: true,
        rowCellValueRenderer: getRowCellRenderer('id2'),
        sortable: false
    }
];
var rowsData = [
    { id1: { name: 'row 1' }, id2: [{ value: 'row 1 value 1' }, { value: 'row 1 value 2' }, { value: 'row 1 value 3' }] },
    { id1: { name: 'row 2' }, id2: [{ value: 'row 2 value 1' }, { value: 'row 2 value 2' }] }
];
var defaultProps = {
    fixFirstColumn: true,
    columnsData: columnsData,
    rowsData: rowsData,
    headRowHeight: 48,
    defaultColumnWidth: 250,
    defaultColumnMinWidth: 200,
    dndColumnReorderingEnabled: true,
    dndColumnReorderingHandler: function () { },
    dndRowReorderingEnabled: true,
    dndRowReorderingHandler: function () { },
    getIdFromRowValue: function (rowValue) { return rowValue.id1.name; },
    getRowCellHeight: function () { return 48; },
    maxRowValuesCount: 1,
    context: {}
};
var setUp = function (props) {
    if (props === void 0) { props = defaultProps; }
    var user = user_event_1.default.setup();
    return __assign(__assign({}, (0, react_2.render)(react_1.default.createElement(BasicTable_1.BasicTable, __assign({}, props)))), { user: user });
};
describe('basic table tests', function () {
    beforeEach(function () {
        jest.clearAllMocks();
    });
    it('should not render Table when columnsData is empty array', function () {
        var props = __assign(__assign({}, defaultProps), { columnsData: [] });
        var container = setUp(props).container;
        expect(container).toBeEmptyDOMElement();
    });
    it('should render correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
        var unmockTableSizing, user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    unmockTableSizing = (0, test_utils_1.mockBasicTableSizing)();
                    user = setUp().user;
                    react_2.screen.getByText('column 1');
                    react_2.screen.getByText('second column');
                    react_2.screen.getByText('row 1');
                    react_2.screen.getByText('row 1 value 1');
                    expect(react_2.screen.queryByText('row 1 value 2')).not.toBeInTheDocument();
                    expect(react_2.screen.queryByText('row 1 value 3')).not.toBeInTheDocument();
                    react_2.screen.getByText('row 2');
                    react_2.screen.getByText('row 2 value 1');
                    expect(react_2.screen.queryByText('row 2 value 2')).not.toBeInTheDocument();
                    return [4 /*yield*/, user.click((0, react_2.within)(getCellByIndexes(0, 0)).queryByTestId('collapse-button'))];
                case 1:
                    _a.sent();
                    react_2.screen.getByText('row 1 value 2');
                    react_2.screen.getByText('row 1 value 3');
                    return [4 /*yield*/, user.click((0, react_2.within)(getCellByIndexes(1, 0)).queryByTestId('collapse-button'))];
                case 2:
                    _a.sent();
                    react_2.screen.getByText('row 2 value 2');
                    return [4 /*yield*/, user.click((0, react_2.within)(getCellByIndexes(0, 0)).queryByTestId('collapse-button'))];
                case 3:
                    _a.sent();
                    expect(react_2.screen.queryByText('row 1 value 2')).not.toBeInTheDocument();
                    expect(react_2.screen.queryByText('row 1 value 3')).not.toBeInTheDocument();
                    return [4 /*yield*/, user.click((0, react_2.within)(getCellByIndexes(1, 0)).queryByTestId('collapse-button'))];
                case 4:
                    _a.sent();
                    expect(react_2.screen.queryByText('row 2 value 2')).not.toBeInTheDocument();
                    unmockTableSizing();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should collapse all rows after start dragging', function () { return __awaiter(void 0, void 0, void 0, function () {
        var unmockTableSizing, mouse, user, dragIndicator;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    unmockTableSizing = (0, test_utils_1.mockBasicTableSizing)();
                    mouse = [
                        { clientX: 0, clientY: 0 },
                        { clientX: 20, clientY: 20 }
                    ];
                    user = setUp().user;
                    return [4 /*yield*/, user.click((0, react_2.within)(getCellByIndexes(0, 0)).queryByTestId('collapse-button'))];
                case 1:
                    _a.sent();
                    return [4 /*yield*/, user.click((0, react_2.within)(getCellByIndexes(1, 0)).queryByTestId('collapse-button'))];
                case 2:
                    _a.sent();
                    expect((0, react_2.within)(getCellByIndexes(0, 1)).queryByText('row 1 value 1')).toBeInTheDocument();
                    expect((0, react_2.within)(getCellByIndexes(0, 1)).queryByText('row 1 value 2')).toBeInTheDocument();
                    expect((0, react_2.within)(getCellByIndexes(0, 1)).queryByText('row 1 value 3')).toBeInTheDocument();
                    expect((0, react_2.within)(getCellByIndexes(1, 1)).queryByText('row 2 value 1')).toBeInTheDocument();
                    expect((0, react_2.within)(getCellByIndexes(1, 1)).queryByText('row 2 value 2')).toBeInTheDocument();
                    dragIndicator = (0, react_2.within)(getCellByIndexes(1, 0)).getByRole('button', { name: 'drag el' });
                    (0, react_2.act)(function () {
                        react_2.fireEvent.mouseDown(dragIndicator, mouse[0]);
                    });
                    return [4 /*yield*/, (0, react_2.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                react_2.fireEvent.mouseMove(dragIndicator, mouse[1]);
                                return [2 /*return*/];
                            });
                        }); })];
                case 3:
                    _a.sent();
                    expect((0, react_2.within)(getCellByIndexes(0, 1)).queryByText('row 1 value 1')).toBeInTheDocument();
                    expect((0, react_2.within)(getCellByIndexes(0, 1)).queryByText('row 1 value 2')).not.toBeInTheDocument();
                    expect((0, react_2.within)(getCellByIndexes(0, 1)).queryByText('row 1 value 3')).not.toBeInTheDocument();
                    expect((0, react_2.within)(getCellByIndexes(1, 1)).queryByText('row 2 value 1')).toBeInTheDocument();
                    expect((0, react_2.within)(getCellByIndexes(1, 1)).queryByText('row 2 value 2')).not.toBeInTheDocument();
                    (0, react_2.act)(function () {
                        react_2.fireEvent.mouseUp(dragIndicator);
                    });
                    unmockTableSizing();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should able to click on buttons in table head', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp().user;
                    expect(onHeadClickFn).not.toHaveBeenCalled();
                    return [4 /*yield*/, user.click(react_2.screen.getAllByRole('button', { name: columnsData[0].label })[1])];
                case 1:
                    _a.sent();
                    expect(onHeadClickFn).toHaveBeenCalled();
                    return [2 /*return*/];
            }
        });
    }); });
});
