"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
var react_1 = require("@testing-library/react");
var ramda_1 = require("ramda");
var core_1 = require("@react-sigma/core");
var useSigmaCustomRenderers_1 = require("../../hooks/useSigmaCustomRenderers");
var label_1 = require("../../rendering/canvas/label");
var utils_1 = require("../../rendering/canvas/utils");
var useEdgeLabelsRenderer_1 = require("./useEdgeLabelsRenderer");
var defaultGraph_test_data_1 = require("./defaultGraph.test-data");
jest.mock('@react-sigma/core');
jest.mock('../../hooks/useSigmaCustomRenderers', function () { return ({
    useSigmaCustomRenderers: jest.fn()
}); });
jest.mock('../../rendering/canvas/utils', function () { return ({
    getCanvasMousePos: jest.fn()
}); });
jest.mock('../../rendering/canvas/label', function () { return ({
    drawLabel: jest.fn().mockImplementation(jest.requireActual('../../rendering/canvas/label').drawLabel)
}); });
describe('useEdgeLabelsRenderer tests', function () {
    var graph;
    var setUp = function (props) {
        graph = (props === null || props === void 0 ? void 0 : props.graph) || (0, defaultGraph_test_data_1.createDefaultGraph)();
        return (0, react_1.renderHook)(useEdgeLabelsRenderer_1.useEdgeLabelsRenderer, {
            initialProps: __assign({ graph: graph, onEdgeLabelClick: jest.fn() }, props)
        });
    };
    var getEdgeDisplayData = jest.fn().mockImplementation(function (edgeId) { return graph.getEdgeAttributes(edgeId); });
    var getNodeDisplayData = jest.fn().mockImplementation(function (nodeId) { return graph.getNodeAttributes(nodeId); });
    var renderers = {};
    var addRenderers = function (rendererObject) { return Object.assign(renderers, rendererObject); };
    var registerEvents = jest.fn();
    var canvas = document.createElement('canvas');
    var canvasContext = canvas.getContext('2d');
    beforeEach(function () {
        var _a;
        useSigmaCustomRenderers_1.useSigmaCustomRenderers.mockReturnValue({
            addRenderers: addRenderers,
            canvasesRef: {
                current: (_a = {},
                    _a[useEdgeLabelsRenderer_1.CUSTOM_EDGE_LABELS_CANVAS_NAME] = {
                        canvasElement: canvas,
                        context: canvasContext
                    },
                    _a)
            }
        });
        core_1.useSigma.mockReturnValue({
            getEdgeDisplayData: getEdgeDisplayData,
            getNodeDisplayData: getNodeDisplayData,
            framedGraphToViewport: function (x) { return x; },
            getSettings: function () { return ({
                labelSize: 12,
                labelColor: 'black'
            }); },
            scaleSize: function (x) { return x; }
        });
        core_1.useRegisterEvents.mockReturnValue(registerEvents);
        utils_1.getCanvasMousePos.mockImplementation(function (_canvas, event) { return ({ x: event.clientX, y: event.clientY }); });
    });
    afterEach(function () {
        jest.clearAllMocks();
    });
    var getEdgesLabelsRenderer = function () { return renderers[useEdgeLabelsRenderer_1.CUSTOM_EDGE_LABELS_CANVAS_NAME]; };
    var getDotsRenderer = function () { return renderers[useEdgeLabelsRenderer_1.EDGE_DOTS_CANVAS_NAME]; };
    var getHoversRenderer = function () { return renderers[useEdgeLabelsRenderer_1.HOVERS_CUSTOM_EDGE_LABELS_CANVAS_NAME]; };
    var getMousemove = function () { return (0, ramda_1.last)(registerEvents.mock.calls)[0].mousemove; };
    var getClick = function () { return (0, ramda_1.last)(registerEvents.mock.calls)[0].click; };
    var getCanvasContextEventTypes = function (context) {
        return context._events.map(function (event) { return event.type; });
    };
    it('should call drawLabel with correct params', function () {
        setUp();
        var renderer = getEdgesLabelsRenderer().renderer;
        (0, react_1.act)(function () {
            renderer(canvasContext);
        });
        expect(label_1.drawLabel).toHaveBeenCalledTimes(2);
        expect(label_1.drawLabel).toHaveBeenCalledWith(canvasContext, { label: '1', size: 4, x: 150, y: 100 }, { labelColor: 'black', labelSize: 12 }, { backgroundColor: 'rgba(250, 250, 250, 0.9)' });
        expect(label_1.drawLabel).toHaveBeenCalledWith(canvasContext, { label: '2', size: 4, x: 250, y: 100 }, { labelColor: 'black', labelSize: 12 }, { backgroundColor: 'rgba(250, 250, 250, 0.9)' });
    });
    it('should draw dot in the center of edge', function () {
        setUp();
        var canvas = document.createElement('canvas');
        var canvasContext = canvas.getContext('2d');
        var renderer = getDotsRenderer().renderer;
        (0, react_1.act)(function () {
            renderer(canvasContext);
        });
        expect(getCanvasContextEventTypes(canvasContext)).toEqual([
            'beginPath',
            'arc',
            'fill',
            'beginPath',
            'arc',
            'fill'
        ]);
        expect(canvasContext.arc).toHaveBeenCalledWith(150, 100, 2, 0, 2 * Math.PI);
        expect(canvasContext.arc).toHaveBeenCalledWith(250, 100, 2, 0, 2 * Math.PI);
    });
    it('should correctly draw a hover state of edge', function () {
        setUp();
        var hoversCanvas = document.createElement('canvas');
        var hoversCanvasContext = hoversCanvas.getContext('2d');
        var edgeLabelsRenderer = getEdgesLabelsRenderer().renderer;
        (0, react_1.act)(function () {
            edgeLabelsRenderer(canvasContext);
            var mousemove = getMousemove();
            var event = { original: { clientX: 170, clientY: 105 } };
            canvasContext.isPointInPath.mockReturnValueOnce(true);
            mousemove(event);
        });
        label_1.drawLabel.mockClear();
        (0, react_1.act)(function () {
            var hoversRenderer = getHoversRenderer().renderer;
            hoversRenderer(hoversCanvasContext);
        });
        expect(getCanvasContextEventTypes(hoversCanvasContext)).toEqual(expect.arrayContaining(['beginPath', 'arc', 'fill']));
        expect(hoversCanvasContext.arc).toHaveBeenCalledTimes(1);
        expect(hoversCanvasContext.arc).toHaveBeenCalledWith(150, 100, 2.8, 0, 2 * Math.PI);
        expect(label_1.drawLabel).toHaveBeenCalledTimes(1);
        expect(label_1.drawLabel).toHaveBeenCalledWith(hoversCanvasContext, { label: '1', size: 4, x: 150, y: 100 }, { labelColor: 'black', labelSize: 12 * 1.2 }, { backgroundColor: 'rgba(250, 250, 250, 1)' });
    });
    it('mousemove event should work correctly if cursor over edge caption', function () {
        var onEdgeLabelClick = jest.fn();
        var result = setUp({ onEdgeLabelClick: onEdgeLabelClick }).result;
        var renderer = getEdgesLabelsRenderer().renderer;
        expect(result.current).toBe(undefined);
        (0, react_1.act)(function () {
            renderer(canvasContext);
            var mousemove = getMousemove();
            var event = { original: { clientX: 170, clientY: 105 } };
            canvasContext.isPointInPath.mockReturnValueOnce(true);
            mousemove(event);
        });
        expect(result.current).toBe('hcp->hca');
        expect(onEdgeLabelClick).not.toHaveBeenCalled();
        (0, react_1.act)(function () {
            var click = getClick();
            click({ original: { clientX: 170, clientY: 105 } });
        });
        expect(onEdgeLabelClick).toHaveBeenCalledWith('hcp->hca', { clientX: 170, clientY: 105 });
    });
    it('mousemove event should work correctly if cursor not over edge caption', function () {
        var onEdgeLabelClick = jest.fn();
        var result = setUp({ onEdgeLabelClick: onEdgeLabelClick }).result;
        var renderer = getEdgesLabelsRenderer().renderer;
        expect(result.current).toBe(undefined);
        (0, react_1.act)(function () {
            renderer(canvasContext);
            var mousemove = getMousemove();
            var event = { original: { clientX: 0, clientY: 0 } };
            canvasContext.isPointInPath.mockReturnValueOnce(false);
            mousemove(event);
        });
        expect(result.current).toBe(null);
        expect(onEdgeLabelClick).not.toHaveBeenCalled();
        (0, react_1.act)(function () {
            var click = getClick();
            click({ original: { clientX: 0, clientY: 0 } });
        });
        expect(onEdgeLabelClick).not.toHaveBeenCalled();
    });
});
