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 React from 'react';
import { screen, render } from '@testing-library/react';
import moment from 'moment';
import { formatDataTypeValue } from '@reltio/mdm-sdk';
import { MdmModuleProvider } from '../contexts/MdmModuleContext';
import { DataTypeValue } from './DataTypeValue';
jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { formatDataTypeValue: jest.fn().mockImplementation(jest.requireActual('@reltio/mdm-sdk').formatDataTypeValue) })); });
var defaultAttributesPresentation = { attr1: {} };
var defaultDateMask = '99-99-99';
var defaultDateTimeMask = '99-99-99 99:99:99';
var defaultMdmValues = {
    attributesPresentation: defaultAttributesPresentation,
    dateMask: defaultDateMask,
    dateTimeMask: defaultDateTimeMask
};
var setUp = function (props, mdmValues) {
    if (mdmValues === void 0) { mdmValues = defaultMdmValues; }
    var Providers = function (_a) {
        var children = _a.children;
        return React.createElement(MdmModuleProvider, { values: mdmValues }, children);
    };
    return render(React.createElement(DataTypeValue, __assign({}, props)), { wrapper: Providers });
};
describe('DataTypeValue tests', function () {
    var checkFormatDataTypeCall = function (_a) {
        var _b = _a.attributesPresentation, attributesPresentation = _b === void 0 ? defaultAttributesPresentation : _b, _c = _a.dateMask, dateMask = _c === void 0 ? defaultDateMask : _c, _d = _a.dateTimeMask, dateTimeMask = _d === void 0 ? defaultDateTimeMask : _d, dataTypeDefinition = _a.dataTypeDefinition, value = _a.value;
        expect(formatDataTypeValue).toHaveBeenCalledWith({ attributesPresentation: attributesPresentation, dateMask: dateMask, dateTimeMask: dateTimeMask, dataTypeDefinition: dataTypeDefinition }, value);
    };
    afterEach(function () {
        jest.clearAllMocks();
    });
    it('should render regular attribute', function () {
        var value = '123';
        var dataTypeDefinition = { type: 'String' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(screen.queryByRole('link')).not.toBeInTheDocument();
        screen.getByText(value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should render attribute with timestamp and system format if masks were not configured', function () {
        var value = '03/05/2020';
        var dataTypeDefinition = { type: 'Date' };
        var mdmValues = __assign(__assign({}, defaultMdmValues), { dateMask: undefined, dateTimeMask: undefined });
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition }, mdmValues);
        expect(screen.queryByRole('link')).not.toBeInTheDocument();
        screen.getByText(value);
        checkFormatDataTypeCall({
            dataTypeDefinition: dataTypeDefinition,
            value: value,
            dateMask: moment.localeData().longDateFormat('L'),
            dateTimeMask: moment.localeData().longDateFormat('L') + ' ' + moment.localeData().longDateFormat('LTS')
        });
    });
    it('should render attribute with date and dateMask', function () {
        var value = '03/05/2020';
        var dataTypeDefinition = { type: 'Date' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(screen.queryByRole('link')).not.toBeInTheDocument();
        screen.getByText('03-05-20');
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should render URL attribute as Link', function () {
        var value = 'https://reltio.com';
        var dataTypeDefinition = { type: 'URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(screen.getByText(value)).toHaveAttribute('href', value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should render Blog URL attribute as Link', function () {
        var value = 'https://reltio.com';
        var dataTypeDefinition = { type: 'Blog URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(screen.getByText(value)).toHaveAttribute('href', value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should render Image URL attribute as Link', function () {
        var value = 'https://reltio.com';
        var dataTypeDefinition = { type: 'Image URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(screen.getByText(value)).toHaveAttribute('href', value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should make absolute for url attribute', function () {
        var value = 'www.reltio.com';
        var dataTypeDefinition = { type: 'URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        var expectedUrl = 'http://' + value; // NOSONAR
        expect(screen.getByText(value)).toHaveAttribute('href', expectedUrl);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should render Email attribute as Link', function () {
        var value = 'it@reltio.com';
        var dataTypeDefinition = { type: 'Email' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(screen.getByText(value)).toHaveAttribute('href', 'mailto:' + value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should not render attribute as Link if its type is not URL/BLOG_URL/IMAGE_URL/Email', function () {
        var value = 'https://reltio.com';
        var dataTypeDefinition = { type: 'notURL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(screen.queryByRole('link')).not.toBeInTheDocument();
        screen.getByText(value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should not render url attribute as Link if "rich" prop is false', function () {
        var value = 'https://reltio.com';
        var dataTypeDefinition = { type: 'URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition, rich: false });
        expect(screen.queryByRole('link')).not.toBeInTheDocument();
        screen.getByText(value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should not render url attribute as Link if url is not valid', function () {
        var value = 'httpsreltiocom';
        var dataTypeDefinition = { type: 'URL' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(screen.queryByRole('link')).not.toBeInTheDocument();
        screen.getByText(value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
    it('should not render email attribute as Link if email is not valid', function () {
        var value = 'itreltio.com';
        var dataTypeDefinition = { type: 'Email' };
        setUp({ value: value, dataTypeDefinition: dataTypeDefinition });
        expect(screen.queryByRole('link')).not.toBeInTheDocument();
        screen.getByText(value);
        checkFormatDataTypeCall({ dataTypeDefinition: dataTypeDefinition, value: value });
    });
});
