"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 mdm_sdk_1 = require("@reltio/mdm-sdk");
var CollaborationContext_1 = require("../../../contexts/CollaborationContext");
var MdmModuleContext_1 = require("../../../contexts/MdmModuleContext");
var helpers_1 = require("../../helpers");
var Comment_1 = require("./Comment");
describe('Comment tests', function () {
    var TIME = 1620280049022;
    var createComment = function (status, createdBy) {
        if (status === void 0) { status = 'open'; }
        if (createdBy === void 0) { createdBy = 'vyacheslav.kovyazin'; }
        return {
            createdBy: createdBy,
            createdTime: TIME,
            status: status,
            objectId: 'entities/13JKIKAz/attributes/Name/2mlWuqDq5',
            objectType: mdm_sdk_1.CollaborationObjectTypes.ENTITY_ATTRIBUTE,
            content: 'comment',
            relatedObjectUris: ['entities/13JKIKAz'],
            commentId: 'commentId',
            namedUsers: [],
            replies: [],
            modifiedTime: 0,
            deleted: false,
            visibility: '',
            selfLink: '',
            permanentLink: ''
        };
    };
    var defaultCurrentComment = createComment();
    var defaultCommentsMap = {
        uri1: [
            {
                commentId: defaultCurrentComment.commentId,
                status: 'open',
                replies: 0
            }
        ]
    };
    var defaultUsername = 'vyacheslav.kovyazin';
    var entity = {
        type: 'configuration/entityTypes/HCP',
        uri: 'entities/02ZRRE9',
        attributes: {
            FirstName: [
                {
                    type: 'configuration/entityTypes/HCP/attributes/FirstName',
                    ov: true,
                    value: 'nameValue',
                    uri: 'entities/02ZRRE9/attributes/FirstName/15p60Tel'
                }
            ]
        }
    };
    var metadata = {
        entityTypes: [
            {
                uri: 'configuration/entityTypes/HCP',
                attributes: [
                    {
                        uri: 'configuration/entityTypes/HCP/attributes/FirstName',
                        type: 'String',
                        name: 'FirstName',
                        label: 'First Name',
                        access: ['READ', 'CREATE', 'UPDATE', 'INITIATE_CHANGE_REQUEST']
                    }
                ]
            }
        ]
    };
    var defaultMdmValues = {
        user: {
            username: defaultUsername
        },
        entity: entity,
        metadata: metadata
    };
    var defaultCollaborationContextValue = {
        commentsMap: defaultCommentsMap,
        totalCommentsCount: null,
        currentComment: defaultCurrentComment,
        createComment: function () { return Promise.resolve(); },
        editComment: function () { return Promise.resolve(); },
        editReply: function () { return Promise.resolve(); },
        deleteComment: jest.fn(),
        deleteReply: function () { },
        createReply: function () { return Promise.resolve(); },
        reOpenThread: jest.fn(),
        resolveThread: jest.fn(),
        getComment: jest.fn(),
        clearCurrentComment: jest.fn(),
        sending: false,
        loading: false,
        pageToken: '',
        getComments: function () { return Promise.resolve(); },
        comments: [],
        getCommentsCount: function () { return Promise.resolve(); },
        getCommentState: function () { return ({}); },
        updateCommentState: jest.fn(),
        clearCommentState: function () { }
    };
    var defaultProps = {
        comment: defaultCurrentComment,
        onReply: jest.fn(),
        uri: 'entities/13JKIKAz/attributes/Name/2mlWuqDq5',
        relatedObjectUris: ['entities/13JKIKAz'],
        objectType: mdm_sdk_1.CollaborationObjectTypes.ENTITY_ATTRIBUTE
    };
    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.collaborationContextValue, collaborationContextValue = _e === void 0 ? defaultCollaborationContextValue : _e;
        var Providers = function (_a) {
            var children = _a.children;
            return (react_1.default.createElement(MdmModuleContext_1.MdmModuleProvider, { values: mdmValues },
                react_1.default.createElement(CollaborationContext_1.CollaborationContext.Provider, { value: collaborationContextValue }, children)));
        };
        var user = user_event_1.default.setup();
        return __assign({ user: user }, (0, react_2.render)(react_1.default.createElement(Comment_1.Comment, __assign({}, props)), { wrapper: Providers }));
    };
    it('should render avatar correctly', function () {
        setUp();
        expect(react_2.screen.queryByText('VK')).toBeInTheDocument();
    });
    it('should render username correctly', function () {
        setUp();
        expect(react_2.screen.queryByText('vyacheslav.kovyazin')).toBeInTheDocument();
    });
    it('should render time correctly', function () {
        setUp();
        expect(react_2.screen.queryByText((0, helpers_1.formatDate)(TIME))).toBeInTheDocument();
    });
    it('should render CommentContent correctly', function () {
        setUp();
        expect(react_2.screen.queryByText('comment')).toBeInTheDocument();
    });
    it('should render buttons correctly if status is "open"', function () {
        setUp();
        expect(react_2.screen.queryByText('Reply')).toBeInTheDocument();
        expect(react_2.screen.queryByText('Resolve')).toBeInTheDocument();
        expect(react_2.screen.queryByText('Re-open')).not.toBeInTheDocument();
    });
    it('should render buttons correctly if status is "resolved"', function () {
        var props = __assign(__assign({}, defaultProps), { comment: createComment('resolved') });
        setUp({ props: props });
        expect(react_2.screen.queryByText('Reply')).toBeInTheDocument();
        expect(react_2.screen.queryByText('Re-open')).toBeInTheDocument();
        expect(react_2.screen.queryByText('Resolve')).not.toBeInTheDocument();
    });
    it('onReply handler should work correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
        var onReply, user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    onReply = defaultProps.onReply;
                    user = setUp().user;
                    expect(onReply).toHaveBeenCalledTimes(0);
                    return [4 /*yield*/, user.click(react_2.screen.getByText('Reply'))];
                case 1:
                    _a.sent();
                    expect(onReply).toHaveBeenCalledTimes(1);
                    return [2 /*return*/];
            }
        });
    }); });
    it('should disable "Resolve" button if sending is "true"', function () {
        var collaborationContextValue = __assign(__assign({}, defaultCollaborationContextValue), { sending: true });
        setUp({ collaborationContextValue: collaborationContextValue });
        expect(react_2.screen.getByText('Resolve')).toBeDisabled();
    });
    it('should disable "Re-Open" button if sending is "true"', function () {
        var collaborationContextValue = __assign(__assign({}, defaultCollaborationContextValue), { sending: true });
        var props = __assign(__assign({}, defaultProps), { comment: createComment('resolved') });
        setUp({ props: props, collaborationContextValue: collaborationContextValue });
        expect(react_2.screen.getByText('Re-open')).toBeDisabled();
    });
    it('handleResolve handler should work correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
        var resolveThread, user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    resolveThread = defaultCollaborationContextValue.resolveThread;
                    user = setUp().user;
                    return [4 /*yield*/, user.click(react_2.screen.getByText('Resolve'))];
                case 1:
                    _a.sent();
                    expect(resolveThread).toHaveBeenCalledWith({
                        commentId: 'commentId',
                        uri: 'entities/13JKIKAz/attributes/Name/2mlWuqDq5'
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('handleReOpen handler should work correctly', function () { return __awaiter(void 0, void 0, void 0, function () {
        var reOpenThread, props, user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    reOpenThread = defaultCollaborationContextValue.reOpenThread;
                    props = __assign(__assign({}, defaultProps), { comment: createComment('resolved') });
                    user = setUp({ props: props }).user;
                    return [4 /*yield*/, user.click(react_2.screen.getByText('Re-open'))];
                case 1:
                    _a.sent();
                    expect(reOpenThread).toHaveBeenCalledWith({
                        commentId: 'commentId',
                        uri: 'entities/13JKIKAz/attributes/Name/2mlWuqDq5'
                    });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should call updateCommentState with right params when onCancel called', function () { return __awaiter(void 0, void 0, void 0, function () {
        var updateCommentState, getCommentState, collaborationContextValue, user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    updateCommentState = defaultCollaborationContextValue.updateCommentState;
                    getCommentState = function () { return ({ isEditing: true }); };
                    collaborationContextValue = __assign(__assign({}, defaultCollaborationContextValue), { getCommentState: getCommentState });
                    user = setUp({ collaborationContextValue: collaborationContextValue }).user;
                    return [4 /*yield*/, user.click(react_2.screen.getByText('Cancel'))];
                case 1:
                    _a.sent();
                    expect(updateCommentState).toHaveBeenCalledWith(defaultProps.uri, 'comment/commentId', { isEditing: false });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should call updateCommentState with right params when updateIsEditingComment called', function () { return __awaiter(void 0, void 0, void 0, function () {
        var updateCommentState, getCommentState, collaborationContextValue, user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    updateCommentState = defaultCollaborationContextValue.updateCommentState;
                    getCommentState = function () { return ({ isEditing: false }); };
                    collaborationContextValue = __assign(__assign({}, defaultCollaborationContextValue), { getCommentState: getCommentState });
                    user = setUp({ collaborationContextValue: collaborationContextValue }).user;
                    return [4 /*yield*/, user.click(react_2.screen.getByLabelText('Open menu'))];
                case 1:
                    _a.sent();
                    return [4 /*yield*/, user.click(react_2.screen.getByText('Edit'))];
                case 2:
                    _a.sent();
                    expect(updateCommentState).toHaveBeenCalledWith(defaultProps.uri, 'comment/commentId', { isEditing: true });
                    return [2 /*return*/];
            }
        });
    }); });
    it('should render sendMessageArea insead of comment if isEditing equals true', function () {
        var getCommentState = function () { return ({ isEditing: true }); };
        var collaborationContextValue = __assign(__assign({}, defaultCollaborationContextValue), { getCommentState: getCommentState });
        setUp({ collaborationContextValue: collaborationContextValue });
        expect(react_2.screen.getByRole('textbox')).toHaveValue('comment');
    });
    it('should render DropDownMenuButton if comment created by current user', function () {
        setUp();
        expect(react_2.screen.queryByLabelText('Open menu')).toBeInTheDocument();
    });
    it('should not render DropDownMenuButton if comment created by another user', function () {
        var props = __assign(__assign({}, defaultProps), { comment: createComment('open', 'alexey.smirnov') });
        setUp({ props: props });
        expect(react_2.screen.queryByLabelText('Open menu')).not.toBeInTheDocument();
    });
    it('should hide Edit button in DropDownMenuButton if comment is resolved', function () { return __awaiter(void 0, void 0, void 0, function () {
        var props, user;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    props = __assign(__assign({}, defaultProps), { comment: createComment('resolved') });
                    user = setUp({ props: props }).user;
                    return [4 /*yield*/, user.click(react_2.screen.getByLabelText('Open menu'))];
                case 1:
                    _a.sent();
                    expect(react_2.screen.queryByText('Delete')).toBeInTheDocument();
                    expect(react_2.screen.queryByText('Edit')).not.toBeInTheDocument();
                    return [2 /*return*/];
            }
        });
    }); });
});
