Commit 189f77eb authored by Joram Wilander's avatar Joram Wilander Committed by JoramWilander

Fix routing to DMs by email (#2195)

parent 0bcede0a
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
import {joinChannel, getChannelByNameAndTeamName, markGroupChannelOpen} from 'mattermost-redux/actions/channels'; import {joinChannel, getChannelByNameAndTeamName, markGroupChannelOpen} from 'mattermost-redux/actions/channels';
import {getUser, getUserByUsername, getUserByEmail} from 'mattermost-redux/actions/users'; import {getUser, getUserByUsername, getUserByEmail} from 'mattermost-redux/actions/users';
import {getTeamByName} from 'mattermost-redux/selectors/entities/teams'; import {getTeamByName} from 'mattermost-redux/selectors/entities/teams';
import {getCurrentUserId, getUserByUsername as selectUserByUsername, getUser as selectUser} from 'mattermost-redux/selectors/entities/users'; import {getCurrentUserId, getUserByUsername as selectUserByUsername, getUser as selectUser, getUserByEmail as selectUserByEmail} from 'mattermost-redux/selectors/entities/users';
import {getChannelByName, getOtherChannels, getChannel, getChannelsNameMapInTeam} from 'mattermost-redux/selectors/entities/channels'; import {getChannelByName, getOtherChannels, getChannel, getChannelsNameMapInTeam} from 'mattermost-redux/selectors/entities/channels';
import {Constants} from 'utils/constants.jsx'; import {Constants} from 'utils/constants.jsx';
...@@ -193,13 +193,13 @@ export function goToDirectChannelByUserIds(match, history) { ...@@ -193,13 +193,13 @@ export function goToDirectChannelByUserIds(match, history) {
}; };
} }
function goToDirectChannelByEmail(match, history) { export function goToDirectChannelByEmail(match, history) {
return async (dispatch, getState) => { return async (dispatch, getState) => {
const state = getState(); const state = getState();
const {team, identifier} = match.params; const {team, identifier} = match.params;
const email = identifier.toLowerCase(); const email = identifier.toLowerCase();
let user = getUserByEmail(state, email); let user = selectUserByEmail(state, email);
if (!user) { if (!user) {
const {data, error} = await dispatch(getUserByEmail(email)); const {data, error} = await dispatch(getUserByEmail(email));
if (error) { if (error) {
......
...@@ -5,9 +5,16 @@ import thunk from 'redux-thunk'; ...@@ -5,9 +5,16 @@ import thunk from 'redux-thunk';
import configureStore from 'redux-mock-store'; import configureStore from 'redux-mock-store';
import {joinChannel} from 'mattermost-redux/actions/channels'; import {joinChannel} from 'mattermost-redux/actions/channels';
import {getUserByEmail} from 'mattermost-redux/actions/users';
import {emitChannelClickEvent} from 'actions/global_actions.jsx'; import {emitChannelClickEvent} from 'actions/global_actions.jsx';
import {goToChannelByChannelName, goToDirectChannelByUserId, goToDirectChannelByUserIds, goToChannelByChannelId} from 'components/channel_layout/channel_identifier_router/actions'; import {
goToChannelByChannelName,
goToDirectChannelByUserId,
goToDirectChannelByUserIds,
goToChannelByChannelId,
goToDirectChannelByEmail,
} from 'components/channel_layout/channel_identifier_router/actions';
jest.mock('actions/global_actions.jsx', () => ({ jest.mock('actions/global_actions.jsx', () => ({
emitChannelClickEvent: jest.fn(), emitChannelClickEvent: jest.fn(),
...@@ -17,6 +24,10 @@ jest.mock('mattermost-redux/actions/channels', () => ({ ...@@ -17,6 +24,10 @@ jest.mock('mattermost-redux/actions/channels', () => ({
joinChannel: jest.fn(() => ({type: '', data: {channel: {id: 'channel_id3', name: 'achannel3', team_id: 'team_id1', type: 'O'}}})), 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: 'user3@bladekick.com', username: 'user3'}})),
}));
const mockStore = configureStore([thunk]); const mockStore = configureStore([thunk]);
describe('Actions', () => { describe('Actions', () => {
...@@ -47,7 +58,7 @@ describe('Actions', () => { ...@@ -47,7 +58,7 @@ describe('Actions', () => {
}, },
users: { users: {
currentUserId: 'current_user_id', currentUserId: 'current_user_id',
profiles: {user_id2: {id: 'user_id2', username: 'user2'}}, profiles: {user_id2: {id: 'user_id2', username: 'user2', email: 'user2@bladekick.com'}},
}, },
general: {license: {IsLicensed: 'false'}, config: {}}, general: {license: {IsLicensed: 'false'}, config: {}},
preferences: {myPreferences: {}}, preferences: {myPreferences: {}},
...@@ -117,4 +128,24 @@ describe('Actions', () => { ...@@ -117,4 +128,24 @@ describe('Actions', () => {
expect(history.replace).toHaveBeenCalledWith('/team2/messages/@user2'); 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: 'user2@bladekick.com'}}, 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: 'user3@bladekick.com'}}, history));
expect(getUserByEmail).toHaveBeenCalledWith('user3@bladekick.com');
expect(history.replace).toHaveBeenCalledWith('/team1/messages/@user3');
});
});
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment