// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import React from 'react'; import * as NavigationActions from '@actions/navigation'; import {BotTag, GuestTag} from '@components/tag'; import Preferences from '@mm-redux/constants/preferences'; import {CustomStatusDuration} from '@mm-redux/types/users'; import {shallowWithIntl} from '@test/intl-test-helper'; import UserProfile from './user_profile.js'; jest.mock('@utils/theme', () => { const original = jest.requireActual('../../utils/theme'); return { ...original, changeOpacity: jest.fn(), }; }); describe('user_profile', () => { const actions = { setChannelDisplayName: jest.fn(), makeDirectChannel: jest.fn(), loadBot: jest.fn(), getRemoteClusterInfo: jest.fn(), unsetCustomStatus: jest.fn(), }; const baseProps = { actions, config: { ShowEmailAddress: true, }, teammateNameDisplay: 'username', teams: [], theme: Preferences.THEMES.default, enableTimezone: false, isMilitaryTime: false, isMyUser: false, componentId: 'component-id', isCustomStatusExpired: false, isCustomStatusExpirySupported: false, }; const user = { email: 'test@test.com', first_name: 'test', id: '4hzdnk6mg7gepe7yze6m3domnc', last_name: 'fake', nickname: 'nick', username: 'fred', is_bot: false, }; const customStatus = { emoji: 'calendar', text: 'In a meeting', duration: CustomStatusDuration.DONT_CLEAR, }; const customStatusProps = { ...baseProps, customStatus, config: { EnableCustomUserStatuses: 'true', }, user, }; test('should match snapshot', () => { const wrapper = shallowWithIntl( , ); expect(wrapper.getElement()).toMatchSnapshot(); }); test('should match snapshot with custom status', () => { const wrapper = shallowWithIntl( , {context: {intl: {formatMessage: jest.fn()}}}, ); expect(wrapper.getElement()).toMatchSnapshot(); }); test('should match snapshot with custom status and isMyUser true', () => { const wrapper = shallowWithIntl( , {context: {intl: {formatMessage: jest.fn()}}}, ); expect(wrapper.getElement()).toMatchSnapshot(); }); it('should match snapshot with custom status expiry', () => { const wrapper = shallowWithIntl( , {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 = shallowWithIntl( , ); expect(wrapper.containsMatchingElement( , )).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 = shallowWithIntl( , ); expect(wrapper.containsMatchingElement( , )).toEqual(true); }); test('should push EditProfile', () => { jest.spyOn(global, 'requestAnimationFrame').mockImplementation((cb) => cb()); const goToScreen = jest.spyOn(NavigationActions, 'goToScreen'); const wrapper = shallowWithIntl( , ); wrapper.instance().goToEditProfile(); expect(goToScreen).toHaveBeenCalledTimes(1); }); test('should match snapshot when user is from remote', () => { const remoteUser = { ...user, remote_id: 'sr23g5h456', }; const clusterInfo = { display_name: 'Remote Organization', }; const wrapper = shallowWithIntl( , ); expect(wrapper.getElement()).toMatchSnapshot(); }); test('should call goToEditProfile', () => { const goToScreen = jest.spyOn(NavigationActions, 'goToScreen'); const wrapper = shallowWithIntl( , ); const event = {buttonId: wrapper.instance().rightButton.id}; wrapper.instance().navigationButtonPressed(event); expect(goToScreen).toHaveBeenCalledTimes(1); }); test('should call close', () => { const dismissModal = jest.spyOn(NavigationActions, 'dismissModal'); const wrapper = shallowWithIntl( , ); const close = jest.spyOn(wrapper.instance(), 'close'); const event = {buttonId: 'close-settings'}; wrapper.instance().navigationButtonPressed(event); expect(close).toHaveBeenCalledTimes(1); expect(dismissModal).toHaveBeenCalledTimes(1); }); });