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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var _a;
import React from 'react';
import { mockResizeObserver } from 'jsdom-testing-mocks';
import { act, render, screen, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterMoment } from '@mui/x-date-pickers/AdapterMoment';
import { DataTypes, EntityAttrTypes, ErrorSource, ErrorType, Mode } from '@reltio/mdm-sdk';
import { MdmModuleProvider } from '../contexts/MdmModuleContext';
import { FeaturesContext } from '../contexts/FeaturesContext';
import { HiddenAttributesContext } from '../contexts/HiddenAttributesContext';
import { HasDeletionsContext } from '../contexts/HasDeletionsContext';
import { PinnedAttributesContext } from '../contexts/PinnedAttributesContext';
import EditModeAttributesPager from './EditModeAttributesPager';
import { ScrollToElementContext, ScrollType } from '../contexts/ScrollToElementContext';
var defaultAttributeType = {
    uri: 'configuration/entityTypes/HCP/attributes/Fruit',
    type: 'String',
    name: 'Fruit',
    label: 'Fruit',
    defaultValue: 'pineapple'
};
var defaultValues = [
    { type: defaultAttributeType.uri, uri: 'entities/sIeHttl/attributes/Fruit/apple', value: 'apple', ov: true },
    { type: defaultAttributeType.uri, uri: 'entities/sIeHttl/attributes/Fruit/kiwi', value: 'kiwi', ov: true },
    { type: defaultAttributeType.uri, uri: 'entities/sIeHttl/attributes/Fruit/orange', value: 'orange', ov: false },
    { type: defaultAttributeType.uri, uri: 'entities/sIeHttl/attributes/Fruit/banana', value: 'banana', ov: true },
    { type: defaultAttributeType.uri, uri: 'entities/sIeHttl/attributes/Fruit/mango', value: 'mango', ov: true },
    { type: defaultAttributeType.uri, uri: 'entities/sIeHttl/attributes/Fruit/avocado', value: 'avocado', ov: false }
];
var entity = { uri: 'entities/sIeHttl', type: 'configuration/entityTypes/HCP' };
var modifiedEntities = (_a = {}, _a[entity.uri] = entity, _a);
var dependentLookups = { editors: {}, structure: {}, initedTypeUris: [] };
var metadata = {
    roles: [{ uri: 'configuration/roles/OfficeStaff', description: '', label: 'Office Staff' }],
    entityTypes: [{ uri: 'configuration/entityTypes/HCP', entityTypeRoleURIs: ['configuration/roles/OfficeStaff'] }]
};
var defaultMdmValues = { metadata: metadata, dependentLookups: dependentLookups, entity: entity, modifiedEntities: modifiedEntities };
var defaultMdmActions = { errorDeactivated: jest.fn(), requestNextPageOfAttributeValues: jest.fn() };
var defaultProps = {
    attributeType: defaultAttributeType,
    values: defaultValues,
    mode: Mode.Editing,
    parentUri: 'entities/sIeHttl',
    showEmptyEditors: false,
    onDeleteAttribute: jest.fn(),
    onChangeAttribute: jest.fn(),
    onAddAttributes: jest.fn()
};
var setUp = function (_a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.props, props = _c === void 0 ? defaultProps : _c, _d = _b.mdmValues, mdmValues = _d === void 0 ? defaultMdmValues : _d, _e = _b.mdmActions, mdmActions = _e === void 0 ? defaultMdmActions : _e, _f = _b.hiddenAttributesContextValue, hiddenAttributesContextValue = _f === void 0 ? {} : _f, _g = _b.pinnedAttributesContextValue, pinnedAttributesContextValue = _g === void 0 ? [] : _g, _h = _b.CustomProviders, CustomProviders = _h === void 0 ? React.Fragment : _h;
    var user = userEvent.setup();
    var Providers = function (_a) {
        var children = _a.children;
        return (React.createElement(CustomProviders, null,
            React.createElement(LocalizationProvider, { dateAdapter: AdapterMoment },
                React.createElement(MdmModuleProvider, { values: mdmValues, actions: mdmActions },
                    React.createElement(HiddenAttributesContext.Provider, { value: hiddenAttributesContextValue },
                        React.createElement(PinnedAttributesContext.Provider, { value: pinnedAttributesContextValue },
                            React.createElement(FeaturesContext.Provider, { value: { showDescription: true } }, children)))))));
    };
    return __assign(__assign({}, render(React.createElement(EditModeAttributesPager, __assign({}, props)), { wrapper: Providers })), { user: user });
};
describe('Edit mode attributes pager tests', function () {
    var resizeObserver = mockResizeObserver();
    describe('attribute renderer', function () {
        it('should render title correctly', function () {
            setUp();
            var title = screen.getByTestId('reltio-attribute-label');
            expect(title).toHaveTextContent('Fruit');
            expect(title).not.toHaveTextContent('*');
        });
        it('should render title correctly if attribute type is required', function () {
            var attributeType = __assign(__assign({}, defaultAttributeType), { required: true });
            var props = __assign(__assign({}, defaultProps), { attributeType: attributeType });
            setUp({ props: props });
            var title = screen.getByTestId('reltio-attribute-label');
            expect(title).toHaveTextContent('Fruit');
            expect(title).toHaveTextContent('*');
        });
        it('should render description correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
            var attributeType, props, user;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        attributeType = __assign(__assign({}, defaultAttributeType), { description: 'The soft, pulpy part of a flowering plant that contains seeds' });
                        props = __assign(__assign({}, defaultProps), { attributeType: attributeType });
                        user = setUp({ props: props }).user;
                        expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
                        return [4 /*yield*/, user.hover(screen.getByTestId('reltio-description-icon'))];
                    case 1:
                        _a.sent();
                        screen.getByText('The soft, pulpy part of a flowering plant that contains seeds');
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not render description if attribute does not have it', function () { return __awaiter(void 0, void 0, void 0, function () {
            return __generator(this, function (_a) {
                setUp();
                expect(screen.queryByTestId('reltio-description-icon')).not.toBeInTheDocument();
                return [2 /*return*/];
            });
        }); });
        it('should render control attribute icon if controlAttributes has attribute type uri', function () {
            var mdmValues = __assign(__assign({}, defaultMdmValues), { controlAttributes: [
                    'configuration/entityTypes/HCP/attributes/Vegatable',
                    'configuration/entityTypes/HCP/attributes/Fruit'
                ] });
            setUp({ mdmValues: mdmValues });
            screen.getByTestId('reltio-control-attribute-icon');
        });
        it('should not render control attribute icon if controlAttributes does not have attribute type uri', function () {
            setUp();
            expect(screen.queryByTestId('reltio-control-attribute-icon')).not.toBeInTheDocument();
        });
        it('should render ov icon and non ov values correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
            var user, nonOvValues;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        user = setUp().user;
                        expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
                        return [4 /*yield*/, user.hover(screen.getByText('+ 2'))];
                    case 1:
                        _a.sent();
                        nonOvValues = screen.getAllByTestId('non-ov-value');
                        expect(nonOvValues).toHaveLength(2);
                        expect(nonOvValues[0]).toHaveTextContent('orange');
                        expect(nonOvValues[1]).toHaveTextContent('avocado');
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not render ov icon if showNonOv is true', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props;
            return __generator(this, function (_a) {
                props = __assign(__assign({}, defaultProps), { showNonOv: true });
                setUp({ props: props });
                expect(screen.queryByTestId('ov-icon')).not.toBeInTheDocument();
                return [2 /*return*/];
            });
        }); });
        it('should render cardinality message correctly', function () {
            var attributeType = {
                uri: 'configuration/entityTypes/HCP/attributes/Int',
                type: 'Int',
                name: 'Int',
                label: 'Int',
                cardinality: { minValue: 10 }
            };
            var values = [{ type: attributeType.uri, uri: 'entities/sIeHttl/attributes/Int/1', value: 5, ov: true }];
            var props = __assign(__assign({}, defaultProps), { attributeType: attributeType, values: values });
            setUp({ props: props });
            screen.getByText('This attribute can have minimum 10 values');
        });
        it('should render, expand and collapse attribute values correctly when max is less than values count', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, user, attributes;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { max: 3 });
                        user = setUp({ props: props }).user;
                        attributes = screen.getAllByTestId('reltio-edit-attribute');
                        expect(attributes).toHaveLength(3);
                        expect(within(attributes[0]).getByRole('textbox')).toHaveValue('apple');
                        expect(within(attributes[1]).getByRole('textbox')).toHaveValue('kiwi');
                        expect(within(attributes[2]).getByRole('textbox')).toHaveValue('banana');
                        expect(screen.queryByText('Show less')).not.toBeInTheDocument();
                        return [4 /*yield*/, user.click(screen.getByText('Show 1 more of remaining 1 value'))];
                    case 1:
                        _a.sent();
                        attributes = screen.getAllByTestId('reltio-edit-attribute');
                        expect(attributes).toHaveLength(4);
                        expect(within(attributes[3]).getByRole('textbox')).toHaveValue('mango');
                        expect(screen.queryByTestId('show-more')).not.toBeInTheDocument();
                        return [4 /*yield*/, user.click(screen.getByText('Show less'))];
                    case 2:
                        _a.sent();
                        attributes = screen.getAllByTestId('reltio-edit-attribute');
                        expect(attributes).toHaveLength(3);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should render, expand and collapse attribute values correctly when showNonOv is true and max is less than values count', function () { return __awaiter(void 0, void 0, void 0, function () {
            var mdmValues, props, user, attributes;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        mdmValues = __assign(__assign({}, defaultMdmValues), { maxValuesInResponse: 2 });
                        props = __assign(__assign({}, defaultProps), { showNonOv: true });
                        user = setUp({ props: props, mdmValues: mdmValues }).user;
                        attributes = screen.getAllByTestId('reltio-edit-attribute');
                        expect(attributes).toHaveLength(2);
                        expect(within(attributes[0]).getByRole('textbox')).toHaveValue('apple');
                        expect(within(attributes[1]).getByRole('textbox')).toHaveValue('kiwi');
                        return [4 /*yield*/, user.click(screen.getByText('Show 2 more of remaining 4 value'))];
                    case 1:
                        _a.sent();
                        attributes = screen.getAllByTestId('reltio-edit-attribute');
                        expect(attributes).toHaveLength(4);
                        expect(within(attributes[2]).getByRole('textbox')).toHaveValue('orange');
                        expect(within(attributes[3]).getByRole('textbox')).toHaveValue('banana');
                        return [4 /*yield*/, user.click(screen.getByText('Show 2 more of remaining 2 value'))];
                    case 2:
                        _a.sent();
                        attributes = screen.getAllByTestId('reltio-edit-attribute');
                        expect(attributes).toHaveLength(6);
                        expect(within(attributes[4]).getByRole('textbox')).toHaveValue('mango');
                        expect(within(attributes[5]).getByRole('textbox')).toHaveValue('avocado');
                        expect(screen.queryByTestId('show-more')).not.toBeInTheDocument();
                        return [4 /*yield*/, user.click(screen.getByText('Show less'))];
                    case 3:
                        _a.sent();
                        attributes = screen.getAllByTestId('reltio-edit-attribute');
                        expect(attributes).toHaveLength(2);
                        screen.getByText('Show 2 more of remaining 4 value');
                        return [2 /*return*/];
                }
            });
        }); });
        it('should render attribute values correctly if max is not passed', function () {
            setUp();
            var attributes = screen.getAllByTestId('reltio-edit-attribute');
            expect(attributes).toHaveLength(4);
            expect(within(attributes[0]).getByRole('textbox')).toHaveValue('apple');
            expect(within(attributes[1]).getByRole('textbox')).toHaveValue('kiwi');
            expect(within(attributes[2]).getByRole('textbox')).toHaveValue('banana');
            expect(within(attributes[3]).getByRole('textbox')).toHaveValue('mango');
            expect(screen.queryByTestId('show-more')).not.toBeInTheDocument();
            expect(screen.queryByTestId('show-less')).not.toBeInTheDocument();
        });
        it('should render attribute values correctly if showNonOv prop is true and max is not passed', function () {
            var props = __assign(__assign({}, defaultProps), { showNonOv: true });
            setUp({ props: props });
            var attributes = screen.getAllByTestId('reltio-edit-attribute');
            expect(attributes).toHaveLength(6);
            expect(within(attributes[0]).getByRole('textbox')).toHaveValue('apple');
            expect(within(attributes[1]).getByRole('textbox')).toHaveValue('kiwi');
            expect(within(attributes[2]).getByRole('textbox')).toHaveValue('orange');
            expect(within(attributes[3]).getByRole('textbox')).toHaveValue('banana');
            expect(within(attributes[4]).getByRole('textbox')).toHaveValue('mango');
            expect(within(attributes[5]).getByRole('textbox')).toHaveValue('avocado');
            expect(screen.queryByTestId('show-more')).not.toBeInTheDocument();
            expect(screen.queryByTestId('show-less')).not.toBeInTheDocument();
        });
        it('should return null if there are no values and showEmptyEditors is falsy', function () {
            var props = __assign(__assign({}, defaultProps), { values: [], showEmptyEditors: false });
            var container = setUp({ props: props }).container;
            expect(container).toBeEmptyDOMElement();
        });
        it('should render special "empty" attribute if there are no values and showEmptyEditors is true', function () {
            var props = __assign(__assign({}, defaultProps), { values: [], showEmptyEditors: true });
            setUp({ props: props });
            expect(within(screen.getByTestId('reltio-edit-attribute')).getByRole('textbox')).toHaveValue('');
        });
        it('should render special "empty" attribute if there are no values and attribute are required', function () {
            var attributeType = __assign(__assign({}, defaultAttributeType), { required: true });
            var props = __assign(__assign({}, defaultProps), { attributeType: attributeType, values: [], showEmptyEditors: true });
            setUp({ props: props });
            expect(within(screen.getByTestId('reltio-edit-attribute')).getByRole('textbox')).toHaveValue('');
        });
        it('should add 2 attributes on add one more button click for simple "empty" attribute', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, user;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { values: [], showEmptyEditors: true });
                        user = setUp({ props: props }).user;
                        return [4 /*yield*/, user.click(screen.getByTestId('reltio-add-one-more-simple-attribute-button'))];
                    case 1:
                        _a.sent();
                        expect(props.onAddAttributes).toHaveBeenCalledWith([
                            {
                                attributeType: {
                                    uri: 'configuration/entityTypes/HCP/attributes/Fruit',
                                    type: 'String',
                                    name: 'Fruit',
                                    label: 'Fruit'
                                },
                                index: Infinity,
                                parentUri: props.parentUri
                            },
                            {
                                attributeType: props.attributeType,
                                index: Infinity,
                                parentUri: props.parentUri
                            }
                        ]);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should add 1 attribute on on add one more button click for Nested "empty" attribute', function () { return __awaiter(void 0, void 0, void 0, function () {
            var attributeType, props, user;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        attributeType = __assign(__assign({}, defaultAttributeType), { type: 'Nested' });
                        props = __assign(__assign({}, defaultProps), { attributeType: attributeType, values: [], showEmptyEditors: true });
                        user = setUp({ props: props }).user;
                        return [4 /*yield*/, user.click(screen.getByText('Create attribute'))];
                    case 1:
                        _a.sent();
                        expect(props.onAddAttributes).toHaveBeenCalledWith([
                            {
                                parentUri: props.parentUri,
                                index: Infinity,
                                attributeType: props.attributeType
                            }
                        ]);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should return null if there are no values, showEmptyEditors is true but delete operation was performed', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, _a, user, container;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { values: [], showEmptyEditors: true });
                        _a = setUp({ props: props }), user = _a.user, container = _a.container;
                        return [4 /*yield*/, user.click(screen.getByTestId('reltio-delete-simple-attribute-button'))];
                    case 1:
                        _b.sent();
                        expect(container).toBeEmptyDOMElement();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should render special "empty" attribute if there are no values and attribute is pinned', function () {
            var props = __assign(__assign({}, defaultProps), { values: [] });
            var pinnedAttributesContextValue = [defaultAttributeType.uri];
            setUp({ props: props, pinnedAttributesContextValue: pinnedAttributesContextValue });
            expect(within(screen.getByTestId('reltio-edit-attribute')).getByRole('textbox')).toHaveValue('');
        });
        it('should call onAddAttributes with correct params in case when values is not empty', 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;
                        return [4 /*yield*/, user.click(screen.getByTestId('reltio-add-one-more-simple-attribute-button'))];
                    case 1:
                        _a.sent();
                        expect(defaultProps.onAddAttributes).toHaveBeenCalledWith([
                            {
                                attributeType: defaultProps.attributeType,
                                index: Infinity,
                                parentUri: defaultProps.parentUri
                            }
                        ]);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should render correct if one of the values is masked', function () {
            var values = __spreadArray(__spreadArray([], defaultValues, true), [
                {
                    type: defaultAttributeType.uri,
                    uri: 'entities/sIeHttl/attributes/Fruit/strawberry',
                    value: 'strawberry',
                    ov: true,
                    masked: true
                }
            ], false);
            var props = __assign(__assign({}, defaultProps), { values: values });
            setUp({ props: props });
            var attributes = screen.getAllByTestId('reltio-edit-attribute');
            expect(within(attributes[4]).queryByRole('textbox')).not.toBeInTheDocument();
            within(attributes[4]).getByText('strawberry');
        });
        it('should not allow to add more values in edit mode if cardinality max value is one', function () {
            var props = __assign(__assign({}, defaultProps), { values: [], showEmptyEditors: true, attributeType: __assign(__assign({}, defaultAttributeType), { cardinality: { maxValue: 1, minValue: 0 } }) });
            setUp({ props: props });
            expect(screen.queryByTestId('reltio-add-one-more-simple-attribute-button')).not.toBeInTheDocument();
        });
        it('should allow to add more values in edit mode if cardinality max value is more than one', function () {
            var props = __assign(__assign({}, defaultProps), { values: [], showEmptyEditors: true, attributeType: __assign(__assign({}, defaultAttributeType), { cardinality: { maxValue: 2, minValue: 1 } }) });
            setUp({ props: props });
            screen.getByTestId('reltio-add-one-more-simple-attribute-button');
        });
        it('should highlight error if it have same parent uri, empty uri, and attribute type uri', function () {
            var fullError = {
                type: 'invalid',
                name: '',
                attributeTypeUri: defaultAttributeType.uri,
                parentUri: defaultProps.parentUri,
                uri: undefined,
                source: 'validationService',
                severity: 'ERROR'
            };
            var highlightError = jest.fn();
            var scrollIntoRef = jest.fn();
            var Providers = function (_a) {
                var children = _a.children;
                return (React.createElement(ScrollToElementContext.Provider, { value: { element: fullError, highlightError: highlightError, scrollIntoRef: scrollIntoRef, type: ScrollType.Error } }, children));
            };
            setUp({ CustomProviders: Providers });
            expect(scrollIntoRef).toHaveBeenCalled();
        });
        it('should not highlight error if it have uri', function () {
            var fullError = {
                type: 'invalid',
                name: '',
                attributeTypeUri: defaultAttributeType.uri,
                parentUri: defaultProps.parentUri,
                uri: 'entities/sIeHttl/attributes/Address/edEosww/AddressType/uri$$1627902448724',
                source: 'validationService',
                severity: 'ERROR'
            };
            var highlightError = jest.fn();
            var scrollIntoRef = jest.fn();
            var Providers = function (_a) {
                var children = _a.children;
                return (React.createElement(ScrollToElementContext.Provider, { value: { element: fullError, highlightError: highlightError, scrollIntoRef: scrollIntoRef, type: ScrollType.Error } }, children));
            };
            setUp({ CustomProviders: Providers });
            expect(scrollIntoRef).not.toHaveBeenCalled();
        });
    });
    describe('image line attribute renderer', function () {
        var defaultAttributeType = {
            type: DataTypes.TYPE_IMAGE,
            label: 'Image',
            name: 'ImageGallery',
            uri: 'configuration/entityTypes/HCP/attributes/ImageGallery'
        };
        var defaultValues = [
            {
                label: '',
                value: {
                    Width: [
                        {
                            type: 'configuration/entityTypes/HCP/attributes/ImageGallery/attributes/Width',
                            value: '200',
                            uri: 'entities/sIeHttl/attributes/ImageGallery/1IozCV9I/Width/1IozCuiq'
                        }
                    ],
                    Height: [
                        {
                            type: 'configuration/entityTypes/HCP/attributes/ImageGallery/attributes/Height',
                            value: '300',
                            uri: 'entities/sIeHttl/attributes/ImageGallery/1IozCV9I/Height/1IozCyz6'
                        }
                    ],
                    CdnUrlThumbnail: [
                        {
                            type: 'configuration/entityTypes/HCP/attributes/ImageGallery/attributes/CdnUrlThumbnail',
                            value: 'https://reltio.com/image.png',
                            uri: 'entities/sIeHttl/attributes/ImageGallery/1IozCV9I/CdnUrlThumbnail/1IozCqSa'
                        }
                    ]
                },
                uri: 'entities/sIeHttl/attributes/ImageGallery/1IozCV9I'
            }
        ];
        var defaultImageLineAttributeProps = __assign(__assign({}, defaultProps), { attributeType: defaultAttributeType, values: defaultValues });
        it('should render title correctly', function () {
            setUp({ props: defaultImageLineAttributeProps });
            var title = screen.getByTestId('reltio-attribute-label');
            expect(title).toHaveTextContent('Image');
            expect(title).not.toHaveTextContent('*');
        });
        it('should render title correctly if attribute type is required', function () {
            var attributeType = __assign(__assign({}, defaultAttributeType), { required: true });
            var props = __assign(__assign({}, defaultImageLineAttributeProps), { attributeType: attributeType });
            setUp({ props: props });
            var title = screen.getByTestId('reltio-attribute-label');
            expect(title).toHaveTextContent('Image');
            expect(title).toHaveTextContent('*');
        });
        it('should render description correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
            var attributeType, props, user;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        attributeType = __assign(__assign({}, defaultAttributeType), { description: 'Image description' });
                        props = __assign(__assign({}, defaultImageLineAttributeProps), { attributeType: attributeType });
                        user = setUp({ props: props }).user;
                        expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
                        return [4 /*yield*/, user.hover(screen.getByTestId('reltio-description-icon'))];
                    case 1:
                        _a.sent();
                        screen.getByText('Image description');
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not render description if attribute does not have it', function () { return __awaiter(void 0, void 0, void 0, function () {
            return __generator(this, function (_a) {
                setUp({ props: defaultImageLineAttributeProps });
                expect(screen.queryByTestId('reltio-description-icon')).not.toBeInTheDocument();
                return [2 /*return*/];
            });
        }); });
        it('should render attribute values correctly', function () {
            setUp({ props: defaultImageLineAttributeProps });
            var imageLine = screen.getByTestId('image-line-edit-mode');
            resizeObserver.mockElementSize(imageLine, { contentBoxSize: { inlineSize: 1000 } });
            act(function () {
                resizeObserver.resize();
            });
            screen.getByTestId('reltio-upload-image-button');
            var attributeValues = screen.getAllByTestId('reltio-attribute-value');
            expect(attributeValues).toHaveLength(2);
            expect(attributeValues[0]).toHaveAttribute('src', 'https://reltio.com/image.png');
            within(attributeValues[1]).getByTestId('SvgDefaultImage');
        });
    });
    describe('special attribute renderer', function () {
        it('should render Roles component for roles', function () {
            var attributeType = EntityAttrTypes.roles;
            var props = __assign(__assign({}, defaultProps), { attributeType: attributeType, values: ['configuration/roles/OfficeStaff'] });
            setUp({ props: props });
            expect(screen.getByTestId('reltio-attribute-label')).toHaveTextContent(attributeType.label);
            expect(screen.getAllByTestId('reltio-attribute-value')).toHaveLength(1);
            expect(screen.getByRole('combobox')).toBeInTheDocument();
            expect(screen.getByText('Office Staff')).toBeInTheDocument();
        });
        it('should render Tags component for tags and special icon before title', function () {
            var attributeType = EntityAttrTypes.tags;
            var props = __assign(__assign({}, defaultProps), { attributeType: attributeType, values: ['tag1'] });
            setUp({ props: props });
            expect(screen.getByTestId('reltio-attribute-label')).toHaveTextContent(attributeType.label);
            expect(screen.getAllByTestId('reltio-attribute-value')).toHaveLength(1);
            expect(screen.getByRole('textbox')).toBeInTheDocument();
            expect(screen.getByText('tag1')).toBeInTheDocument();
        });
        it('should render empty Tags', function () {
            var attributeType = EntityAttrTypes.tags;
            var props = __assign(__assign({}, defaultProps), { attributeType: attributeType, values: [] });
            setUp({ props: props });
            expect(screen.getByTestId('reltio-attribute-label')).toHaveTextContent(attributeType.label);
            expect(screen.getAllByTestId('reltio-attribute-value')).toHaveLength(1);
            expect(screen.getByRole('textbox')).toBeInTheDocument();
        });
        it('should render Attribute component for activeness attributes', function () {
            var attributeType = EntityAttrTypes.endDate;
            var props = __assign(__assign({}, defaultProps), { attributeType: attributeType, values: [1690891216477] });
            setUp({ props: props });
            expect(screen.getByTestId('reltio-attribute-label')).toHaveTextContent(attributeType.label);
            expect(screen.getAllByTestId('reltio-attribute-value')).toHaveLength(1);
            expect(screen.getByRole('textbox')).toHaveValue('08/01/2023');
        });
        it('should return null if there are no activeness values and showEmptyEditors is falsy', function () {
            var props = __assign(__assign({}, defaultProps), { attributeType: EntityAttrTypes.endDate, values: [] });
            var container = setUp({ props: props }).container;
            expect(container).toBeEmptyDOMElement();
        });
        it('should render special "empty" activeness attribute if values empty and showEmptyEditors is true', function () {
            var attributeType = EntityAttrTypes.endDate;
            var props = __assign(__assign({}, defaultProps), { attributeType: attributeType, values: [], showEmptyEditors: true });
            setUp({ props: props });
            expect(screen.getByTestId('reltio-attribute-label')).toHaveTextContent(attributeType.label);
            expect(screen.getAllByTestId('reltio-attribute-value')).toHaveLength(1);
            expect(screen.getByRole('textbox')).toHaveValue('');
        });
        it('should render null if values empty, showEmptyEditors is true but delete operation was performed', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, _a, user, container;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { attributeType: EntityAttrTypes.endDate, values: [], showEmptyEditors: true });
                        _a = setUp({ props: props }), user = _a.user, container = _a.container;
                        expect(container).not.toBeEmptyDOMElement();
                        return [4 /*yield*/, user.click(screen.getByTestId('reltio-delete-simple-attribute-button'))];
                    case 1:
                        _b.sent();
                        expect(container).toBeEmptyDOMElement();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should call HasDeletionsContext.Provider.value.setHasDeletions on delete operation if context is defined', function () { return __awaiter(void 0, void 0, void 0, function () {
            var props, contextValue, CustomProviders, _a, container, user;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        props = __assign(__assign({}, defaultProps), { attributeType: EntityAttrTypes.endDate, values: [], showEmptyEditors: true });
                        contextValue = { setHasDeletions: jest.fn(), hasDeletionsMap: {} };
                        CustomProviders = function (_a) {
                            var children = _a.children;
                            return (React.createElement(HasDeletionsContext.Provider, { value: contextValue }, children));
                        };
                        _a = setUp({ props: props, CustomProviders: CustomProviders }), container = _a.container, user = _a.user;
                        return [4 /*yield*/, user.click(screen.getByTestId('reltio-delete-simple-attribute-button'))];
                    case 1:
                        _b.sent();
                        expect(screen.getAllByTestId('reltio-attribute-value')).toHaveLength(1);
                        expect(contextValue.setHasDeletions).toHaveBeenCalledWith(props.attributeType.uri, true);
                        expect(container).not.toBeEmptyDOMElement();
                        return [2 /*return*/];
                }
            });
        }); });
        it('should not crash without context provider and use internal state for hasDeletions', function () { return __awaiter(void 0, void 0, void 0, function () {
            var attributeType, props, _a, user, container;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        attributeType = EntityAttrTypes.endDate;
                        props = __assign(__assign({}, defaultProps), { attributeType: attributeType, values: [], showEmptyEditors: true });
                        _a = setUp({ props: props }), user = _a.user, container = _a.container;
                        expect(container).not.toBeEmptyDOMElement();
                        return [4 /*yield*/, user.click(screen.getByTestId('reltio-delete-simple-attribute-button'))];
                    case 1:
                        _b.sent();
                        expect(screen.queryAllByTestId('reltio-attribute-value')).toHaveLength(0);
                        return [2 /*return*/];
                }
            });
        }); });
        it('should pass additional params to attribute component', function () {
            var attributeType = EntityAttrTypes.endDate;
            var additionalControlsRenderer = function () { return React.createElement("div", { "data-reltio-id": "additional-controls" }); };
            var props = __assign(__assign({}, defaultProps), { attributeType: attributeType, values: [1690891216477], additionalControlsRenderer: additionalControlsRenderer });
            setUp({ props: props });
            expect(screen.getByTestId('reltio-attribute-label')).toHaveTextContent(attributeType.label);
            expect(screen.getAllByTestId('reltio-attribute-value')).toHaveLength(1);
            expect(screen.getByTestId('additional-controls')).toBeInTheDocument();
        });
    });
    describe('errors', function () {
        it('should render error message correctly', function () {
            var profileErrors = [
                {
                    type: ErrorType.invalid,
                    uri: '',
                    attributeTypeUri: 'configuration/entityTypes/HCP/attributes/Fruit',
                    parentUri: 'entities/sIeHttl',
                    message: 'Attribute is invalid',
                    source: ErrorSource.local,
                    path: []
                }
            ];
            var mdmValues = __assign(__assign({}, defaultMdmValues), { profileErrors: profileErrors });
            var props = __assign(__assign({}, defaultProps), { values: [defaultValues[0]] });
            setUp({ props: props, mdmValues: mdmValues });
            var attribute = screen.getByTestId('reltio-edit-attribute');
            expect(within(attribute).queryByText('Attribute is invalid')).not.toBeInTheDocument();
            screen.getByText('Attribute is invalid');
        });
        it('should render active error correctly', function () {
            var profileErrors = [
                {
                    type: ErrorType.missed,
                    uri: '',
                    attributeTypeUri: 'configuration/entityTypes/HCP/attributes/Fruit',
                    parentUri: 'entities/sIeHttl',
                    message: 'Attribute is required',
                    source: ErrorSource.local,
                    path: []
                }
            ];
            var attributeType = __assign(__assign({}, defaultAttributeType), { required: true });
            var mdmValues = __assign(__assign({}, defaultMdmValues), { profileErrors: profileErrors });
            var props = __assign(__assign({}, defaultProps), { attributeType: attributeType, values: [] });
            setUp({ props: props, mdmValues: mdmValues });
            var attribute = screen.getByTestId('reltio-edit-attribute');
            within(attribute).getByText('Attribute is required');
        });
        it('should not render active error if attribute is hidden', function () {
            var _a;
            var profileErrors = [
                {
                    type: ErrorType.missed,
                    uri: '',
                    attributeTypeUri: 'configuration/entityTypes/HCP/attributes/Fruit',
                    parentUri: 'entities/sIeHttl',
                    message: 'Attribute is required',
                    source: ErrorSource.local,
                    path: []
                }
            ];
            var mdmValues = __assign(__assign({}, defaultMdmValues), { profileErrors: profileErrors });
            var hiddenAttributesContextValue = (_a = {},
                _a[defaultProps.parentUri] = ['configuration/entityTypes/HCP/attributes/Fruit'],
                _a);
            setUp({ mdmValues: mdmValues, hiddenAttributesContextValue: hiddenAttributesContextValue });
            expect(screen.queryByText('Attribute is required')).not.toBeInTheDocument();
        });
    });
});
