"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 react_2 = require("@testing-library/react");
var user_event_1 = __importDefault(require("@testing-library/user-event"));
var core_1 = require("@dnd-kit/core");
var mdm_sdk_1 = require("@reltio/mdm-sdk");
var ComplexAttributeLabel_1 = require("../ComplexAttributeLabel");
var AutoExpandedAttributesContext_1 = require("../contexts/AutoExpandedAttributesContext");
var MdmModuleContext_1 = require("../contexts/MdmModuleContext");
var test_utils_1 = require("../test-utils");
var InlineComplexAttribute_1 = require("./InlineComplexAttribute");
jest.mock('../contexts/AutoExpandedAttributesContext', function () { return ({
    useAttributeAutoExpanded: jest.fn()
}); });
var attributeType = {
    uri: 'configuration/entityTypes/HCP/attributes/Nested',
    type: 'Nested',
    name: 'Nested',
    label: 'Nested label',
    attributes: [
        {
            uri: 'configuration/entityTypes/HCP/attributes/Nested/attributes/SubNested',
            type: 'String',
            name: 'SubNested',
            label: 'SubNested label'
        }
    ],
    access: ['READ', 'UPDATE', 'DELETE']
};
var attributeValue = {
    type: attributeType.uri,
    uri: 'entities/1/attributes/Nested/1',
    value: {
        SubNested: [{ uri: 'entities/1/attributes/Nested/1/SubNested/1', value: '1' }]
    },
    ov: true
};
var crosswalks = [
    {
        uri: 'entities/1/crosswalks/12jf9nL05',
        type: 'configuration/sources/Reltio',
        value: 'nQpZyQt',
        reltioLoadDate: '2021-06-11T08:24:31.726Z',
        createDate: '2018-11-19T12:14:00.735Z',
        updateDate: '2018-11-19T12:14:00.735Z',
        attributes: ['entities/1/attributes/Nested/1'],
        singleAttributeUpdateDates: {
            'entities/1/attributes/Nested/1': '2018-11-29T05:39:56.445Z'
        }
    },
    {
        uri: 'entities/1/crosswalks/1CwTinUzm',
        type: 'configuration/sources/AHA',
        value: 'AHA_LEV',
        reltioLoadDate: '2021-06-09T11:32:16.434Z',
        sourcePublishDate: '1970-01-01T00:00:00.000Z',
        createDate: '2018-11-28T13:16:18.540Z',
        updateDate: '2018-11-28T13:16:18.540Z',
        attributes: ['entities/1/attributes/Nested/1/SubNested/1'],
        singleAttributeUpdateDates: {
            'entities/1/attributes/Nested/1/SubNested/1': '2021-06-09T11:32:16.434Z'
        }
    }
];
var crosswalksMap = {
    'entities/1/attributes/Nested/1': [crosswalks[0]],
    'entities/1/attributes/Nested/1/SubNested/1': [crosswalks[1]]
};
var metadata = {
    entityTypes: [],
    sources: []
};
var defaultProps = {
    attributeType: attributeType,
    attributeValue: attributeValue,
    attributeTypesList: attributeType.attributes,
    label: react_1.default.createElement(ComplexAttributeLabel_1.ComplexAttributeLabel, null, '<No Label>'),
    crosswalksMap: crosswalksMap,
    onPin: jest.fn(),
    onIgnore: jest.fn(),
    onEdit: jest.fn(),
    onDelete: jest.fn(),
    onAdd: jest.fn(),
    emptyTempAttributeUris: []
};
var setUp = function (props) {
    if (props === void 0) { props = {}; }
    var user = user_event_1.default.setup({ skipHover: true });
    var Providers = function (_a) {
        var children = _a.children;
        return (react_1.default.createElement(MdmModuleContext_1.MdmModuleProvider, { values: { metadata: metadata } },
            react_1.default.createElement(core_1.DndContext, null, children)));
    };
    return __assign({ user: user }, (0, react_2.render)(react_1.default.createElement(InlineComplexAttribute_1.InlineComplexAttribute, __assign({}, defaultProps, props)), { wrapper: Providers }));
};
describe('InlineComplexAttribute tests', function () {
    beforeEach(function () {
        jest.clearAllMocks();
        AutoExpandedAttributesContext_1.useAttributeAutoExpanded.mockReturnValue(false);
    });
    it('should render correctly', function () {
        setUp();
        expect(react_2.screen.getByText('<No Label>')).toBeInTheDocument();
        var crosswalks = react_2.screen.getAllByTestId('crosswalk-icon');
        expect(crosswalks).toHaveLength(2);
        expect(react_2.screen.queryByLabelText('Pin')).not.toBeInTheDocument();
        expect(react_2.screen.queryByLabelText('Ignore')).not.toBeInTheDocument();
        expect(react_2.screen.queryByLabelText('Delete')).not.toBeInTheDocument();
        expect(react_2.screen.getByTestId('arrow-expand-button')).toBeInTheDocument();
        expect(react_2.screen.getByTestId('arrow-expand-button').querySelector('svg')).not.toHaveClass('expanded');
        expect(react_2.screen.queryByTestId('branch-decorator')).not.toBeInTheDocument();
    });
    it('should render actions on hover', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp().user;
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('inline-complex-attribute'))];
                case 1:
                    _a.sent();
                    return [4 /*yield*/, react_2.screen.findByLabelText('Pin')];
                case 2:
                    _a.sent();
                    expect(react_2.screen.getByLabelText('Pin').firstChild).not.toBeDisabled();
                    expect(react_2.screen.getByTestId('SvgPinOutlined')).toBeInTheDocument();
                    expect(react_2.screen.getByLabelText('Ignore').firstChild).not.toBeDisabled();
                    expect(react_2.screen.getByTestId('SvgIgnoredOutlined')).toBeInTheDocument();
                    expect(react_2.screen.getByLabelText('Delete')).toBeInTheDocument();
                    expect((0, test_utils_1.getMuiIconByName)('Delete')).toBeInTheDocument();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should show InlineAttributesList on arrow button click', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp().user;
                    return [4 /*yield*/, user.click(react_2.screen.getByTestId('arrow-expand-button'))];
                case 1:
                    _a.sent();
                    expect(react_2.screen.getByTestId('arrow-expand-button').querySelector('svg')).toHaveClass('expanded');
                    expect(react_2.screen.getByTestId('branch-decorator')).toBeInTheDocument();
                    expect(react_2.screen.getByText('SubNested label')).toBeInTheDocument();
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('value-index-0'))];
                case 2:
                    _a.sent();
                    return [4 /*yield*/, react_2.screen.findByLabelText('Pin')];
                case 3:
                    _a.sent();
                    expect(react_2.screen.getByLabelText('Pin').firstChild).not.toBeDisabled();
                    expect(react_2.screen.getByTestId('SvgPinOutlined')).toBeInTheDocument();
                    expect(react_2.screen.getByLabelText('Ignore').firstChild).not.toBeDisabled();
                    expect(react_2.screen.getByTestId('SvgIgnoredOutlined')).toBeInTheDocument();
                    expect(react_2.screen.getByLabelText('More actions')).toBeInTheDocument();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should show pin button if attribute value has pin: true', function () {
        setUp({ attributeValue: __assign(__assign({}, attributeValue), { pin: true }) });
        expect(react_2.screen.getByLabelText('Pin')).toBeInTheDocument();
        expect(react_2.screen.getByTestId('SvgPin')).toBeInTheDocument();
    });
    it('should show disabled pin button if attribute value has pin: true, but editing is disabled', function () {
        setUp({ attributeValue: __assign(__assign({}, attributeValue), { pin: true }), disableEdit: true });
        expect(react_2.screen.getByLabelText("You don't have permissions to pin").firstChild).toBeDisabled();
        expect(react_2.screen.getByTestId('SvgPin')).toBeInTheDocument();
    });
    it('should show ignore button if attribute value has ignored: true', function () {
        setUp({ attributeValue: __assign(__assign({}, attributeValue), { ignored: true }) });
        expect(react_2.screen.getByLabelText('Ignore')).toBeInTheDocument();
        expect(react_2.screen.getByTestId('SvgIgnored')).toBeInTheDocument();
    });
    it('should show disabled ignore button if attribute value has ignored: true but editing is disabled', function () {
        setUp({ attributeValue: __assign(__assign({}, attributeValue), { ignored: true }), disableEdit: true });
        expect(react_2.screen.getByLabelText("You don't have permissions to ignore").firstChild).toBeDisabled();
        expect(react_2.screen.getByTestId('SvgIgnored')).toBeInTheDocument();
    });
    it('should hide delete button, disallow children deletion if props.disableDelete is true', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    user = setUp({ disableDelete: true }).user;
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('inline-complex-attribute'))];
                case 1:
                    _a.sent();
                    return [4 /*yield*/, react_2.screen.findByLabelText('Pin')];
                case 2:
                    _a.sent();
                    expect(react_2.screen.queryByLabelText('Delete')).not.toBeInTheDocument();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should hide ignore and pin button, disallow children editing if props.disableEdit is true', 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({ disableEdit: true }).user;
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('inline-complex-attribute'))];
                case 1:
                    _c.sent();
                    return [4 /*yield*/, react_2.screen.findByLabelText('Delete')];
                case 2:
                    _c.sent();
                    expect(react_2.screen.queryByLabelText('Pin')).not.toBeInTheDocument();
                    expect(react_2.screen.queryByLabelText('Ignore')).not.toBeInTheDocument();
                    return [4 /*yield*/, user.click(react_2.screen.getByTestId('arrow-expand-button'))];
                case 3:
                    _c.sent();
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('value-index-0'))];
                case 4:
                    _c.sent();
                    _b = (_a = user).click;
                    return [4 /*yield*/, react_2.screen.findByLabelText('More actions')];
                case 5: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
                case 6:
                    _c.sent();
                    expect(react_2.screen.getByText('Edit').parentElement).toHaveAttribute('aria-disabled', 'true');
                    expect(react_2.screen.getByText('Delete').parentElement).not.toHaveAttribute('aria-disabled', 'true');
                    return [2 /*return*/];
            }
        });
    }); });
    it('should hide delete button, disable children deletion if attributeType has no delete permissions', 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({
                        attributeType: __assign(__assign({}, attributeType), { access: ['READ', 'UPDATE'] })
                    }).user;
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('inline-complex-attribute'))];
                case 1:
                    _c.sent();
                    return [4 /*yield*/, react_2.screen.findByLabelText('Pin')];
                case 2:
                    _c.sent();
                    expect(react_2.screen.queryByLabelText('Delete')).not.toBeInTheDocument();
                    return [4 /*yield*/, user.click(react_2.screen.getByTestId('arrow-expand-button'))];
                case 3:
                    _c.sent();
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('value-index-0'))];
                case 4:
                    _c.sent();
                    _b = (_a = user).click;
                    return [4 /*yield*/, react_2.screen.findByLabelText('More actions')];
                case 5: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
                case 6:
                    _c.sent();
                    expect(react_2.screen.getByText('Edit').parentElement).not.toHaveAttribute('aria-disabled', 'true');
                    expect(react_2.screen.getByText('Delete').parentElement).toHaveAttribute('aria-disabled', 'true');
                    return [2 /*return*/];
            }
        });
    }); });
    it('should hide ignore and pin button, disable children editing if attribute type has no update permissions', 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({
                        attributeType: __assign(__assign({}, attributeType), { access: ['READ', 'DELETE'] })
                    }).user;
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('inline-complex-attribute'))];
                case 1:
                    _c.sent();
                    return [4 /*yield*/, react_2.screen.findByLabelText('Delete')];
                case 2:
                    _c.sent();
                    expect(react_2.screen.queryByLabelText('Pin')).not.toBeInTheDocument();
                    expect(react_2.screen.queryByLabelText('Ignore')).not.toBeInTheDocument();
                    return [4 /*yield*/, user.click(react_2.screen.getByTestId('arrow-expand-button'))];
                case 3:
                    _c.sent();
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('value-index-0'))];
                case 4:
                    _c.sent();
                    _b = (_a = user).click;
                    return [4 /*yield*/, react_2.screen.findByLabelText('More actions')];
                case 5: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
                case 6:
                    _c.sent();
                    expect(react_2.screen.getByText('Edit').parentElement).toHaveAttribute('aria-disabled', 'true');
                    expect(react_2.screen.getByText('Delete').parentElement).not.toHaveAttribute('aria-disabled', 'true');
                    return [2 /*return*/];
            }
        });
    }); });
    it('should call props.onDelete on Delete 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;
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('inline-complex-attribute'))];
                case 1:
                    _c.sent();
                    _b = (_a = user).click;
                    return [4 /*yield*/, react_2.screen.findByLabelText('Delete')];
                case 2: return [4 /*yield*/, _b.apply(_a, [_c.sent()])];
                case 3:
                    _c.sent();
                    expect(defaultProps.onDelete).toHaveBeenCalledWith({
                        attributeType: attributeType,
                        attributeValue: attributeValue,
                        crosswalks: [crosswalks[0]]
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should call props.onPin on Pin 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;
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('inline-complex-attribute'))];
                case 1:
                    _c.sent();
                    _b = (_a = user).click;
                    return [4 /*yield*/, react_2.screen.findByLabelText('Pin')];
                case 2: return [4 /*yield*/, _b.apply(_a, [(_c.sent()).firstChild])];
                case 3:
                    _c.sent();
                    expect(defaultProps.onPin).toHaveBeenCalledWith({ attributeValue: attributeValue });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should call props.onIgnore on Ignore 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;
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('inline-complex-attribute'))];
                case 1:
                    _c.sent();
                    _b = (_a = user).click;
                    return [4 /*yield*/, react_2.screen.findByLabelText('Ignore')];
                case 2: return [4 /*yield*/, _b.apply(_a, [(_c.sent()).firstChild])];
                case 3:
                    _c.sent();
                    expect(defaultProps.onIgnore).toHaveBeenCalledWith({ attributeValue: attributeValue });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should render temporary attribute expanded by default', function () {
        AutoExpandedAttributesContext_1.useAttributeAutoExpanded.mockReturnValueOnce(true);
        setUp();
        expect(react_2.screen.getByTestId('arrow-expand-button').querySelector('svg')).toHaveClass('expanded');
        expect(react_2.screen.getByTestId('branch-decorator')).toBeInTheDocument();
        expect(react_2.screen.getByText('SubNested label')).toBeInTheDocument();
        expect(react_2.screen.getByTestId('simple-attributes-wrapper')).toBeInTheDocument();
    });
    it('should hide ignore and pin buttons for temporary attribute', function () { return __awaiter(void 0, void 0, void 0, function () {
        var user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    AutoExpandedAttributesContext_1.useAttributeAutoExpanded.mockReturnValueOnce(true);
                    user = setUp({ attributeValue: (0, mdm_sdk_1.createNewAttribute)({ attributeType: attributeType, parentUri: 'entities/1' }) }).user;
                    return [4 /*yield*/, user.hover(react_2.screen.getByTestId('inline-complex-attribute'))];
                case 1:
                    _a.sent();
                    return [4 /*yield*/, react_2.screen.findByLabelText('Delete')];
                case 2:
                    _a.sent();
                    expect(react_2.screen.queryByLabelText('Pin')).not.toBeInTheDocument();
                    expect(react_2.screen.queryByLabelText('Ignore')).not.toBeInTheDocument();
                    return [2 /*return*/];
            }
        });
    }); });
    it('should render attribute expanded by default if it is expanded via context', function () {
        AutoExpandedAttributesContext_1.useAttributeAutoExpanded.mockReturnValueOnce(true);
        setUp();
        expect(react_2.screen.getByTestId('arrow-expand-button').querySelector('svg')).toHaveClass('expanded');
        expect(react_2.screen.getByTestId('branch-decorator')).toBeInTheDocument();
        expect(react_2.screen.getByText('SubNested label')).toBeInTheDocument();
        expect(react_2.screen.getByTestId('simple-attributes-wrapper')).toBeInTheDocument();
    });
});
