"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 __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var react_2 = require("@testing-library/react");
var user_event_1 = __importDefault(require("@testing-library/user-event"));
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var EntityCreator_1 = require("./EntityCreator");
var entityType = {
    uri: 'configuration/entityTypes/HCP',
    label: 'HCP',
    attributes: [
        {
            label: 'String',
            name: 'FirstName',
            type: 'String',
            uri: 'configuration/entityTypes/HCP/attributes/FirstName'
        },
        {
            label: 'Enumeration',
            name: 'Enumeration',
            type: 'String',
            uri: 'configuration/entityTypes/HCP/attributes/Enumeration'
        },
        {
            label: 'Int',
            name: 'Int',
            type: 'Int',
            uri: 'configuration/entityTypes/HCP/attributes/Int'
        },
        {
            label: 'Float',
            name: 'Float',
            type: 'Float',
            uri: 'configuration/entityTypes/HCP/attributes/Float'
        }
    ]
};
var defaultEntityUri = 'entities/123';
var defaultEntity = {
    uri: defaultEntityUri,
    type: 'configuration/entityTypes/HCP',
    attributes: { Attr1: [], Attr2: [] }
};
var defaultMdmValues = {
    modifiedEntities: (_a = {}, _a[defaultEntityUri] = defaultEntity, _a),
    dependentLookups: { editors: {}, structure: {}, initedTypeUris: [] }
};
var mdmActions = {
    addAttributes: jest.fn(),
    removeAttribute: jest.fn(),
    modifyAttribute: jest.fn()
};
var defaultProps = {
    entityType: entityType,
    entityUri: defaultEntityUri,
    mode: mdm_sdk_1.Mode.Editing
};
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;
    var user = user_event_1.default.setup();
    var Providers = function (_a) {
        var children = _a.children;
        return (react_1.default.createElement(MdmModuleContext_1.MdmModuleProvider, { values: mdmValues, actions: mdmActions }, children));
    };
    return __assign(__assign({}, (0, react_2.render)(react_1.default.createElement(EntityCreator_1.EntityCreator, __assign({}, props)), { wrapper: Providers })), { user: user });
};
describe('Entity сreator tests', function () {
    it('should render nothing if entity is null', function () {
        var props = __assign(__assign({}, defaultProps), { entityUri: 'entities/234234232' });
        var container = setUp({ props: props }).container;
        expect(container).toBeEmptyDOMElement();
    });
    it('should render correctly if entity is not null', function () {
        setUp();
        var branchDecorators = react_2.screen.getAllByTestId('branch-decorator');
        expect(branchDecorators).toHaveLength(5);
        (0, react_2.within)(branchDecorators[0]).getByText('More attributes');
        (0, react_2.within)(branchDecorators[1]).getByText('String');
        (0, react_2.within)(branchDecorators[1]).getByRole('textbox');
        (0, react_2.within)(branchDecorators[2]).getByText('Enumeration');
        (0, react_2.within)(branchDecorators[2]).getByRole('textbox');
        (0, react_2.within)(branchDecorators[3]).getByText('Int');
        (0, react_2.within)(branchDecorators[3]).getByRole('textbox');
        (0, react_2.within)(branchDecorators[4]).getByText('Float');
        (0, react_2.within)(branchDecorators[4]).getByRole('textbox');
    });
    it('should use attributeTypesSelectionStrategy for evaluating needed attribute types correctly', function () {
        var props = __assign(__assign({}, defaultProps), { attributeTypesSelectionStrategy: function (entityType) { return entityType.attributes.slice(0, 2); } });
        setUp({ props: props });
        var branchDecorators = react_2.screen.getAllByTestId('branch-decorator');
        expect(branchDecorators).toHaveLength(3);
        (0, react_2.within)(branchDecorators[0]).getByText('More attributes');
        (0, react_2.within)(branchDecorators[1]).getByText('String');
        (0, react_2.within)(branchDecorators[1]).getByRole('textbox');
        (0, react_2.within)(branchDecorators[2]).getByText('Enumeration');
    });
});
