"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 __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 };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var useCrosswalkActions_1 = require("./useCrosswalkActions");
var react_1 = require("@testing-library/react");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { addCrosswalk: jest.fn(), addCrosswalkInContributor: jest.fn(), editCrosswalkAttribute: jest.fn(), deleteCrosswalkAttribute: jest.fn(), deleteCrosswalk: jest.fn() })); });
describe('useCrosswalkActions tests', function () {
    var initialProps = {
        onSuccess: jest.fn(),
        onError: jest.fn()
    };
    var getActions = function () {
        return (0, react_1.renderHook)(useCrosswalkActions_1.useCrosswalkActions, { initialProps: initialProps }).result.current;
    };
    var resolveSuccess, resolveError;
    var entity = {
        uri: 'entities/123',
        type: 'configuration/entityTypes/HCP'
    };
    mdm_sdk_1.addCrosswalk.mockImplementation(function () {
        return new Promise(function (_resolve) {
            resolveSuccess = function () {
                return _resolve([{ index: 0, object: { uri: 'relation/1', type: 'type', value: '1' }, successful: true }]);
            };
            resolveError = function () {
                return _resolve([{ index: 0, successful: false, errors: { errorMessage: 'Operation failed with error' } }]);
            };
        });
    });
    mdm_sdk_1.addCrosswalkInContributor.mockImplementation(function () {
        return new Promise(function (_resolve) {
            resolveSuccess = function () { return _resolve([{ index: 0, object: entity, successful: true }]); };
            resolveError = function () {
                return _resolve([{ index: 0, successful: false, errors: { errorMessage: 'Operation failed with error' } }]);
            };
        });
    });
    mdm_sdk_1.editCrosswalkAttribute.mockImplementation(function () {
        return new Promise(function (_resolve) {
            resolveSuccess = function () { return _resolve([{ index: 0, object: entity, successful: true }]); };
            resolveError = function () {
                return _resolve([{ index: 0, successful: false, errors: { errorMessage: 'Operation failed with error' } }]);
            };
        });
    });
    mdm_sdk_1.deleteCrosswalkAttribute.mockImplementation(function () {
        return new Promise(function (_resolve) {
            resolveSuccess = function () { return _resolve([{ index: 0, object: { status: 'success' }, successful: true }]); };
            resolveError = function () {
                return _resolve([{ index: 0, successful: false, errors: { errorMessage: 'Operation failed with error' } }]);
            };
        });
    });
    mdm_sdk_1.deleteCrosswalk.mockImplementation(function () {
        return new Promise(function (_resolve, _reject) {
            resolveSuccess = function () { return _resolve({ status: 'success' }); };
            resolveError = function () { return _reject({ errorMessage: 'Operation failed with error' }); };
        });
    });
    it('should return correct add action', function () { return __awaiter(void 0, void 0, void 0, function () {
        var addCrosswalkAction, addEvent;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    addCrosswalkAction = getActions().addCrosswalk;
                    addEvent = {
                        entity: entity,
                        crosswalk: {
                            type: 'configuration/sources/Test',
                            value: 'test'
                        }
                    };
                    addCrosswalkAction(addEvent);
                    expect(mdm_sdk_1.addCrosswalk).toHaveBeenCalledWith({
                        entityUri: entity.uri,
                        crosswalk: addEvent.crosswalk
                    });
                    return [4 /*yield*/, (0, react_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                resolveSuccess();
                                return [2 /*return*/];
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(initialProps.onSuccess).toHaveBeenCalled();
                    addCrosswalkAction(addEvent);
                    expect(mdm_sdk_1.addCrosswalk).toHaveBeenCalledWith({
                        entityUri: entity.uri,
                        crosswalk: addEvent.crosswalk
                    });
                    return [4 /*yield*/, (0, react_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                resolveError();
                                return [2 /*return*/];
                            });
                        }); })];
                case 2:
                    _a.sent();
                    expect(initialProps.onError).toHaveBeenCalledWith({ errorMessage: 'Operation failed with error' });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should return correct add in contributor action', function () { return __awaiter(void 0, void 0, void 0, function () {
        var addCrosswalkAction, addInContributorEvent;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    addCrosswalkAction = getActions().addCrosswalk;
                    addInContributorEvent = {
                        entity: entity,
                        contributorCrosswalk: {
                            uri: 'entities/2/crosswalks/1',
                            value: '123',
                            type: 'configuration/sources/Reltio',
                            sourceTable: 'sourceTable'
                        },
                        crosswalk: {
                            type: 'configuration/sources/Test',
                            value: 'test',
                            sourceTable: 'crosswalkSourceTable',
                            deleteDate: '123'
                        }
                    };
                    addCrosswalkAction(addInContributorEvent);
                    expect(mdm_sdk_1.addCrosswalkInContributor).toHaveBeenCalledWith({
                        entityTypeUri: entity.type,
                        contributorCrosswalkType: addInContributorEvent.contributorCrosswalk.type,
                        contributorCrosswalkValue: addInContributorEvent.contributorCrosswalk.value,
                        contributorCrosswalkSourceTable: addInContributorEvent.contributorCrosswalk.sourceTable,
                        crosswalkType: addInContributorEvent.crosswalk.type,
                        crosswalkValue: addInContributorEvent.crosswalk.value,
                        crosswalkSourceTable: addInContributorEvent.crosswalk.sourceTable,
                        crosswalkDeleteDate: addInContributorEvent.crosswalk.deleteDate
                    });
                    return [4 /*yield*/, (0, react_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                resolveSuccess();
                                return [2 /*return*/];
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(initialProps.onSuccess).toHaveBeenCalled();
                    addCrosswalkAction(addInContributorEvent);
                    expect(mdm_sdk_1.addCrosswalkInContributor).toHaveBeenCalledWith({
                        entityTypeUri: entity.type,
                        contributorCrosswalkType: addInContributorEvent.contributorCrosswalk.type,
                        contributorCrosswalkValue: addInContributorEvent.contributorCrosswalk.value,
                        contributorCrosswalkSourceTable: addInContributorEvent.contributorCrosswalk.sourceTable,
                        crosswalkType: addInContributorEvent.crosswalk.type,
                        crosswalkValue: addInContributorEvent.crosswalk.value,
                        crosswalkSourceTable: addInContributorEvent.crosswalk.sourceTable,
                        crosswalkDeleteDate: addInContributorEvent.crosswalk.deleteDate
                    });
                    return [4 /*yield*/, (0, react_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                resolveError();
                                return [2 /*return*/];
                            });
                        }); })];
                case 2:
                    _a.sent();
                    expect(initialProps.onError).toHaveBeenCalledWith({ errorMessage: 'Operation failed with error' });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should return correct edit crosswalk attribute action', function () { return __awaiter(void 0, void 0, void 0, function () {
        var editCrosswalk, editEvent;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    editCrosswalk = getActions().editCrosswalk;
                    editEvent = {
                        entityUri: 'entities/123',
                        crosswalkId: '370iIXrRf',
                        attributeName: 'deleteDate',
                        value: '1631905200000'
                    };
                    editCrosswalk(editEvent);
                    expect(mdm_sdk_1.editCrosswalkAttribute).toHaveBeenCalledWith(editEvent);
                    return [4 /*yield*/, (0, react_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                resolveSuccess();
                                return [2 /*return*/];
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(initialProps.onSuccess).toHaveBeenCalled();
                    editCrosswalk(editEvent);
                    expect(mdm_sdk_1.editCrosswalkAttribute).toHaveBeenCalledWith(editEvent);
                    return [4 /*yield*/, (0, react_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                resolveError();
                                return [2 /*return*/];
                            });
                        }); })];
                case 2:
                    _a.sent();
                    expect(initialProps.onError).toHaveBeenCalledWith({ errorMessage: 'Operation failed with error' });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should return correct delete crosswalk attribute action', function () { return __awaiter(void 0, void 0, void 0, function () {
        var editCrosswalk, deleteAttributeEvent;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    editCrosswalk = getActions().editCrosswalk;
                    deleteAttributeEvent = {
                        entityUri: 'entities/123',
                        crosswalkId: '370iIXrRf',
                        attributeName: 'deleteDate'
                    };
                    editCrosswalk(deleteAttributeEvent);
                    expect(mdm_sdk_1.deleteCrosswalkAttribute).toHaveBeenCalledWith(deleteAttributeEvent);
                    return [4 /*yield*/, (0, react_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                resolveSuccess();
                                return [2 /*return*/];
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(initialProps.onSuccess).toHaveBeenCalled();
                    editCrosswalk(deleteAttributeEvent);
                    expect(mdm_sdk_1.deleteCrosswalkAttribute).toHaveBeenCalledWith(deleteAttributeEvent);
                    return [4 /*yield*/, (0, react_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                resolveError();
                                return [2 /*return*/];
                            });
                        }); })];
                case 2:
                    _a.sent();
                    expect(initialProps.onError).toHaveBeenCalledWith({ errorMessage: 'Operation failed with error' });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should return correct delete crosswalk action', function () { return __awaiter(void 0, void 0, void 0, function () {
        var deleteCrosswalkAction, deleteEvent;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    deleteCrosswalkAction = getActions().deleteCrosswalk;
                    deleteEvent = {
                        entityUri: 'entities/123',
                        crosswalkId: '370iIXrRf'
                    };
                    deleteCrosswalkAction(deleteEvent);
                    expect(mdm_sdk_1.deleteCrosswalk).toHaveBeenCalledWith(deleteEvent);
                    return [4 /*yield*/, (0, react_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                resolveSuccess();
                                return [2 /*return*/];
                            });
                        }); })];
                case 1:
                    _a.sent();
                    expect(initialProps.onSuccess).toHaveBeenCalled();
                    deleteCrosswalkAction(deleteEvent);
                    expect(mdm_sdk_1.deleteCrosswalk).toHaveBeenCalledWith(deleteEvent);
                    return [4 /*yield*/, (0, react_1.act)(function () { return __awaiter(void 0, void 0, void 0, function () {
                            return __generator(this, function (_a) {
                                resolveError();
                                return [2 /*return*/];
                            });
                        }); })];
                case 2:
                    _a.sent();
                    expect(initialProps.onError).toHaveBeenCalledWith({ errorMessage: 'Operation failed with error' });
                    return [2 /*return*/];
            }
        });
    }); });
});
