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
/
actions.test.js
151 lines (124 loc) · 6.8 KB
/
actions.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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import thunk from 'redux-thunk';
import configureStore from 'redux-mock-store';
import {joinChannel} from 'mattermost-redux/actions/channels';
import {getUserByEmail} from 'mattermost-redux/actions/users';
import {emitChannelClickEvent} from 'actions/global_actions.jsx';
import {
goToChannelByChannelName,
goToDirectChannelByUserId,
goToDirectChannelByUserIds,
goToChannelByChannelId,
goToDirectChannelByEmail,
} from 'components/channel_layout/channel_identifier_router/actions';
jest.mock('actions/global_actions.jsx', () => ({
emitChannelClickEvent: jest.fn(),
}));
jest.mock('mattermost-redux/actions/channels', () => ({
joinChannel: jest.fn(() => ({type: '', data: {channel: {id: 'channel_id3', name: 'achannel3', team_id: 'team_id1', type: 'O'}}})),
}));
jest.mock('mattermost-redux/actions/users', () => ({
getUserByEmail: jest.fn(() => ({type: '', data: {id: 'user_id3', email: '[email protected]', username: 'user3'}})),
}));
const mockStore = configureStore([thunk]);
describe('Actions', () => {
const channel1 = {id: 'channel_id1', name: 'achannel', team_id: 'team_id1'};
const channel2 = {id: 'channel_id2', name: 'achannel', team_id: 'team_id2'};
const channel3 = {id: 'channel_id3', name: 'achannel3', team_id: 'team_id1', type: 'O'};
const initialState = {
entities: {
channels: {
currentChannelId: 'channel_id1',
channels: {channel_id1: channel1, channel_id2: channel2, channel_id3: channel3},
myMembers: {channel_id1: {channel_id: 'channel_id1', user_id: 'current_user_id'}, channel_id2: {channel_id: 'channel_id2', user_id: 'current_user_id'}},
channelsInTeam: {team_id1: ['channel_id1'], team_id2: ['channel_id2']},
},
teams: {
currentTeamId: 'team_id1',
teams: {
team_id1: {
id: 'team_id1',
name: 'team1',
},
team_id2: {
id: 'team_id2',
name: 'team2',
},
},
},
users: {
currentUserId: 'current_user_id',
profiles: {user_id2: {id: 'user_id2', username: 'user2', email: '[email protected]'}},
},
general: {license: {IsLicensed: 'false'}, config: {}},
preferences: {myPreferences: {}},
},
};
describe('goToChannelByChannelId', () => {
test('switch to public channel we have locally but need to join', async () => {
const testStore = await mockStore(initialState);
const history = {replace: jest.fn()};
await testStore.dispatch(goToChannelByChannelId({params: {team: 'team1', identifier: 'channel_id3'}}, history));
expect(joinChannel).toHaveBeenCalledWith('current_user_id', 'team_id1', 'channel_id3', null);
expect(history.replace).toHaveBeenCalledWith('/team1/channels/achannel3');
});
});
describe('goToChannelByChannelName', () => {
test('switch to channel on different team with same name', async () => {
const testStore = await mockStore(initialState);
await testStore.dispatch(goToChannelByChannelName({params: {team: 'team2', identifier: 'achannel'}}, {}));
expect(emitChannelClickEvent).toHaveBeenCalledWith(channel2);
});
test('switch to public channel we have locally but need to join', async () => {
const testStore = await mockStore(initialState);
await testStore.dispatch(goToChannelByChannelName({params: {team: 'team1', identifier: 'achannel3'}}, {}));
expect(joinChannel).toHaveBeenCalledWith('current_user_id', 'team_id1', null, 'achannel3');
expect(emitChannelClickEvent).toHaveBeenCalledWith(channel3);
});
});
describe('goToDirectChannelByUserId', () => {
test('switch to a direct channel by user id on the same team', async () => {
const testStore = await mockStore(initialState);
const history = {replace: jest.fn()};
await testStore.dispatch(goToDirectChannelByUserId({params: {team: 'team1'}}, history, 'user_id2'));
expect(history.replace).toHaveBeenCalledWith('/team1/messages/@user2');
});
test('switch to a direct channel by user id on different team', async () => {
const testStore = await mockStore(initialState);
const history = {replace: jest.fn()};
await testStore.dispatch(goToDirectChannelByUserId({params: {team: 'team2'}}, history, 'user_id2'));
expect(history.replace).toHaveBeenCalledWith('/team2/messages/@user2');
});
});
describe('goToDirectChannelByUserIds', () => {
test('switch to a direct channel by name on the same team', async () => {
const testStore = await mockStore(initialState);
const history = {replace: jest.fn()};
await testStore.dispatch(goToDirectChannelByUserIds({params: {team: 'team1', identifier: 'current_user_id__user_id2'}}, history));
expect(history.replace).toHaveBeenCalledWith('/team1/messages/@user2');
});
test('switch to a direct channel by name on different team', async () => {
const testStore = await mockStore(initialState);
const history = {replace: jest.fn()};
await testStore.dispatch(goToDirectChannelByUserIds({params: {team: 'team2', identifier: 'current_user_id__user_id2'}}, history));
expect(history.replace).toHaveBeenCalledWith('/team2/messages/@user2');
});
});
describe('goToDirectChannelByEmail', () => {
test('switch to a direct channel by email with user already existing locally', async () => {
const testStore = await mockStore(initialState);
const history = {replace: jest.fn()};
await testStore.dispatch(goToDirectChannelByEmail({params: {team: 'team1', identifier: '[email protected]'}}, history));
expect(getUserByEmail).not.toHaveBeenCalled();
expect(history.replace).toHaveBeenCalledWith('/team1/messages/@user2');
});
test('switch to a direct channel by email with user not existing locally', async () => {
const testStore = await mockStore(initialState);
const history = {replace: jest.fn()};
await testStore.dispatch(goToDirectChannelByEmail({params: {team: 'team1', identifier: '[email protected]'}}, history));
expect(getUserByEmail).toHaveBeenCalledWith('[email protected]');
expect(history.replace).toHaveBeenCalledWith('/team1/messages/@user3');
});
});
});