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 };
    }
};
import React from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { createTheme, lighten, ThemeProvider } from '@mui/material/styles';
import { svg2Url } from '@reltio/mdm-sdk';
import { theme as reltioTheme } from '../../../constants';
import EntityMarker from './EntityMarker';
import { EntityMarkerContext } from '../../../contexts/EntityMarkerContext';
import { createHoveredMarker } from './icons/marker-hover.inline.svg';
import { createMarker } from './icons/marker.inline.svg';
import { MdmModuleProvider } from '../../../contexts/MdmModuleContext';
jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { debounce: jest.fn(function (fn, _delay) { return jest.requireActual('@reltio/mdm-sdk').debounce(fn, 0); }) })); });
var muiTheme = createTheme(reltioTheme);
jest.mock('@vis.gl/react-google-maps', function () {
    // The only reason why all the mock stuff inlcuding 'requre' inside the jest.mock is that
    // mocks with forwardRef doesn't work outside (in beforeEach/beforeAll)
    // eslint-disable-next-line @typescript-eslint/no-require-imports
    var React = require('react');
    var forwardRef = React.forwardRef, useRef = React.useRef, useImperativeHandle = React.useImperativeHandle;
    // eslint-disable-next-line react/prop-types
    var AdvancedMarker = forwardRef(function (_a, ref) {
        var children = _a.children, onClick = _a.onClick, onMouseEnter = _a.onMouseEnter, onMouseLeave = _a.onMouseLeave;
        var divRef = useRef(null);
        useImperativeHandle(ref, function () { return ({
            element: divRef.current
        }); });
        return (React.createElement("div", { ref: divRef, onClick: onClick, onMouseEnter: onMouseEnter, onMouseLeave: onMouseLeave }, children));
    });
    AdvancedMarker.displayName = 'AdvancedMarker';
    return __assign(__assign({}, jest.requireActual('@vis.gl/react-google-maps')), { AdvancedMarker: AdvancedMarker });
});
var markerColor = lighten(muiTheme.palette.primary.main, 0.3);
var hoveredMarkerIcon = svg2Url(createHoveredMarker(markerColor));
var markerIcon = svg2Url(createMarker(markerColor));
describe('EntityMarker', function () {
    var mockMarker = {
        id: 'someId',
        label: 'Entity 123',
        point: { lat: 10, lng: 20 },
        entity: { uri: 'entities/123', label: 'Entity label 123', type: 'hcp' }
    };
    var mockOnClick = jest.fn();
    var metadata = { entityTypes: [], relationTypes: [] };
    var defaultProps = { marker: mockMarker, onClick: mockOnClick };
    var setUp = function (props, contextValue) {
        if (props === void 0) { props = defaultProps; }
        if (contextValue === void 0) { contextValue = {}; }
        var Providers = function (_a) {
            var children = _a.children;
            return (React.createElement(ThemeProvider, { theme: muiTheme },
                React.createElement(MdmModuleProvider, { values: { metadata: metadata } }, children)));
        };
        return render(React.createElement(EntityMarkerContext.Provider, { value: contextValue },
            React.createElement(EntityMarker, __assign({}, props))), { wrapper: Providers });
    };
    afterEach(function () {
        jest.clearAllMocks();
    });
    it('should render correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            setUp();
            expect(screen.getByTestId('marker-icon')).toHaveAttribute('src', markerIcon);
            return [2 /*return*/];
        });
    }); });
    it('should call onClick when marker is clicked', function () { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    setUp();
                    return [4 /*yield*/, userEvent.click(screen.getByTestId('marker-icon'))];
                case 1:
                    _a.sent();
                    expect(mockOnClick).toHaveBeenCalledWith(mockMarker);
                    return [2 /*return*/];
            }
        });
    }); });
    it('should show ProfileCard on mouse enter and hide on mouse leave', function () { return __awaiter(void 0, void 0, void 0, function () {
        var _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    setUp();
                    return [4 /*yield*/, userEvent.hover(screen.getByTestId('marker-icon'))];
                case 1:
                    _b.sent();
                    _a = expect;
                    return [4 /*yield*/, screen.findByText(mockMarker.entity.label)];
                case 2:
                    _a.apply(void 0, [_b.sent()]).toBeInTheDocument();
                    return [4 /*yield*/, userEvent.unhover(screen.getByTestId('marker-icon'))];
                case 3:
                    _b.sent();
                    expect(screen.queryByText(mockMarker.entity.label)).not.toBeInTheDocument();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should use hovered marker icon when hovered', function () { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    setUp();
                    return [4 /*yield*/, userEvent.hover(screen.getByTestId('marker-icon'))];
                case 1:
                    _a.sent();
                    expect(screen.getByTestId('marker-icon')).toHaveAttribute('src', hoveredMarkerIcon);
                    return [2 /*return*/];
            }
        });
    }); });
    it('should use active marker icon when marker is active', function () { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            setUp(undefined, { activeEntityUri: mockMarker.entity.uri });
            expect(screen.getByTestId('marker-icon')).toHaveAttribute('src', hoveredMarkerIcon);
            return [2 /*return*/];
        });
    }); });
    it('should call clusterer with advancedMarker and marker id', function () { return __awaiter(void 0, void 0, void 0, function () {
        var clusterer;
        return __generator(this, function (_a) {
            clusterer = jest.fn();
            setUp(__assign(__assign({}, defaultProps), { clusterer: clusterer }));
            expect(clusterer).toHaveBeenCalledWith(expect.objectContaining({ element: expect.any(HTMLElement) }), mockMarker.id);
            return [2 /*return*/];
        });
    }); });
});
