"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 };
    }
};
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_2 = require("@testing-library/react");
var user_event_1 = __importDefault(require("@testing-library/user-event"));
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var PivotingAttributeContext_1 = require("../contexts/PivotingAttributeContext");
var PivotingTooltip_1 = require("./PivotingTooltip");
jest.mock('@reltio/mdm-sdk', function () { return (__assign(__assign({}, jest.requireActual('@reltio/mdm-sdk')), { getFilteredEntities: jest.fn(), getTotals: jest.fn() })); });
var mdmActions = { openPivotingPerspective: jest.fn() };
var attributes = [
    {
        name: 'Phone',
        uri: 'configuration/entityTypes/HCP/attributes/Phone2',
        type: 'Nested',
        attributes: [
            {
                uri: 'configuration/entityTypes/HCP/attributes/Phone2/attributes/Phone3',
                name: 'Phone3',
                type: 'String'
            }
        ]
    },
    {
        name: 'Phone3',
        uri: 'configuration/entityTypes/HCP/attributes/Phone3',
        type: 'Nested',
        attributes: [
            {
                uri: 'configuration/entityTypes/HCP/attributes/Phone3/attributes/Phone4',
                name: 'Phone4',
                type: 'String'
            }
        ]
    }
];
var metadata = {
    entityTypes: [
        {
            uri: 'configuration/entityTypes/HCP',
            attributes: attributes,
            analyticsAttributes: []
        },
        {
            uri: 'configuration/entityTypes/HCO',
            attributes: []
        }
    ]
};
var defaultMdmValues = {
    metadata: metadata,
    uiPath: 'https://reltio.com/nui',
    globalFilter: {
        query: "((equals(type,'configuration/entityTypes/HCP') and (range(updatedTime,1622458333452,1622717533451) or range(updatedTime,1622458333452,1622717533451))))",
        savedSearches: [],
        savedSearchesUris: []
    },
    entity: { uri: 'entityUri', type: 'configuration/entityTypes/HCP' }
};
var defaultProps = {
    value: {
        value: '8-800-555-35-35',
        ov: true,
        type: 'String',
        lookupCode: '8-800-555-35-35',
        lookupRawValue: '8-800-555-35-35',
        uri: 'configuration/entityTypes/HCP/attributes/Phone'
    },
    attributeType: {
        name: 'Phone',
        uri: 'configuration/entityTypes/HCP/attributes/Phone',
        type: 'String'
    },
    children: '8-800-555-35-35'
};
var defaultPivotingAttributeContextValue = [
    {
        uri: 'configuration/entityTypes/HCP/attributes/Phone',
        popup: { label: 'HCOs', entityType: 'configuration/entityTypes/HCO' }
    },
    {
        uri: 'configuration/entityTypes/HCP/attributes/Phone2',
        children: ['configuration/entityTypes/HCP/attributes/Phone2/attributes/Phone3']
    },
    {
        uri: 'configuration/entityTypes/HCP/attributes/Phone3'
    }
];
var setUp = function (_a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.props, props = _c === void 0 ? defaultProps : _c, _d = _b.mdmValues, mdmValues = _d === void 0 ? defaultMdmValues : _d, _e = _b.pivotingAttributeContextValue, pivotingAttributeContextValue = _e === void 0 ? defaultPivotingAttributeContextValue : _e;
    var user = user_event_1.default.setup();
    (0, react_2.render)(react_1.default.createElement(MdmModuleContext_1.MdmModuleProvider, { values: mdmValues, actions: mdmActions },
        react_1.default.createElement(PivotingAttributeContext_1.PivotingAttributeContext.Provider, { value: pivotingAttributeContextValue },
            react_1.default.createElement(PivotingTooltip_1.PivotingTooltip, __assign({}, props)))));
    return { user: user };
};
describe('Pivoting tooltip test', function () {
    beforeEach(function () {
        var allEntities = [
            { uri: 'entities/01Qi9Bv', type: 'configuration/entityTypes/HCP', label: 'hcp entity label 1' },
            { uri: 'entities/ba7sAl2', type: 'configuration/entityTypes/HCP', label: 'hcp entity label 2' },
            { uri: 'entities/Js28AmP', type: 'configuration/entityTypes/HCO', label: 'hco entity label 1' },
            { uri: 'entities/NsmA10S', type: 'configuration/entityTypes/HCO', label: 'hco entity label 2' },
            { uri: 'entities/Lk2Iq09', type: 'configuration/entityTypes/HCO', label: 'hco entity label 3' },
            { uri: 'entities/jsAm291', type: 'configuration/entityTypes/HCO', label: 'hco entity label 4' },
            { uri: 'entities/Nsb30A3', type: 'configuration/entityTypes/HCO', label: 'hco entity label 5' },
            { uri: 'entities/Bsn13La', type: 'configuration/entityTypes/HCO', label: 'hco entity label 6' },
            { uri: 'entities/JamdS14', type: 'configuration/entityTypes/HCO', label: 'hco entity label 7' }
        ];
        var getEntitites = function (_a) {
            var values = _a.filters[0].values;
            var entityType = values[0];
            return allEntities.filter(function (_a) {
                var type = _a.type;
                return type === entityType;
            });
        };
        mdm_sdk_1.getFilteredEntities.mockImplementation(getEntitites);
        mdm_sdk_1.getTotals.mockImplementation(function (params) { return ({ total: getEntitites(params).length }); });
    });
    afterEach(function () {
        jest.clearAllMocks();
    });
    it('should render tooltip for simple attributes correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user, tooltip, popper, params;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp().user;
                    return [4 /*yield*/, react_2.screen.findByText('8-800-555-35-35')];
                case 1:
                    tooltip = _a.sent();
                    expect(mdm_sdk_1.getTotals).toHaveBeenCalledTimes(0);
                    expect(mdm_sdk_1.getFilteredEntities).toHaveBeenCalledTimes(0);
                    return [4 /*yield*/, user.hover(tooltip)];
                case 2:
                    _a.sent();
                    popper = react_2.screen.getByRole('tooltip');
                    (0, react_2.within)(popper).getByText('Pivot profiles');
                    (0, react_2.within)(popper).getByText('7 items');
                    (0, react_2.within)(popper).getByText('HCOs');
                    (0, react_2.within)(popper).getByText('hco entity label 1');
                    (0, react_2.within)(popper).getByText('hco entity label 2');
                    (0, react_2.within)(popper).getByText('hco entity label 3');
                    (0, react_2.within)(popper).getByText('hco entity label 4');
                    (0, react_2.within)(popper).getByText('hco entity label 5');
                    (0, react_2.within)(popper).getByText('hco entity label 6');
                    (0, react_2.within)(popper).getByText('hco entity label 7');
                    (0, react_2.within)(popper).getByText('See all');
                    params = {
                        filters: [
                            { fieldName: 'type', filter: 'equals', values: ['configuration/entityTypes/HCO'] },
                            { fieldName: 'attributes.Phone', filter: 'equals', values: ['8-800-555-35-35'] }
                        ],
                        options: { globalFilter: defaultMdmValues.globalFilter.query, max: 6, searchOptions: '', sort: 'label' }
                    };
                    expect(mdm_sdk_1.getFilteredEntities).toHaveBeenCalledWith(params);
                    expect(mdm_sdk_1.getTotals).toHaveBeenCalledWith(params);
                    return [2 /*return*/];
            }
        });
    }); });
    it('should not render tooltip for simple attributes if it is not pivoting attribute', function () { return __awaiter(void 0, void 0, void 0, function () {
        var value, attributeType, props, user, _a, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    value = __assign(__assign({}, defaultProps.value), { uri: 'configuration/entityTypes/HCP/attributes/Phone123' });
                    attributeType = __assign(__assign({}, defaultProps.attributeType), { uri: 'configuration/entityTypes/HCP/attributes/Phone123', name: 'Phone123' });
                    props = __assign(__assign({}, defaultProps), { value: value, attributeType: attributeType });
                    user = setUp({ props: props }).user;
                    _b = (_a = user).hover;
                    return [4 /*yield*/, react_2.screen.findByText('8-800-555-35-35')];
                case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
                case 2:
                    _c.sent();
                    expect(react_2.screen.queryByRole('tooltip')).not.toBeInTheDocument();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should render tooltip for nested attributes correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
        var value, props, user, tooltip, popper, params;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    value = {
                        uri: 'configuration/entityTypes/HCP/attributes/Phone2',
                        ov: true,
                        value: {
                            Phone3: [
                                {
                                    value: '8-800-555-35-35',
                                    ov: true,
                                    type: 'String',
                                    lookupCode: '8-800-555-35-35',
                                    lookupRawValue: '8-800-555-35-35',
                                    uri: 'configuration/entityTypes/HCP/attributes/Phone2/attributes/Phone3'
                                }
                            ]
                        }
                    };
                    props = __assign(__assign({}, defaultProps), { value: value, attributeType: attributes[0] });
                    user = setUp({ props: props }).user;
                    return [4 /*yield*/, react_2.screen.findByText('8-800-555-35-35')];
                case 1:
                    tooltip = _a.sent();
                    expect(mdm_sdk_1.getTotals).toHaveBeenCalledTimes(0);
                    expect(mdm_sdk_1.getFilteredEntities).toHaveBeenCalledTimes(0);
                    return [4 /*yield*/, user.hover(tooltip)];
                case 2:
                    _a.sent();
                    popper = react_2.screen.getByRole('tooltip');
                    (0, react_2.within)(popper).getByText('Pivot profiles');
                    (0, react_2.within)(popper).getByText('2 items');
                    (0, react_2.within)(popper).getByText('hcp entity label 1');
                    (0, react_2.within)(popper).getByText('hcp entity label 2');
                    expect((0, react_2.within)(popper).queryByText('See all')).not.toBeInTheDocument();
                    params = {
                        filters: [
                            { fieldName: 'type', filter: 'equals', values: ['configuration/entityTypes/HCP'] },
                            { fieldName: 'attributes.Phone2.Phone3', filter: 'equals', values: ['8-800-555-35-35'] }
                        ],
                        options: { globalFilter: defaultMdmValues.globalFilter.query, max: 6, searchOptions: '', sort: 'label' }
                    };
                    expect(mdm_sdk_1.getFilteredEntities).toHaveBeenCalledWith(params);
                    expect(mdm_sdk_1.getTotals).toHaveBeenCalledWith(params);
                    return [2 /*return*/];
            }
        });
    }); });
    it('should not render tooltip for nested attributes if it is not pivoting attribute', function () { return __awaiter(void 0, void 0, void 0, function () {
        var value, attributeType, props, user, _a, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    value = {
                        uri: 'configuration/entityTypes/HCP/attributes/Phone4',
                        ov: true,
                        value: {
                            Phone3: [
                                {
                                    value: '8-800-555-35-35',
                                    ov: true,
                                    type: 'String',
                                    lookupCode: '8-800-555-35-35',
                                    lookupRawValue: '8-800-555-35-35',
                                    uri: 'configuration/entityTypes/HCP/attributes/Phone2/attributes/Phone3'
                                }
                            ]
                        }
                    };
                    attributeType = __assign(__assign({}, attributes[0]), { name: 'Phone4', uri: 'configuration/entityTypes/HCP/attributes/Phone4' });
                    props = __assign(__assign({}, defaultProps), { value: value, attributeType: attributeType });
                    user = setUp({ props: props }).user;
                    _b = (_a = user).hover;
                    return [4 /*yield*/, react_2.screen.findByText('8-800-555-35-35')];
                case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
                case 2:
                    _c.sent();
                    expect(react_2.screen.queryByRole('tooltip')).not.toBeInTheDocument();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should render tooltip for nested attributes if children do not exist in config of pivoting', function () { return __awaiter(void 0, void 0, void 0, function () {
        var value, props, user, _a, _b, popper, params;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    value = {
                        uri: 'configuration/entityTypes/HCP/attributes/Phone3',
                        ov: true,
                        value: {
                            Phone4: [
                                {
                                    value: '8-800-555-35-35',
                                    ov: true,
                                    type: 'String',
                                    lookupCode: '8-800-555-35-35',
                                    lookupRawValue: '8-800-555-35-35',
                                    uri: 'configuration/entityTypes/HCP/attributes/Phone3/attributes/Phone4'
                                }
                            ]
                        }
                    };
                    props = __assign(__assign({}, defaultProps), { value: value, attributeType: attributes[1] });
                    user = setUp({ props: props }).user;
                    _b = (_a = user).hover;
                    return [4 /*yield*/, react_2.screen.findByText('8-800-555-35-35')];
                case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
                case 2:
                    _c.sent();
                    popper = react_2.screen.getByRole('tooltip');
                    (0, react_2.within)(popper).getByText('Pivot profiles');
                    (0, react_2.within)(popper).getByText('2 items');
                    (0, react_2.within)(popper).getByText('hcp entity label 1');
                    (0, react_2.within)(popper).getByText('hcp entity label 2');
                    expect((0, react_2.within)(popper).queryByText('See all')).not.toBeInTheDocument();
                    params = {
                        filters: [
                            { fieldName: 'type', filter: 'equals', values: ['configuration/entityTypes/HCP'] },
                            { fieldName: 'attributes.Phone3.Phone4', filter: 'equals', values: ['8-800-555-35-35'] }
                        ],
                        options: { globalFilter: defaultMdmValues.globalFilter.query, max: 6, searchOptions: '', sort: 'label' }
                    };
                    expect(mdm_sdk_1.getFilteredEntities).toHaveBeenCalledWith(params);
                    expect(mdm_sdk_1.getTotals).toHaveBeenCalledWith(params);
                    return [2 /*return*/];
            }
        });
    }); });
    it('should not fail if pivoting attributes list is null', function () { return __awaiter(void 0, void 0, void 0, function () {
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    setUp({ pivotingAttributeContextValue: null });
                    return [4 /*yield*/, react_2.screen.findByText('8-800-555-35-35')];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should open pivoting perspective on simple attribute click correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user, _a, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    user = setUp().user;
                    _b = (_a = user).click;
                    return [4 /*yield*/, react_2.screen.findByText('8-800-555-35-35')];
                case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
                case 2:
                    _c.sent();
                    expect(mdmActions.openPivotingPerspective).toHaveBeenCalledWith({
                        attributeType: defaultProps.attributeType,
                        value: [{ Phone: '8-800-555-35-35' }]
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should open pivoting perspective on nested attribute click correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
        var value, attributeType, props, user, _a, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    value = {
                        uri: 'configuration/entityTypes/HCP/attributes/Phone2',
                        ov: true,
                        value: {
                            Phone3: [
                                {
                                    value: '8-800-555-35-35',
                                    ov: true,
                                    type: 'String',
                                    lookupCode: '8-800-555-35-35',
                                    lookupRawValue: '8-800-555-35-35',
                                    uri: 'configuration/entityTypes/HCP/attributes/Phone2/attributes/Phone3'
                                }
                            ]
                        }
                    };
                    attributeType = attributes[0];
                    props = __assign(__assign({}, defaultProps), { value: value, attributeType: attributeType });
                    user = setUp({ props: props }).user;
                    _b = (_a = user).click;
                    return [4 /*yield*/, react_2.screen.findByText('8-800-555-35-35')];
                case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
                case 2:
                    _c.sent();
                    expect(mdmActions.openPivotingPerspective).toHaveBeenCalledWith({
                        attributeType: attributeType,
                        value: [{ Phone3: ['8-800-555-35-35'] }]
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should open pivoting perspective on see all button click correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user, _a, _b;
        return __generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    user = setUp().user;
                    _b = (_a = user).hover;
                    return [4 /*yield*/, react_2.screen.findByText('8-800-555-35-35')];
                case 1: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
                case 2:
                    _c.sent();
                    return [4 /*yield*/, user.click(react_2.screen.getByText('See all'))];
                case 3:
                    _c.sent();
                    expect(mdmActions.openPivotingPerspective).toHaveBeenCalledWith({
                        attributeType: defaultProps.attributeType,
                        value: [{ Phone: '8-800-555-35-35' }]
                    });
                    return [2 /*return*/];
            }
        });
    }); });
});
