import React from 'react';
import { render, screen } from '@testing-library/react';
import { DataTypes } from '@reltio/mdm-sdk';
import { buildColumnsFilter, columnFilterToMdmFilter, defaultRenderRowCell } from './basicTable';
describe('basicTable tests', function () {
    describe('defaultRenderRowCell', function () {
        it('should call CellValueRenderer with all cell values', function () {
            var DefaultCell = defaultRenderRowCell;
            var values = [1, 2, 3];
            var CellValueRenderer = function (_a) {
                var value = _a.value;
                return React.createElement("div", { "data-reltio-id": "cell" }, value);
            };
            render(React.createElement(DefaultCell, { cell: { values: values }, CellValueRenderer: CellValueRenderer }));
            expect(screen.getAllByTestId('cell')).toHaveLength(values.length);
            values.forEach(function (value) {
                expect(screen.getByText(value)).toBeInTheDocument();
            });
        });
    });
    describe('buildColumnsFilter tests', function () {
        var filters = {
            label: { filter: 'equals', value: '123' },
            directional: { filter: 'before', value: 852145200000 }
        };
        var columnsData = [
            { id: 'label', label: 'Label', dataTypeDefinition: { type: DataTypes.TYPE_STRING } },
            { id: 'type', label: 'Type', dataTypeDefinition: { type: DataTypes.TYPE_STRING } },
            { id: 'directional', label: 'Directional Label', dataTypeDefinition: { type: DataTypes.TYPE_DATE } }
        ];
        it('should return correct filter string', function () {
            expect(buildColumnsFilter(columnsData, filters)).toBe("equals(label,'123') and lte(directional,'852163199999')");
        });
        it('should produce UTC day boundaries for Timestamp column when disableTimestampLocalization is on', function () {
            var middleOfUtcDayMs = Date.UTC(2019, 10, 1, 14, 30, 0);
            var utcDayStart = Date.UTC(2019, 10, 1, 0, 0, 0, 0);
            var utcDayEnd = Date.UTC(2019, 10, 1, 23, 59, 59, 999);
            var tsColumns = [
                {
                    id: 'attributes.UpdatedDate',
                    label: 'Updated date',
                    dataTypeDefinition: {
                        type: DataTypes.TYPE_TIMESTAMP,
                        uri: 'configuration/entityTypes/HCP/attributes/UpdatedDate'
                    }
                }
            ];
            var tsFilters = { 'attributes.UpdatedDate': { filter: 'on', value: middleOfUtcDayMs } };
            expect(buildColumnsFilter(tsColumns, tsFilters, { disableTimestampLocalization: true })).toBe("range(attributes.UpdatedDate,".concat(utcDayStart, ",").concat(utcDayEnd, ")"));
        });
    });
    describe('columnFilterToMdmFilter disableTimestampLocalization', function () {
        var middleOfUtcDayMs = Date.UTC(2019, 10, 1, 14, 30, 0);
        var regularTimestampColumn = {
            id: 'attributes.UpdatedDate',
            dataTypeDefinition: {
                type: DataTypes.TYPE_TIMESTAMP,
                uri: 'configuration/entityTypes/HCP/attributes/UpdatedDate'
            }
        };
        var specialTimestampColumn = {
            id: 'createdTime',
            dataTypeDefinition: { type: DataTypes.TYPE_TIMESTAMP, uri: 'createdTime' }
        };
        var stringColumn = {
            id: 'attributes.Name',
            dataTypeDefinition: { type: DataTypes.TYPE_STRING, uri: 'configuration/entityTypes/HCP/attributes/Name' }
        };
        var onFilter = { filter: 'on', value: middleOfUtcDayMs };
        it('adds disableTimestampLocalization to filter for regular timestamp column when flag is on', function () {
            var result = columnFilterToMdmFilter(regularTimestampColumn, onFilter, {
                disableTimestampLocalization: true
            });
            expect(result).toMatchObject({ disableTimestampLocalization: true });
        });
        it('does not add disableTimestampLocalization for regular timestamp column when flag is off', function () {
            var result = columnFilterToMdmFilter(regularTimestampColumn, onFilter);
            expect(result).not.toHaveProperty('disableTimestampLocalization');
        });
        it('does not add disableTimestampLocalization for special timestamp column even when flag is on', function () {
            var result = columnFilterToMdmFilter(specialTimestampColumn, onFilter, {
                disableTimestampLocalization: true
            });
            expect(result).not.toHaveProperty('disableTimestampLocalization');
        });
        it('does not add disableTimestampLocalization for non-timestamp column when flag is on', function () {
            var result = columnFilterToMdmFilter(stringColumn, { filter: 'equals', value: 'John' }, { disableTimestampLocalization: true });
            expect(result).not.toHaveProperty('disableTimestampLocalization');
        });
    });
});
