From 5f2d840f2748d601fb17541a95efec660cad4c01 Mon Sep 17 00:00:00 2001 From: Mattermost Build Date: Tue, 24 Sep 2019 21:35:47 +0200 Subject: [PATCH] Automated cherry pick of #3289 (#3304) * Properly determine if channel is archived * Remove check on ownProps.channelIsArchived --- app/components/post_body/index.js | 5 +- app/components/post_body/index.test.js | 116 +++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 app/components/post_body/index.test.js diff --git a/app/components/post_body/index.js b/app/components/post_body/index.js index 5d4b9ef26..5c6d21f27 100644 --- a/app/components/post_body/index.js +++ b/app/components/post_body/index.js @@ -32,7 +32,7 @@ import PostBody from './post_body'; const POST_TIMEOUT = 20000; -function makeMapStateToProps() { +export function makeMapStateToProps() { const memoizeHasEmojisOnly = memoizeResult((message, customEmojis) => hasEmojisOnly(message, customEmojis)); const getReactionsForPost = makeGetReactionsForPost(); @@ -61,9 +61,10 @@ function makeMapStateToProps() { const roles = getCurrentUserId(state) ? getCurrentUserRoles(state) : ''; const isAdmin = checkIsAdmin(roles); const isSystemAdmin = checkIsSystemAdmin(roles); + const channelIsArchived = channel?.delete_at !== 0; //eslint-disable-line camelcase let canDelete = false; - if (post && !ownProps.channelIsArchived) { + if (post && !channelIsArchived) { canDelete = canDeletePost(state, config, license, currentTeamId, currentChannelId, currentUserId, post, isAdmin, isSystemAdmin); } diff --git a/app/components/post_body/index.test.js b/app/components/post_body/index.test.js new file mode 100644 index 000000000..dd329fe0f --- /dev/null +++ b/app/components/post_body/index.test.js @@ -0,0 +1,116 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import {getChannel} from 'mattermost-redux/selectors/entities/channels'; +import * as PostUtils from 'mattermost-redux/utils/post_utils'; + +import {makeMapStateToProps} from './index.js'; + +jest.mock('mattermost-redux/selectors/entities/channels', () => { + const channels = require.requireActual('mattermost-redux/selectors/entities/channels'); + + return { + ...channels, + getChannel: jest.fn(), + canManageChannelMembers: jest.fn(), + getCurrentChannelId: jest.fn(), + }; +}); + +jest.mock('mattermost-redux/selectors/entities/preferences', () => { + const preferences = require.requireActual('mattermost-redux/selectors/entities/preferences'); + return { + ...preferences, + getTheme: jest.fn(), + }; +}); + +jest.mock('mattermost-redux/selectors/entities/general', () => { + const general = require.requireActual('mattermost-redux/selectors/entities/general'); + return { + ...general, + getConfig: jest.fn(), + getLicense: jest.fn().mockReturnValue({}), + }; +}); + +jest.mock('mattermost-redux/selectors/entities/users', () => { + const users = require.requireActual('mattermost-redux/selectors/entities/users'); + return { + ...users, + getCurrentUserId: jest.fn(), + getCurrentUserRoles: jest.fn(), + }; +}); + +jest.mock('mattermost-redux/selectors/entities/teams', () => { + const teams = require.requireActual('mattermost-redux/selectors/entities/teams'); + return { + ...teams, + getCurrentTeamId: jest.fn(), + }; +}); + +jest.mock('mattermost-redux/selectors/entities/emojis', () => { + const emojis = require.requireActual('mattermost-redux/selectors/entities/emojis'); + return { + ...emojis, + getCustomEmojisByName: jest.fn(), + }; +}); + +jest.mock('mattermost-redux/selectors/entities/posts', () => { + const posts = require.requireActual('mattermost-redux/selectors/entities/posts'); + return { + ...posts, + makeGetReactionsForPost: () => jest.fn(), + }; +}); + +jest.mock('app/selectors/device', () => ({ + getDimensions: jest.fn(), +})); + +describe('makeMapStateToProps', () => { + const defaultState = { + entities: { + general: { + serverVersion: '', + }, + }, + }; + const defaultOwnProps = { + post: {}, + }; + + test('should not call canDeletePost if post is not defined', () => { + const canDeletePost = jest.spyOn(PostUtils, 'canDeletePost'); + const mapStateToProps = makeMapStateToProps(); + const ownProps = { + post: '', + }; + + const props = mapStateToProps(defaultState, ownProps); + expect(props.canDelete).toBe(false); + expect(canDeletePost).not.toHaveBeenCalled(); + }); + + test('should not call canDeletePost if post is defined and channel is archived', () => { + const canDeletePost = jest.spyOn(PostUtils, 'canDeletePost'); + const mapStateToProps = makeMapStateToProps(); + + getChannel.mockReturnValueOnce({delete_at: 1}); //eslint-disable-line camelcase + const props = mapStateToProps(defaultState, defaultOwnProps); + expect(props.canDelete).toBe(false); + expect(canDeletePost).not.toHaveBeenCalled(); + }); + + test('should call canDeletePost if post is defined and channel is not archived', () => { + const canDeletePost = jest.spyOn(PostUtils, 'canDeletePost'); + const mapStateToProps = makeMapStateToProps(); + + getChannel.mockReturnValue({delete_at: 0}); //eslint-disable-line camelcase + mapStateToProps(defaultState, defaultOwnProps); + expect(canDeletePost).toHaveBeenCalledTimes(1); + }); +}); \ No newline at end of file