mattermost-mobile/app/screens/user_profile/user_profile.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

202 lines
5.9 KiB
JavaScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React from 'react';
import {shallow} from 'enzyme';
import Preferences from '@mm-redux/constants/preferences';
import * as NavigationActions from 'app/actions/navigation';
import UserProfile from './user_profile.js';
import {BotTag, GuestTag} from 'app/components/tag';
jest.mock('react-intl');
jest.mock('app/utils/theme', () => {
const original = require.requireActual('app/utils/theme');
return {
...original,
changeOpacity: jest.fn(),
};
});
describe('user_profile', () => {
const actions = {
setChannelDisplayName: jest.fn(),
makeDirectChannel: jest.fn(),
loadBot: jest.fn(),
};
const baseProps = {
actions,
config: {
ShowEmailAddress: true,
},
teammateNameDisplay: 'username',
teams: [],
theme: Preferences.THEMES.default,
enableTimezone: false,
militaryTime: false,
isMyUser: false,
componentId: 'component-id',
isLandscape: false,
};
const user = {
email: 'test@test.com',
first_name: 'test',
id: '4hzdnk6mg7gepe7yze6m3domnc',
last_name: 'fake',
nickname: 'nick',
username: 'fred',
is_bot: false,
};
test('should match snapshot', () => {
const wrapper = shallow(
<UserProfile
{...baseProps}
user={user}
/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
expect(wrapper.getElement()).toMatchSnapshot();
});
test('should contain bot tag', () => {
const botUser = {
email: 'test@test.com',
first_name: 'test',
id: '4hzdnk6mg7gepe7yze6m3domnc',
last_name: 'fake',
nickname: 'nick',
username: 'fred',
is_bot: true,
};
const wrapper = shallow(
<UserProfile
{...baseProps}
user={botUser}
/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
expect(wrapper.containsMatchingElement(
<BotTag
show={true}
theme={baseProps.theme}
/>,
)).toEqual(true);
});
test('should contain guest tag', () => {
const guestUser = {
email: 'test@test.com',
first_name: 'test',
id: '4hzdnk6mg7gepe7yze6m3domnc',
last_name: 'fake',
nickname: 'nick',
username: 'fred',
roles: 'system_guest',
};
const wrapper = shallow(
<UserProfile
{...baseProps}
user={guestUser}
/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
expect(wrapper.containsMatchingElement(
<GuestTag
show={true}
theme={baseProps.theme}
/>,
)).toEqual(true);
});
test('should push EditProfile', () => {
jest.spyOn(global, 'requestAnimationFrame').mockImplementation((cb) => cb());
const goToScreen = jest.spyOn(NavigationActions, 'goToScreen');
const wrapper = shallow(
<UserProfile
{...baseProps}
user={user}
/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
wrapper.instance().goToEditProfile();
expect(goToScreen).toHaveBeenCalledTimes(1);
});
test('should call goToEditProfile', () => {
const goToScreen = jest.spyOn(NavigationActions, 'goToScreen');
const wrapper = shallow(
<UserProfile
{...baseProps}
user={user}
/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
const event = {buttonId: wrapper.instance().rightButton.id};
wrapper.instance().navigationButtonPressed(event);
expect(goToScreen).toHaveBeenCalledTimes(1);
});
test('close should dismiss modal when fromSettings is true', async () => {
const dismissModal = jest.spyOn(NavigationActions, 'dismissModal');
const dismissAllModals = jest.spyOn(NavigationActions, 'dismissAllModals');
const popToRoot = jest.spyOn(NavigationActions, 'popToRoot');
const props = {...baseProps, fromSettings: true};
const wrapper = shallow(
<UserProfile
{...props}
user={user}
/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
await wrapper.instance().close();
expect(dismissModal).toHaveBeenCalledTimes(1);
expect(dismissAllModals).toHaveBeenCalledTimes(0);
expect(popToRoot).toHaveBeenCalledTimes(0);
});
test('close should dismiss all modals and pop to root when fromSettings is false', async () => {
const dismissModal = jest.spyOn(NavigationActions, 'dismissModal');
const popToRoot = jest.spyOn(NavigationActions, 'popToRoot');
const props = {...baseProps, fromSettings: false};
const wrapper = shallow(
<UserProfile
{...props}
user={user}
/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
await wrapper.instance().close();
expect(dismissModal).toHaveBeenCalledTimes(0);
expect(popToRoot).toHaveBeenCalledTimes(1);
});
test('should call close', () => {
const wrapper = shallow(
<UserProfile
{...baseProps}
user={user}
/>,
{context: {intl: {formatMessage: jest.fn()}}},
);
const close = jest.spyOn(wrapper.instance(), 'close');
const event = {buttonId: 'close-settings'};
wrapper.instance().navigationButtonPressed(event);
expect(close).toHaveBeenCalledTimes(1);
});
});