Unverified Commit 8ce06154 authored by Joram Wilander's avatar Joram Wilander Committed by cpanato

Fix rejoining channel from links in posts (#2190)

parent 083bb6de
...@@ -59,15 +59,16 @@ export function onChannelByIdentifierEnter({match, history}) { ...@@ -59,15 +59,16 @@ export function onChannelByIdentifierEnter({match, history}) {
}; };
} }
function goToChannelByChannelId(match, history) { export function goToChannelByChannelId(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 channelId = identifier.toLowerCase(); const channelId = identifier.toLowerCase();
let channel = getChannel(state, channelId); let channel = getChannel(state, channelId);
const member = state.entities.channels.myMembers[channelId];
const teamObj = getTeamByName(state, team); const teamObj = getTeamByName(state, team);
if (!channel) { if (!channel || !member) {
const {data, error} = await dispatch(joinChannel(getCurrentUserId(state), teamObj.id, channelId, null)); const {data, error} = await dispatch(joinChannel(getCurrentUserId(state), teamObj.id, channelId, null));
if (error) { if (error) {
handleChannelJoinError(match, history); handleChannelJoinError(match, history);
...@@ -98,8 +99,12 @@ export function goToChannelByChannelName(match, history) { ...@@ -98,8 +99,12 @@ export function goToChannelByChannelName(match, history) {
} }
let channel = getChannelsNameMapInTeam(state, teamObj.id)[channelName]; let channel = getChannelsNameMapInTeam(state, teamObj.id)[channelName];
let member;
if (channel) {
member = state.entities.channels.myMembers[channel.id];
}
if (!channel) { if (!channel || !member) {
const {data, error: joinError} = await dispatch(joinChannel(getCurrentUserId(state), teamObj.id, null, channelName)); const {data, error: joinError} = await dispatch(joinChannel(getCurrentUserId(state), teamObj.id, null, channelName));
if (joinError) { if (joinError) {
const {data: data2, error: getChannelError} = await dispatch(getChannelByNameAndTeamName(team, channelName, true)); const {data: data2, error: getChannelError} = await dispatch(getChannelByNameAndTeamName(team, channelName, true));
......
...@@ -4,24 +4,31 @@ ...@@ -4,24 +4,31 @@
import thunk from 'redux-thunk'; 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 {emitChannelClickEvent} from 'actions/global_actions.jsx'; import {emitChannelClickEvent} from 'actions/global_actions.jsx';
import {goToChannelByChannelName, goToDirectChannelByUserId, goToDirectChannelByUserIds} from 'components/channel_layout/channel_identifier_router/actions'; import {goToChannelByChannelName, goToDirectChannelByUserId, goToDirectChannelByUserIds, goToChannelByChannelId} 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(),
})); }));
jest.mock('mattermost-redux/actions/channels', () => ({
joinChannel: jest.fn(() => ({type: '', data: {channel: {id: 'channel_id3', name: 'achannel3', team_id: 'team_id1', type: 'O'}}})),
}));
const mockStore = configureStore([thunk]); const mockStore = configureStore([thunk]);
describe('Actions', () => { describe('Actions', () => {
const channel1 = {id: 'channel_id1', name: 'achannel', team_id: 'team_id1'}; const channel1 = {id: 'channel_id1', name: 'achannel', team_id: 'team_id1'};
const channel2 = {id: 'channel_id2', name: 'achannel', team_id: 'team_id2'}; 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 = { const initialState = {
entities: { entities: {
channels: { channels: {
currentChannelId: 'channel_id1', currentChannelId: 'channel_id1',
channels: {channel_id1: channel1, channel_id2: channel2}, 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'}}, 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']}, channelsInTeam: {team_id1: ['channel_id1'], team_id2: ['channel_id2']},
}, },
...@@ -42,10 +49,22 @@ describe('Actions', () => { ...@@ -42,10 +49,22 @@ describe('Actions', () => {
currentUserId: 'current_user_id', currentUserId: 'current_user_id',
profiles: {user_id2: {id: 'user_id2', username: 'user2'}}, profiles: {user_id2: {id: 'user_id2', username: 'user2'}},
}, },
general: {license: {IsLicensed: 'false'}}, 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', () => { describe('goToChannelByChannelName', () => {
test('switch to channel on different team with same name', async () => { test('switch to channel on different team with same name', async () => {
const testStore = await mockStore(initialState); const testStore = await mockStore(initialState);
...@@ -53,6 +72,14 @@ describe('Actions', () => { ...@@ -53,6 +72,14 @@ describe('Actions', () => {
await testStore.dispatch(goToChannelByChannelName({params: {team: 'team2', identifier: 'achannel'}}, {})); await testStore.dispatch(goToChannelByChannelName({params: {team: 'team2', identifier: 'achannel'}}, {}));
expect(emitChannelClickEvent).toHaveBeenCalledWith(channel2); 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', () => { describe('goToDirectChannelByUserId', () => {
......
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