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 { renderHook, act } from '@testing-library/react-hooks';
import { MultiGraph } from 'graphology';
import { useSigma } from '@react-sigma/core';
import { RelationTypeDirection } from '@reltio/mdm-sdk';
import { useLoopRenderer } from './useLoopRenderer';
import { useSigmaCustomRenderers } from '../contexts/SigmaCustomRenderersContext';
import { drawSelfRelationLoop } from '../rendering/canvas/selfRelationLoop';
jest.mock('@react-sigma/core');
jest.mock('../contexts/SigmaCustomRenderersContext');
jest.mock('../rendering/canvas/selfRelationLoop');
var graphJson = {
    attributes: {},
    nodes: [
        {
            key: 'node1',
            attributes: {
                label: '<No label>',
                entityTypeUri: 'hcp',
                traversedRelationsCount: 2,
                untraversedRelationsCount: 0,
                x: 1,
                y: 1
            }
        },
        {
            key: 'node2',
            attributes: {
                label: '<No label>',
                entityTypeUri: 'hcp',
                traversedRelationsCount: 3,
                untraversedRelationsCount: 2,
                x: 1,
                y: 1
            }
        },
        {
            key: 'node3',
            attributes: {
                label: '<No label>',
                entityTypeUri: 'hca',
                traversedRelationsCount: 1,
                untraversedRelationsCount: 0,
                x: 1,
                y: 1
            }
        }
    ],
    edges: [
        {
            key: 'r1',
            source: 'node1',
            target: 'node2',
            attributes: {
                size: 1,
                color: 'rgba(0, 0, 0, 0.2)',
                forceLabel: false,
                loop: false,
                relationTypeUri: 'relType1',
                direction: 'directed'
            }
        },
        {
            key: 'r2',
            source: 'node3',
            target: 'node2',
            attributes: {
                size: 1,
                color: 'rgba(0, 0, 0, 0.2)',
                forceLabel: false,
                loop: false,
                relationTypeUri: 'relType2',
                direction: 'directed'
            }
        },
        {
            key: 'r3',
            source: 'node2',
            target: 'node2',
            attributes: {
                size: 1,
                color: 'rgba(0, 0, 0, 0.2)',
                forceLabel: true,
                loop: true,
                relationTypeUri: 'relType3',
                direction: 'directed'
            }
        },
        {
            key: 'r4',
            source: 'node1',
            target: 'node1',
            attributes: {
                size: 1,
                color: 'rgba(0, 0, 0, 0.2)',
                forceLabel: true,
                loop: true,
                relationTypeUri: 'relType4',
                direction: 'directed'
            }
        }
    ]
};
var edges = {
    r1: {
        size: 1,
        color: 'rgba(0, 0, 0, 0.2)',
        forceLabel: false,
        loop: false,
        relationTypeUri: 'relType1',
        direction: RelationTypeDirection.directed
    },
    r2: {
        size: 1,
        color: 'rgba(0, 0, 0, 0.2)',
        forceLabel: false,
        loop: false,
        relationTypeUri: 'relType2',
        direction: RelationTypeDirection.directed
    },
    r3: {
        size: 1,
        color: 'rgba(0, 0, 0, 0.2)',
        forceLabel: false,
        hidden: true,
        loop: true,
        relationTypeUri: 'relType3',
        direction: RelationTypeDirection.directed
    },
    r4: {
        size: 1,
        color: 'rgba(0, 0, 0, 0.2)',
        forceLabel: true,
        loop: true,
        relationTypeUri: 'relType4',
        direction: RelationTypeDirection.directed
    }
};
var nodes = {
    node1: {
        size: 12,
        label: 'node 1',
        x: 0.5,
        y: 0.4
    },
    node2: {
        size: 12,
        label: 'node 2',
        x: 0.6,
        y: 0.6
    },
    node3: {
        size: 12,
        label: 'node 3',
        x: 0.8,
        y: 0.7
    }
};
var startHook = function (graph) { return renderHook(useLoopRenderer, { initialProps: graph }); };
var createGraph = function () {
    var graph = new MultiGraph();
    graph.import(graphJson);
    return graph;
};
describe('useLoopRenderer tests', function () {
    var getEdgeDisplayData = jest.fn().mockImplementation(function (edgeId) { return edges[edgeId]; });
    var getNodeDisplayData = jest.fn().mockImplementation(function (nodeId) { return nodes[nodeId]; });
    var scaleSize = jest.fn().mockImplementation(function (size) { return size * 2; });
    var framedGraphToViewport = jest.fn().mockImplementation(function (_a) {
        var x = _a.x, y = _a.y;
        return ({ x: x * 100, y: y * 100 });
    });
    var addRenderers = jest.fn();
    var canvases = { selfRelationLoops: {} };
    beforeAll(function () {
        drawSelfRelationLoop.mockImplementation(function (_context, edgeData) { return "path-".concat(edgeData.relationTypeUri); });
        useSigmaCustomRenderers.mockReturnValue({
            addRenderers: addRenderers,
            canvases: canvases
        });
        useSigma.mockReturnValue({
            getEdgeDisplayData: getEdgeDisplayData,
            getNodeDisplayData: getNodeDisplayData,
            scaleSize: scaleSize,
            framedGraphToViewport: framedGraphToViewport
        });
    });
    var getSelfRelationLoopsRenderer = function () {
        return addRenderers.mock.calls[addRenderers.mock.calls.length - 1][0].selfRelationLoops;
    };
    it('should draw self relations correctly and with correctly setting paths', function () {
        var graph = createGraph();
        var result = startHook(graph).result;
        expect(result.current).toEqual({
            selfRelationsPathsMap: {},
            selfRelationLoopsCanvas: canvases.selfRelationLoops
        });
        var renderer = getSelfRelationLoopsRenderer().renderer;
        act(function () {
            renderer('canvas');
        });
        expect(drawSelfRelationLoop).toHaveBeenCalledTimes(1);
        expect(drawSelfRelationLoop).toHaveBeenCalledWith('canvas', __assign(__assign({}, edges.r4), { size: 2 }), __assign(__assign({}, nodes.node1), { size: 24, x: 50, y: 40 }));
        expect(result.current).toEqual({
            selfRelationsPathsMap: { r4: 'path-relType4' },
            selfRelationLoopsCanvas: canvases.selfRelationLoops
        });
    });
});
