mattermost-mobile/app/screens/post_options/post_options.test.js
Miguel Alatzar 2d81b497cf [MM-23520] Port mattermost-redux (#4088)
* Remove mattermost-redux

* Move mm-redux files into app/redux

* Add @redux path to tsconfig.json

* Fix imports

* Install missing dependencies

* Fix tsc errors

* Fix i18n_utils test

* Fix more imports

* Remove redux websocket

* Fix tests

* Rename @redux

* Apply changes from mattermost-redux PR 1103

* Remove mattermost-redux mention in template

* Add missing imports

* Rename app/redux/ to app/mm-redux/

* Remove test file

* Fix fetching Sidebar GM profiles

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2020-04-17 21:12:09 -07:00

131 lines
4 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {Alert} from 'react-native';
import {shallow} from 'enzyme';
import Preferences from '@mm-redux/constants/preferences';
import PostOptions from './post_options';
jest.mock('react-intl');
describe('PostOptions', () => {
const actions = {
addReaction: jest.fn(),
deletePost: jest.fn(),
flagPost: jest.fn(),
pinPost: jest.fn(),
removePost: jest.fn(),
unflagPost: jest.fn(),
unpinPost: jest.fn(),
setUnreadPost: jest.fn(),
};
const post = {
root_id: 'root_id',
id: 'post_id',
message: 'message',
is_pinned: false,
channel_id: 'channel_id',
};
const baseProps = {
actions,
canAddReaction: true,
canReply: true,
canDelete: true,
canPin: true,
canEdit: true,
canMarkAsUnread: true,
canEditUntil: -1,
channelIsReadOnly: false,
currentTeamUrl: 'http://localhost:8065/team-name',
currentUserId: 'user1',
deviceHeight: 600,
hasBeenDeleted: false,
isFlagged: false,
isSystemMessage: false,
managedConfig: {},
post,
showAddReaction: true,
theme: Preferences.THEMES.default,
isLandscape: false,
};
function getWrapper(props = {}) {
return shallow(
<PostOptions
{...baseProps}
{...props}
/>,
{context: {intl: {formatMessage: ({defaultMessage}) => defaultMessage}}},
);
}
test('should match snapshot, showing all possible options', () => {
const wrapper = getWrapper();
expect(wrapper.getElement()).toMatchSnapshot();
});
test('should match snapshot, showing Delete option only for system message to user who has permission to delete', () => {
const wrapper = getWrapper({isSystemMessage: true});
expect(wrapper.getElement()).toMatchSnapshot();
});
test('should match snapshot, no option for system message to user who doesn\'t have the permission to delete', () => {
const wrapper = getWrapper({isSystemMessage: true, canDelete: false});
expect(wrapper.getElement()).toMatchSnapshot();
});
test('should load thread', () => {
const wrapper = getWrapper();
const instance = wrapper.instance();
instance.closeWithAnimation = jest.fn();
wrapper.findWhere((node) => node.key() === 'reply').simulate('press');
expect(instance.closeWithAnimation).toBeCalled();
});
test('should not show reply option', () => {
const wrapper = getWrapper({canReply: false});
expect(wrapper.findWhere((node) => node.key() === 'reply')).toMatchObject({});
});
test('should not show mark as unread option', () => {
const wrapper = getWrapper({canMarkAsUnread: false});
expect(wrapper.findWhere((node) => node.key() === 'markUnread')).toMatchObject({});
});
test('should remove post after delete', () => {
const wrapper = getWrapper();
wrapper.findWhere((node) => node.key() === 'delete').simulate('press');
expect(Alert.alert).toBeCalled();
// Trigger on press of Delete in the Alert
const closeWithAnimation = jest.spyOn(wrapper.instance(), 'closeWithAnimation');
Alert.alert.mock.calls[0][2][1].onPress();
expect(closeWithAnimation).toBeCalled();
// get the callback that gets called by closeWithAnimation
const callback = closeWithAnimation.mock.calls[0][0];
callback();
expect(actions.deletePost).toBeCalled();
expect(actions.removePost).toBeCalled();
});
test('should not show reply option without create_post permission', () => {
const wrapper = getWrapper({canPost: false});
expect(wrapper.findWhere((node) => node.key() === 'reply')).toMatchObject({});
});
});