This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
command.test.js
179 lines (145 loc) · 6.39 KB
/
command.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import {Client4} from 'mattermost-redux/client';
import * as Channels from 'mattermost-redux/selectors/entities/channels';
import * as Teams from 'mattermost-redux/selectors/entities/teams';
import {ActionTypes, Constants} from 'utils/constants';
import * as UserAgent from 'utils/user_agent';
import * as GlobalActions from 'actions/global_actions.jsx';
import * as Utils from 'utils/utils.jsx';
import UserSettingsModal from 'components/user_settings/modal';
import {executeCommand} from './command';
const mockStore = configureStore([thunk]);
const currentChannelId = '123';
const currentTeamId = '321';
const currentUserId = 'user123';
const initialState = {
entities: {
general: {
config: {
ExperimentalViewArchivedChannels: 'false',
},
},
channels: {
currentChannelId,
},
preferences: {
myPreferences: {},
},
teams: {
currentTeamId,
},
users: {
currentUserId,
profiles: {
user123: {
timezone: {
useAutomaticTimezone: true,
automaticTimezone: '',
manualTimezone: '',
},
},
},
},
},
views: {
rhs: {
rhsState: null,
searchTerms: '',
},
},
};
jest.mock('utils/user_agent');
jest.mock('actions/global_actions.jsx');
describe('executeCommand', () => {
let store;
beforeEach(async () => {
store = await mockStore(initialState);
});
describe('search', () => {
test('should fire the UPDATE_RHS_SEARCH_TERMS with the terms', async () => {
store.dispatch(executeCommand('/search foo bar', []));
expect(store.getActions()).toEqual([
{type: 'UPDATE_RHS_SEARCH_TERMS', terms: 'foo bar'},
{type: 'UPDATE_RHS_STATE', state: 'search'},
{type: 'UPDATE_RHS_SEARCH_RESULTS_TERMS', terms: ''},
{type: 'SEARCH_POSTS_REQUEST', isGettingMore: false},
]);
});
});
describe('shortcuts', () => {
UserAgent.isMobile = jest.fn();
test('should return error in case of mobile', async () => {
UserAgent.isMobile.mockReturnValueOnce(true);
const result = await store.dispatch(executeCommand('/shortcuts', []));
expect(result).toEqual({
error: {
message: 'Keyboard shortcuts are not supported on your device',
},
});
});
test('should call toggleShortcutsModal in case of no mobile', async () => {
UserAgent.isMobile.mockReturnValueOnce(false);
const result = await store.dispatch(executeCommand('/shortcuts', []));
expect(GlobalActions.toggleShortcutsModal).toHaveBeenCalled();
expect(result).toEqual({data: true});
});
});
describe('settings', () => {
test('should pass right modal params', async () => {
const result = await store.dispatch(executeCommand('/settings', {}));
expect(store.getActions()).toEqual([
{
type: ActionTypes.MODAL_OPEN,
dialogType: UserSettingsModal,
modalId: 'user_settings',
},
]);
expect(result).toEqual({data: true});
});
});
describe('collapse', () => {
test('call executeCommand with right params', async () => {
Client4.executeCommand = jest.fn().mockResolvedValue({});
await store.dispatch(executeCommand('/collapse', []));
expect(Client4.executeCommand).toHaveBeenCalledWith('/collapse ', []);
});
});
describe('leave', () => {
test('should send message when command typed in reply threads', async () => {
GlobalActions.sendEphemeralPost = jest.fn();
const result = await store.dispatch(executeCommand('/leave', {channel_id: 'channel_id', parent_id: 'parent_id'}));
expect(GlobalActions.sendEphemeralPost).
toHaveBeenCalledWith('/leave is not supported in reply threads. Use it in the center channel instead.',
'channel_id', 'parent_id');
expect(result).toEqual({data: true});
});
test('should show private modal if channel is private', async () => {
GlobalActions.showLeavePrivateChannelModal = jest.fn();
Channels.getCurrentChannel = jest.fn(() => ({type: Constants.PRIVATE_CHANNEL}));
const result = await store.dispatch(executeCommand('/leave', {}));
expect(GlobalActions.showLeavePrivateChannelModal).toHaveBeenCalledWith({type: Constants.PRIVATE_CHANNEL});
expect(result).toEqual({data: true});
});
test('should use user id as name if channel is dm', async () => {
Utils.getUserIdFromChannelName = jest.fn(() => 'userId');
Channels.getRedirectChannelNameForTeam = jest.fn(() => 'channel1');
Teams.getCurrentRelativeTeamUrl = jest.fn(() => '/team1');
Channels.getCurrentChannel = jest.fn(() => ({type: Constants.DM_CHANNEL}));
const result = await store.dispatch(executeCommand('/leave', {}));
expect(store.getActions()[0].data).toEqual([{category: 'direct_channel_show', name: 'userId', user_id: 'user123', value: 'false'}]);
expect(result).toEqual({data: true});
});
test('should use channel id as name if channel is gm', async () => {
Utils.getUserIdFromChannelName = jest.fn(() => 'userId');
Channels.getRedirectChannelNameForTeam = jest.fn(() => 'channel1');
Teams.getCurrentRelativeTeamUrl = jest.fn(() => '/team1');
Channels.getCurrentChannel = jest.fn(() => ({type: Constants.GM_CHANNEL, id: 'channelId'}));
const result = await store.dispatch(executeCommand('/leave', {}));
expect(store.getActions()[0].data).toEqual([{category: 'group_channel_show', name: 'channelId', user_id: 'user123', value: 'false'}]);
expect(result).toEqual({data: true});
});
});
});