"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_2 = require("@testing-library/react");
const helpers_1 = require("../../../testUtils/helpers");
const __1 = require("..");
const TextInput_1 = require("../../TextInput");
describe('<Table/>', () => {
    it('allows to edit correct value of 2nd row after deleting 1st row', () => {
        var _a, _b, _c, _d;
        let updateDataCalled = false;
        const WrapperComponent = () => {
            const initialData = (count) => Array.from({ length: count }).map((e, i) => ({
                identity: `0x${i}`,
            }));
            const [data, setData] = react_1.useState(initialData(2));
            const columns = react_1.useMemo(() => [
                {
                    Header: 'uniqueValue',
                    columns: [
                        {
                            Header: 'Identity',
                            accessor: 'identity',
                            editor: TextInput_1.TextInput,
                        },
                    ],
                },
            ], [data]);
            const updateData = (newData) => {
                updateDataCalled = true;
                expect(newData).toStrictEqual([
                    {
                        identity: '0x1edited',
                    },
                ]);
                setData(newData);
            };
            const deleteRow = (rowData) => {
                setData(data.filter((entry) => entry.identity !== rowData.identity));
            };
            return (react_1.default.createElement(__1.Table, { columns: columns, data: data, deleteRow: deleteRow, updateData: updateData }));
        };
        const { container, getByText } = helpers_1.render(react_1.default.createElement(WrapperComponent, null));
        expect(container).toBeDefined();
        let tableRows = container.querySelectorAll('tbody tr');
        expect(tableRows.length).toBe(2);
        expect((_a = tableRows[0].querySelector('td')) === null || _a === void 0 ? void 0 : _a.textContent).toBe('0x0');
        expect((_b = tableRows[1].querySelector('td')) === null || _b === void 0 ? void 0 : _b.textContent).toBe('0x1');
        const deleteFirstRowEl = tableRows[0].querySelector('.showOnHover');
        if (deleteFirstRowEl) {
            react_2.fireEvent.click(deleteFirstRowEl);
        }
        tableRows = container.querySelectorAll('tbody tr');
        expect(tableRows.length).toBe(1);
        expect((_c = tableRows[0].querySelector('td')) === null || _c === void 0 ? void 0 : _c.textContent).toBe('0x1');
        react_2.fireEvent.click(getByText('0x1'));
        const identityEditor = container.querySelector('tbody tr input');
        expect(identityEditor === null || identityEditor === void 0 ? void 0 : identityEditor.getAttribute('value')).toBe('0x1');
        if (identityEditor) {
            react_2.fireEvent.change(identityEditor, {
                target: { value: '0x1edited' },
            });
        }
        expect(identityEditor === null || identityEditor === void 0 ? void 0 : identityEditor.getAttribute('value')).toBe('0x1edited');
        expect(updateDataCalled).toBe(false);
        if (identityEditor) {
            react_2.fireEvent.blur(identityEditor);
        }
        expect((_d = tableRows[0].querySelector('td')) === null || _d === void 0 ? void 0 : _d.textContent).toBe('0x1edited');
        expect(updateDataCalled).toBe(true);
    });
});
//# sourceMappingURL=Table.test.js.map