"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 };
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = __importDefault(require("react"));
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var react_hooks_1 = require("@testing-library/react-hooks");
var MdmModuleContext_1 = require("../../contexts/MdmModuleContext");
var useEditableConnection_1 = require("./useEditableConnection");
jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { getAllUsersForTenant: jest.fn(), getRelationType: jest.fn(), isAvailableRelationBetweenEntities: jest.fn().mockReturnValue(true) })); });
var connectionInit = {
    relation: {
        uri: 'relations/8908',
        type: 'configuration/relationTypes/knowledge',
        attributes: {}
    },
    entity: {
        uri: 'entity/uri',
        label: 'ZhaChy',
        type: 'HCP'
    }
};
var metadata = { entityTypes: [] };
var getActions = function (_a) {
    var _b = _a.connection, connection = _b === void 0 ? connectionInit : _b, onConnectionChange = _a.onConnectionChange;
    var Providers = function (_a) {
        var children = _a.children;
        return react_1.default.createElement(MdmModuleContext_1.MdmModuleProvider, { values: { metadata: metadata } }, children);
    };
    var initialProps = {
        connection: connection,
        onConnectionChange: onConnectionChange
    };
    var result = (0, react_hooks_1.renderHook)(useEditableConnection_1.useEditableConnection, { initialProps: initialProps, wrapper: Providers }).result;
    return result.current;
};
describe('useEditableConnection behaviour', function () {
    describe('Test connection change attributes behaviour)', function () {
        it('should call onConnectionChange with new attributes on add new attributes', function () {
            var onConnectionChange = jest.fn();
            var hook = getActions({ onConnectionChange: onConnectionChange });
            var onAddAttributes = hook.onAddAttributes;
            var payloadItem = {
                parentUri: 'relations/8908',
                attributeType: {
                    label: 'Role',
                    name: 'Role',
                    type: 'String',
                    uri: 'configuration/relationTypes/knowledge/attributes/Role'
                },
                index: 0
            };
            onAddAttributes([payloadItem, payloadItem]);
            expect(onConnectionChange).toHaveBeenCalledWith(__assign(__assign({}, connectionInit), { relation: __assign(__assign({}, connectionInit.relation), { attributes: {
                        Role: [
                            {
                                type: 'configuration/relationTypes/knowledge/attributes/Role',
                                uri: expect.stringContaining('relations/8908/attributes/Role/uri$$'),
                                value: ''
                            },
                            {
                                type: 'configuration/relationTypes/knowledge/attributes/Role',
                                uri: expect.stringContaining('relations/8908/attributes/Role/uri$$'),
                                value: ''
                            }
                        ]
                    } }) }));
        });
        it('should call onConnectionChange with removed attribute on remove attribute action', function () {
            var onConnectionChange = jest.fn();
            var relationUri = 'relations/8908';
            var attributeUri = 'relations/8908/attributes/Role/uri$$123';
            var hook = getActions({
                onConnectionChange: onConnectionChange,
                connection: __assign(__assign({}, connectionInit), { relation: {
                        uri: relationUri,
                        type: 'configuration/relationTypes/knowledge',
                        attributes: {
                            Role: [
                                {
                                    type: 'configuration/relationTypes/knowledge/attributes/Role',
                                    uri: attributeUri,
                                    value: '123'
                                }
                            ]
                        }
                    } })
            });
            var payload = {
                attributeType: {
                    label: 'Role',
                    name: 'Role',
                    type: 'String',
                    uri: 'configuration/relationTypes/knowledge/attributes/Role'
                },
                uri: attributeUri
            };
            var onRemoveAttribute = hook.onRemoveAttribute;
            onRemoveAttribute(payload);
            expect(onConnectionChange).toHaveBeenCalledWith(__assign(__assign({}, connectionInit), { relation: __assign(__assign({}, connectionInit.relation), { attributes: {} }) }));
        });
        it('should call onConnectionChange with editing attribute on edit attribute action', function () {
            var onConnectionChange = jest.fn();
            var relationUri = 'relations/8908';
            var attributeUri = 'relations/8908/attributes/Role/uri$$123';
            var hook = getActions({
                onConnectionChange: onConnectionChange,
                connection: __assign(__assign({}, connectionInit), { relation: {
                        uri: relationUri,
                        type: 'configuration/relationTypes/knowledge',
                        attributes: {
                            Role: [
                                {
                                    type: 'configuration/relationTypes/knowledge/attributes/Role',
                                    uri: attributeUri,
                                    value: '123'
                                }
                            ]
                        }
                    } })
            });
            var payload = {
                attributeType: {
                    label: 'Role',
                    name: 'Role',
                    type: 'String',
                    uri: 'configuration/relationTypes/knowledge/attributes/Role'
                },
                value: 'changed value',
                uri: attributeUri
            };
            var onChangeAttribute = hook.onChangeAttribute;
            onChangeAttribute(payload);
            expect(onConnectionChange).toHaveBeenCalledWith(__assign(__assign({}, connectionInit), { relation: __assign(__assign({}, connectionInit.relation), { attributes: {
                        Role: [
                            {
                                type: 'configuration/relationTypes/knowledge/attributes/Role',
                                uri: attributeUri,
                                value: 'changed value'
                            }
                        ]
                    } }) }));
        });
    });
    describe('Test add new connection behaviour)', function () {
        var newConnection = {
            relation: {
                uri: 'relations/uri$$123',
                type: null,
                attributes: {}
            }
        };
        var relatedEntity = {
            uri: 'entities/entity1',
            type: 'configuration/entityTypes/HCP'
        };
        it('should call onConnectionChange with new relation type on change relation type', function () {
            var onConnectionChange = jest.fn();
            var newType = 'configuration/entityTypes/HCO';
            var newDirection = mdm_sdk_1.Directions.OUT;
            var hook = getActions({ onConnectionChange: onConnectionChange, connection: newConnection, relatedEntity: relatedEntity });
            var onChangeRelationType = hook.onChangeRelationType;
            onChangeRelationType({ type: newType, direction: newDirection });
            expect(onConnectionChange).toHaveBeenCalledWith({
                entity: null,
                relation: __assign(__assign({}, newConnection.relation), { attributes: {}, type: newType, direction: newDirection })
            });
        });
        it('should call onConnectionChange with new relation type entity stays filled with current value', function () {
            var onConnectionChange = jest.fn();
            var newType = 'configuration/entityTypes/Committee';
            var newDirection = mdm_sdk_1.Directions.OUT;
            var hook = getActions({ onConnectionChange: onConnectionChange, connection: connectionInit, relatedEntity: relatedEntity });
            var onChangeRelationType = hook.onChangeRelationType;
            mdm_sdk_1.isAvailableRelationBetweenEntities.mockReturnValueOnce(true);
            onChangeRelationType({ type: newType, direction: newDirection });
            expect(onConnectionChange).toHaveBeenCalledWith({
                entity: connectionInit.entity,
                relation: __assign(__assign({}, connectionInit.relation), { attributes: {}, type: newType, direction: newDirection })
            });
        });
        it('should call onConnectionChange with new relation type on change relation type (and remove previous selectedEntity, if the entity is not compatible with new relation type)', function () {
            var onConnectionChange = jest.fn();
            var newType = 'configuration/entityTypes/Committee';
            var newDirection = mdm_sdk_1.Directions.OUT;
            var hook = getActions({ onConnectionChange: onConnectionChange, connection: connectionInit, relatedEntity: relatedEntity });
            var onChangeRelationType = hook.onChangeRelationType;
            mdm_sdk_1.isAvailableRelationBetweenEntities.mockReturnValueOnce(false);
            onChangeRelationType({ type: newType, direction: newDirection });
            expect(onConnectionChange).toHaveBeenCalledWith({
                entity: null,
                relation: __assign(__assign({}, connectionInit.relation), { attributes: {}, type: newType, direction: newDirection })
            });
        });
        it('should call onConnectionChange with new entity on change entity', function () {
            var onConnectionChange = jest.fn();
            var newEntity = {
                uri: 'entities/entity2',
                type: 'configuration/entityTypes/HCO'
            };
            var hook = getActions({ onConnectionChange: onConnectionChange, connection: newConnection, relatedEntity: relatedEntity });
            var onChangeEntity = hook.onChangeEntity;
            onChangeEntity(newEntity);
            expect(onConnectionChange).toHaveBeenCalledWith(__assign(__assign({}, newConnection), { entity: newEntity }));
        });
    });
});
