Commit 871ec294 authored by Joram Wilander's avatar Joram Wilander Committed by JoramWilander

Switch to town square if last viewed channel is current channel (#2196)

parent 189f77eb
......@@ -2,7 +2,7 @@
// See LICENSE.txt for license information.
import {leaveChannel as leaveChannelRedux, joinChannel, unfavoriteChannel} from 'mattermost-redux/actions/channels';
import {getChannel, getChannelByName} from 'mattermost-redux/selectors/entities/channels';
import {getChannel, getChannelByName, getCurrentChannel, getDefaultChannel} from 'mattermost-redux/selectors/entities/channels';
import {getCurrentRelativeTeamUrl, getCurrentTeamId} from 'mattermost-redux/selectors/entities/teams';
import {getCurrentUserId, getUserByUsername} from 'mattermost-redux/selectors/entities/users';
import {getMyPreferences} from 'mattermost-redux/selectors/entities/preferences';
......@@ -28,8 +28,14 @@ export function checkAndSetMobileView() {
export function goToLastViewedChannel() {
return async (dispatch, getState) => {
const state = getState();
const lastViewedChannel = getChannelByName(state, getLastViewedChannelName(state));
return dispatch(switchToChannel(lastViewedChannel));
const currentChannel = getCurrentChannel(state);
let channelToSwitchTo = getChannelByName(state, getLastViewedChannelName(state));
if (currentChannel.id === channelToSwitchTo.id) {
channelToSwitchTo = getDefaultChannel(state);
}
return dispatch(switchToChannel(channelToSwitchTo));
};
}
......
......@@ -4,6 +4,7 @@
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import {General} from 'mattermost-redux/constants';
import {leaveChannel} from 'mattermost-redux/actions/channels';
import {browserHistory} from 'utils/browser_history';
......@@ -30,8 +31,13 @@ jest.mock('mattermost-redux/actions/channels', () => ({
}),
}));
jest.mock('selectors/local_storage', () => ({
getLastViewedChannelName: () => 'channel1',
}));
describe('channel view actions', () => {
const channel1 = {id: 'channelid1', name: 'channel1', display_name: 'Channel 1', type: 'O'};
const channel1 = {id: 'channelid1', name: 'channel1', display_name: 'Channel 1', type: 'O', team_id: 'teamid1'};
const townsquare = {id: 'channelid2', name: General.DEFAULT_CHANNEL, display_name: 'Town Square', type: 'O', team_id: 'teamid1'};
const gmChannel = {id: 'gmchannelid', name: 'gmchannel', display_name: 'GM Channel 1', type: 'G'};
const team1 = {id: 'teamid1', name: 'team1'};
......@@ -47,7 +53,8 @@ describe('channel view actions', () => {
teams: {teamid1: team1},
},
channels: {
channels: {channelid1: channel1, gmchannelid: gmChannel},
currentChannelId: 'channelid1',
channels: {channelid1: channel1, channelid2: townsquare, gmchannelid: gmChannel},
myMembers: {gmchannelid: {channel_id: 'gmchannelid', user_id: 'userid1'}},
},
general: {
......@@ -90,4 +97,11 @@ describe('channel view actions', () => {
expect(leaveChannel).toHaveBeenCalledWith('channelid');
});
});
describe('goToLastViewedChannel', () => {
test('should switch to town square if last viewed channel is current channel', async () => {
await store.dispatch(Actions.goToLastViewedChannel());
expect(browserHistory.push).toHaveBeenCalledWith(`/${team1.name}/channels/${General.DEFAULT_CHANNEL}`);
});
});
});
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