UNPKG

mattermost-redux

Version:

Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client

1,633 lines (1,446 loc) 117 kB
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import assert from 'assert'; import {General, Preferences, Permissions} from '../../constants'; import {CategoryTypes} from 'constants/channel_categories'; import mergeObjects from 'test/merge_objects'; import TestHelper from 'test/test_helper'; import {sortChannelsByDisplayName, getDirectChannelName} from 'utils/channel_utils'; import deepFreezeAndThrowOnMutation from 'utils/deep_freeze'; import * as Selectors from './channels'; const sortUsernames = (a, b) => a.localeCompare(b, General.DEFAULT_LOCALE, {numeric: true}); describe('Selectors.Channels.getChannelsInCurrentTeam', () => { const team1 = TestHelper.fakeTeamWithId(); const team2 = TestHelper.fakeTeamWithId(); it('should return channels in current team', () => { const user = TestHelper.fakeUserWithId(); const profiles = { [user.id]: user, }; const channel1 = TestHelper.fakeChannelWithId(team1.id); const channel2 = TestHelper.fakeChannelWithId(team2.id); const channel3 = TestHelper.fakeChannelWithId(team1.id); const channel4 = TestHelper.fakeChannelWithId(''); const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, [channel4.id]: channel4, }; const channelsInTeam = { [team1.id]: [channel1.id, channel3.id], [team2.id]: [channel2.id], '': [channel4.id], }; const testState = deepFreezeAndThrowOnMutation({ entities: { users: { currentUserId: user.id, profiles, }, teams: { currentTeamId: team1.id, }, channels: { channels, channelsInTeam, }, }, }); const channelsInCurrentTeam = [channel1, channel3].sort(sortChannelsByDisplayName.bind(null, [])); assert.deepEqual(Selectors.getChannelsInCurrentTeam(testState), channelsInCurrentTeam); }); it('should order by user locale', () => { const userDe = { ...TestHelper.fakeUserWithId(), locale: 'de', }; const userSv = { ...TestHelper.fakeUserWithId(), locale: 'sv', }; const profilesDe = { [userDe.id]: userDe, }; const profilesSv = { [userSv.id]: userSv, }; const channel1 = { ...TestHelper.fakeChannelWithId(team1.id), display_name: 'z', }; const channel2 = { ...TestHelper.fakeChannelWithId(team1.id), display_name: 'ä', }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, }; const channelsInTeam = { [team1.id]: [channel1.id, channel2.id], }; const testStateDe = deepFreezeAndThrowOnMutation({ entities: { users: { currentUserId: userDe.id, profiles: profilesDe, }, teams: { currentTeamId: team1.id, }, channels: { channels, channelsInTeam, }, }, }); const testStateSv = deepFreezeAndThrowOnMutation({ entities: { users: { currentUserId: userSv.id, profiles: profilesSv, }, teams: { currentTeamId: team1.id, }, channels: { channels, channelsInTeam, }, }, }); const channelsInCurrentTeamDe = [channel1, channel2].sort(sortChannelsByDisplayName.bind(null, userDe.locale)); const channelsInCurrentTeamSv = [channel1, channel2].sort(sortChannelsByDisplayName.bind(null, userSv.locale)); assert.deepEqual(Selectors.getChannelsInCurrentTeam(testStateDe), channelsInCurrentTeamDe); assert.deepEqual(Selectors.getChannelsInCurrentTeam(testStateSv), channelsInCurrentTeamSv); }); }); describe('Selectors.Channels.getMyChannels', () => { const team1 = TestHelper.fakeTeamWithId(); const team2 = TestHelper.fakeTeamWithId(); const user = TestHelper.fakeUserWithId(); const user2 = TestHelper.fakeUserWithId(); const user3 = TestHelper.fakeUserWithId(); const profiles = { [user.id]: user, [user2.id]: user2, [user3.id]: user3, }; const channel1 = { ...TestHelper.fakeChannelWithId(team1.id), display_name: 'Channel Name', }; const channel2 = { ...TestHelper.fakeChannelWithId(team2.id), display_name: 'Channel Name', }; const channel3 = { ...TestHelper.fakeChannelWithId(team1.id), display_name: 'Channel Name', }; const channel4 = { ...TestHelper.fakeChannelWithId(''), display_name: 'Channel Name', type: General.DM_CHANNEL, name: getDirectChannelName(user.id, user2.id), }; const channel5 = { ...TestHelper.fakeChannelWithId(team1.id), display_name: [user.username, user2.username, user3.username].join(', '), type: General.GM_CHANNEL, name: '', }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, [channel4.id]: channel4, [channel5.id]: channel5, }; const channelsInTeam = { [team1.id]: [channel1.id, channel3.id], [team2.id]: [channel2.id], '': [channel4.id, channel5.id], }; const myMembers = { [channel1.id]: {}, [channel3.id]: {}, [channel4.id]: {}, [channel5.id]: {}, }; const testState = deepFreezeAndThrowOnMutation({ entities: { users: { currentUserId: user.id, profiles, statuses: {}, profilesInChannel: { [channel4.id]: new Set([user.id, user2.id]), [channel5.id]: new Set([user.id, user2.id, user3.id]), }, }, teams: { currentTeamId: team1.id, }, channels: { channels, channelsInTeam, myMembers, }, preferences: { myPreferences: {}, }, general: { config: {}, }, }, }); it('get my channels in current team and DMs', () => { const channelsInCurrentTeam = [channel1, channel3].sort(sortChannelsByDisplayName.bind(null, [])); assert.deepEqual(Selectors.getMyChannels(testState), [ ...channelsInCurrentTeam, {...channel4, display_name: user2.username, status: 'offline', teammate_id: user2.id}, {...channel5, display_name: [user2.username, user3.username].sort(sortUsernames).join(', ')}, ]); }); }); describe('Selectors.Channels.getMembersInCurrentChannel', () => { const channel1 = TestHelper.fakeChannelWithId(''); const user = TestHelper.fakeUserWithId(); const user2 = TestHelper.fakeUserWithId(); const user3 = TestHelper.fakeUserWithId(); const membersInChannel = { [channel1.id]: { [user.id]: {}, [user2.id]: {}, [user3.id]: {}, }, }; const testState = deepFreezeAndThrowOnMutation({ entities: { channels: { currentChannelId: channel1.id, membersInChannel, }, }, }); it('should return members in current channel', () => { assert.deepEqual(Selectors.getMembersInCurrentChannel(testState), membersInChannel[channel1.id]); }); }); describe('Selectors.Channels.getOtherChannels', () => { const team1 = TestHelper.fakeTeamWithId(); const team2 = TestHelper.fakeTeamWithId(); const user = TestHelper.fakeUserWithId(); const profiles = { [user.id]: user, }; const channel1 = { ...TestHelper.fakeChannelWithId(team1.id), display_name: 'Channel Name', type: General.OPEN_CHANNEL, }; const channel2 = { ...TestHelper.fakeChannelWithId(team2.id), display_name: 'Channel Name', type: General.OPEN_CHANNEL, }; const channel3 = { ...TestHelper.fakeChannelWithId(team1.id), display_name: 'Channel Name', type: General.PRIVATE_CHANNEL, }; const channel4 = { ...TestHelper.fakeChannelWithId(''), display_name: 'Channel Name', type: General.DM_CHANNEL, }; const channel5 = { ...TestHelper.fakeChannelWithId(''), display_name: 'Channel Name', type: General.OPEN_CHANNEL, delete_at: 444, }; const channel6 = { ...TestHelper.fakeChannelWithId(team1.id), display_name: 'Channel Name', type: General.OPEN_CHANNEL, }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, [channel4.id]: channel4, [channel5.id]: channel5, [channel6.id]: channel6, }; const channelsInTeam = { [team1.id]: [channel1.id, channel3.id, channel5.id, channel6.id], [team2.id]: [channel2.id], '': [channel4.id], }; const myMembers = { [channel4.id]: {}, [channel6.id]: {}, }; const testState = deepFreezeAndThrowOnMutation({ entities: { users: { currentUserId: user.id, profiles, }, teams: { currentTeamId: team1.id, }, channels: { channels, channelsInTeam, myMembers, }, }, }); it('get public channels not member of', () => { assert.deepEqual(Selectors.getOtherChannels(testState), [channel1, channel5].sort(sortChannelsByDisplayName.bind(null, []))); }); it('get public, unarchived channels not member of', () => { assert.deepEqual(Selectors.getOtherChannels(testState, false), [channel1]); }); }); describe('getChannel', () => { const team1 = TestHelper.fakeTeamWithId(); const team2 = TestHelper.fakeTeamWithId(); const user = TestHelper.fakeUserWithId(); const user2 = TestHelper.fakeUserWithId(); const user3 = TestHelper.fakeUserWithId(); const profiles = { [user.id]: user, [user2.id]: user2, [user3.id]: user3, }; const channel1 = { ...TestHelper.fakeChannelWithId(team1.id), type: General.OPEN_CHANNEL, }; const channel2 = { ...TestHelper.fakeChannelWithId(team2.id), type: General.DM_CHANNEL, name: getDirectChannelName(user.id, user2.id), }; const channel3 = { ...TestHelper.fakeChannelWithId(team2.id), type: General.GM_CHANNEL, display_name: [user.username, user2.username, user3.username].join(', '), }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, }; const testState = deepFreezeAndThrowOnMutation({ entities: { users: { currentUserId: user.id, profiles, statuses: {}, profilesInChannel: { [channel2.id]: new Set([user.id, user2.id]), [channel3.id]: new Set([user.id, user2.id, user3.id]), }, }, channels: { channels, }, preferences: { myPreferences: {}, }, general: { config: {}, }, }, }); test('should return channels directly from the store', () => { expect(Selectors.getChannel(testState, channel1.id)).toBe(channel1); expect(Selectors.getChannel(testState, channel2.id)).toBe(channel2); expect(Selectors.getChannel(testState, channel3.id)).toBe(channel3); }); }); describe('makeGetChannel', () => { const team1 = TestHelper.fakeTeamWithId(); const team2 = TestHelper.fakeTeamWithId(); const user = TestHelper.fakeUserWithId(); const user2 = TestHelper.fakeUserWithId(); const user3 = TestHelper.fakeUserWithId(); const profiles = { [user.id]: user, [user2.id]: user2, [user3.id]: user3, }; const channel1 = { ...TestHelper.fakeChannelWithId(team1.id), type: General.OPEN_CHANNEL, }; const channel2 = { ...TestHelper.fakeChannelWithId(team2.id), type: General.DM_CHANNEL, name: getDirectChannelName(user.id, user2.id), }; const channel3 = { ...TestHelper.fakeChannelWithId(team2.id), type: General.GM_CHANNEL, display_name: [user.username, user2.username, user3.username].join(', '), }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, }; const testState = deepFreezeAndThrowOnMutation({ entities: { users: { currentUserId: user.id, profiles, statuses: {}, profilesInChannel: { [channel2.id]: new Set([user.id, user2.id]), [channel3.id]: new Set([user.id, user2.id, user3.id]), }, }, channels: { channels, }, preferences: { myPreferences: {}, }, general: { config: {}, }, }, }); test('should return non-DM/non-GM channels directly from the store', () => { const getChannel = Selectors.makeGetChannel(); expect(getChannel(testState, {id: channel1.id})).toBe(channel1); }); test('should return DMs with computed data added', () => { const getChannel = Selectors.makeGetChannel(); expect(getChannel(testState, {id: channel2.id})).toEqual({ ...channel2, display_name: user2.username, status: 'offline', teammate_id: user2.id, }); }); test('should return GMs with computed data added', () => { const getChannel = Selectors.makeGetChannel(); expect(getChannel(testState, {id: channel3.id})).toEqual({ ...channel3, display_name: [user2.username, user3.username].sort(sortUsernames).join(', '), }); }); }); describe('Selectors.Channels.getChannelByName', () => { const team1 = TestHelper.fakeTeamWithId(); const team2 = TestHelper.fakeTeamWithId(); const channel1 = { ...TestHelper.fakeChannelWithId(team1.id), name: 'ch1', }; const channel2 = { ...TestHelper.fakeChannelWithId(team2.id), name: 'ch2', }; const channel3 = { ...TestHelper.fakeChannelWithId(team1.id), name: 'ch3', }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, }; const testState = deepFreezeAndThrowOnMutation({ entities: { channels: { channels, }, }, }); it('get first channel that matches by name', () => { assert.deepEqual(Selectors.getChannelByName(testState, channel3.name), channel3); }); it('return null if no channel matches by name', () => { assert.deepEqual(Selectors.getChannelByName(testState, 'noChannel'), null); }); }); describe('Selectors.Channels.getChannelsNameMapInCurrentTeam', () => { const team1 = TestHelper.fakeTeamWithId(); const team2 = TestHelper.fakeTeamWithId(); const channel1 = { ...TestHelper.fakeChannelWithId(team1.id), name: 'Ch1', }; const channel2 = { ...TestHelper.fakeChannelWithId(team2.id), name: 'Ch2', }; const channel3 = { ...TestHelper.fakeChannelWithId(team2.id), name: 'Ch3', }; const channel4 = { ...TestHelper.fakeChannelWithId(team1.id), name: 'Ch4', }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, [channel4.id]: channel4, }; const channelsInTeam = { [team1.id]: [channel1.id, channel4.id], [team2.id]: [channel2.id, channel3.id], }; const testState = deepFreezeAndThrowOnMutation({ entities: { teams: { currentTeamId: team1.id, }, channels: { channels, channelsInTeam, }, }, }); it('get channel map for current team', () => { const channelMap = { [channel1.name]: channel1, [channel4.name]: channel4, }; assert.deepEqual(Selectors.getChannelsNameMapInCurrentTeam(testState), channelMap); }); }); describe('Selectors.Channels.getChannelsNameMapInTeam', () => { const team1 = TestHelper.fakeTeamWithId(); const team2 = TestHelper.fakeTeamWithId(); const channel1 = { ...TestHelper.fakeChannelWithId(team1.id), name: 'Ch1', }; const channel2 = { ...TestHelper.fakeChannelWithId(team2.id), name: 'Ch2', }; const channel3 = { ...TestHelper.fakeChannelWithId(team2.id), name: 'Ch3', }; const channel4 = { ...TestHelper.fakeChannelWithId(team1.id), name: 'Ch4', }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, [channel4.id]: channel4, }; const channelsInTeam = { [team1.id]: [channel1.id, channel4.id], [team2.id]: [channel2.id, channel3.id], }; const testState = deepFreezeAndThrowOnMutation({ entities: { channels: { channels, channelsInTeam, }, }, }); it('get channel map for team', () => { const channelMap = { [channel1.name]: channel1, [channel4.name]: channel4, }; assert.deepEqual(Selectors.getChannelsNameMapInTeam(testState, team1.id), channelMap); }); it('get empty map for non-existing team', () => { assert.deepEqual(Selectors.getChannelsNameMapInTeam(testState, 'junk'), {}); }); }); describe('Selectors.Channels.getGroupChannels', () => { const team1 = TestHelper.fakeTeamWithId(); const user = TestHelper.fakeUserWithId(); const user2 = TestHelper.fakeUserWithId(); const user3 = TestHelper.fakeUserWithId(); const profiles = { [user.id]: user, [user2.id]: user2, [user3.id]: user3, }; const channel1 = { ...TestHelper.fakeChannelWithId(team1.id), type: General.OPEN_CHANNEL, display_name: 'Channel Name', }; const channel2 = { ...TestHelper.fakeChannelWithId(team1.id), type: General.PRIVATE_CHANNEL, display_name: 'Channel Name', }; const channel3 = { ...TestHelper.fakeChannelWithId(''), type: General.GM_CHANNEL, display_name: [user.username, user3.username].join(', '), name: '', }; const channel4 = { ...TestHelper.fakeChannelWithId(''), type: General.DM_CHANNEL, display_name: 'Channel Name', name: getDirectChannelName(user.id, user2.id), }; const channel5 = { ...TestHelper.fakeChannelWithId(''), type: General.GM_CHANNEL, display_name: [user.username, user2.username, user3.username].join(', '), name: '', }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, [channel4.id]: channel4, [channel5.id]: channel5, }; const channelsInTeam = { [team1.id]: [channel1.id, channel2.id], '': [channel3.id, channel4.id, channel5.id], }; const testState = deepFreezeAndThrowOnMutation({ entities: { users: { currentUserId: user.id, profiles, statuses: {}, profilesInChannel: { [channel3.id]: new Set([user.id, user3.id]), [channel4.id]: new Set([user.id, user2.id]), [channel5.id]: new Set([user.id, user2.id, user3.id]), }, }, channels: { channels, channelsInTeam, }, preferences: { myPreferences: {}, }, general: { config: {}, }, }, }); it('get group channels', () => { assert.deepEqual(Selectors.getGroupChannels(testState), [ {...channel3, display_name: [user3.username].sort(sortUsernames).join(', ')}, {...channel5, display_name: [user2.username, user3.username].sort(sortUsernames).join(', ')}, ]); }); }); describe('Selectors.Channels.getChannelIdsInCurrentTeam', () => { const team1 = TestHelper.fakeTeamWithId(); const team2 = TestHelper.fakeTeamWithId(); const channel1 = TestHelper.fakeChannelWithId(team1.id); const channel2 = TestHelper.fakeChannelWithId(team1.id); const channel3 = TestHelper.fakeChannelWithId(team2.id); const channel4 = TestHelper.fakeChannelWithId(team2.id); const channel5 = TestHelper.fakeChannelWithId(''); const channelsInTeam = { [team1.id]: [channel1.id, channel2.id], [team2.id]: [channel3.id, channel4.id], // eslint-disable-next-line no-useless-computed-key ['']: [channel5.id], }; const testState = deepFreezeAndThrowOnMutation({ entities: { teams: { currentTeamId: team1.id, }, channels: { channelsInTeam, }, }, }); it('get channel ids in current team strict equal', () => { const newChannel = TestHelper.fakeChannelWithId(team2.id); const modifiedState = { ...testState, entities: { ...testState.entities, channels: { ...testState.entities.channels, channelsInTeam: { ...testState.entities.channels.channelsInTeam, [team2.id]: [ ...testState.entities.channels.channelsInTeam[team2.id], newChannel.id, ], }, }, }, }; const fromOriginalState = Selectors.getChannelIdsInCurrentTeam(testState); const fromModifiedState = Selectors.getChannelIdsInCurrentTeam(modifiedState); assert.ok(fromOriginalState === fromModifiedState); // it should't have a direct channel assert.equal(fromModifiedState.includes(channel5.id), false, 'should not have direct channel on a team'); }); }); describe('Selectors.Channels.getChannelIdsForCurrentTeam', () => { const team1 = TestHelper.fakeTeamWithId(); const team2 = TestHelper.fakeTeamWithId(); const channel1 = TestHelper.fakeChannelWithId(team1.id); const channel2 = TestHelper.fakeChannelWithId(team1.id); const channel3 = TestHelper.fakeChannelWithId(team2.id); const channel4 = TestHelper.fakeChannelWithId(team2.id); const channel5 = TestHelper.fakeChannelWithId(''); const channelsInTeam = { [team1.id]: [channel1.id, channel2.id], [team2.id]: [channel3.id, channel4.id], // eslint-disable-next-line no-useless-computed-key ['']: [channel5.id], }; const testState = deepFreezeAndThrowOnMutation({ entities: { teams: { currentTeamId: team1.id, }, channels: { channelsInTeam, }, }, }); it('get channel ids for current team strict equal', () => { const anotherChannel = TestHelper.fakeChannelWithId(team2.id); const modifiedState = { ...testState, entities: { ...testState.entities, channels: { ...testState.entities.channels, channelsInTeam: { ...testState.entities.channels.channelsInTeam, [team2.id]: [ ...testState.entities.channels.channelsInTeam[team2.id], anotherChannel.id, ], }, }, }, }; const fromOriginalState = Selectors.getChannelIdsForCurrentTeam(testState); const fromModifiedState = Selectors.getChannelIdsForCurrentTeam(modifiedState); assert.ok(fromOriginalState === fromModifiedState); // it should have a direct channel assert.ok(fromModifiedState.includes(channel5.id)); }); }); describe('Selectors.Channels.isCurrentChannelFavorite', () => { const team1 = TestHelper.fakeTeamWithId(); const channel1 = TestHelper.fakeChannelWithId(team1.id); const channel2 = TestHelper.fakeChannelWithId(team1.id); const myPreferences = { [`${Preferences.CATEGORY_FAVORITE_CHANNEL}--${channel1.id}`]: { name: channel1.id, category: Preferences.CATEGORY_FAVORITE_CHANNEL, value: 'true', }, }; const testState = deepFreezeAndThrowOnMutation({ entities: { channels: { currentChannelId: channel1.id, }, general: { config: { EnableLegacySidebar: 'true', }, }, preferences: { myPreferences, }, }, }); it('isCurrentChannelFavorite', () => { assert.ok(Selectors.isCurrentChannelFavorite(testState) === true); const newState = { entities: { channels: { currentChannelId: channel2.id, }, preferences: { myPreferences, }, general: { config: { EnableLegacySidebar: 'true', }, }, }, }; assert.ok(Selectors.isCurrentChannelFavorite(newState) === false); }); }); describe('Selectors.Channels.isCurrentChannelMuted', () => { const team1 = TestHelper.fakeTeamWithId(); const channel1 = TestHelper.fakeChannelWithId(team1.id); const channel2 = TestHelper.fakeChannelWithId(team1.id); const myMembers = { [channel1.id]: {channel_id: channel1.id}, [channel2.id]: {channel_id: channel2.id, notify_props: {mark_unread: 'mention'}}, }; const testState = deepFreezeAndThrowOnMutation({ entities: { channels: { currentChannelId: channel1.id, myMembers, }, }, }); it('isCurrentChannelMuted', () => { assert.ok(Selectors.isCurrentChannelMuted(testState) === false); const newState = { entities: { channels: { ...testState.entities.channels, currentChannelId: channel2.id, }, }, }; assert.ok(Selectors.isCurrentChannelMuted(newState) === true); }); }); describe('Selectors.Channels.isCurrentChannelArchived', () => { const team1 = TestHelper.fakeTeamWithId(); const channel1 = TestHelper.fakeChannelWithId(team1.id); const channel2 = { ...TestHelper.fakeChannelWithId(team1.id), delete_at: 1, }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, }; const testState = deepFreezeAndThrowOnMutation({ entities: { channels: { currentChannelId: channel1.id, channels, }, preferences: { myPreferences: {}, }, general: { config: {}, }, }, }); it('isCurrentChannelArchived', () => { assert.ok(Selectors.isCurrentChannelArchived(testState) === false); const newState = { entities: { ...testState.entities, channels: { ...testState.entities.channels, currentChannelId: channel2.id, }, }, }; assert.ok(Selectors.isCurrentChannelArchived(newState) === true); }); }); describe('Selectors.Channels.isCurrentChannelDefault', () => { const team1 = TestHelper.fakeTeamWithId(); const channel1 = TestHelper.fakeChannelWithId(team1.id); const channel2 = { ...TestHelper.fakeChannelWithId(team1.id), name: General.DEFAULT_CHANNEL, }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, }; const testState = deepFreezeAndThrowOnMutation({ entities: { channels: { currentChannelId: channel1.id, channels, }, preferences: { myPreferences: {}, }, general: { config: {}, }, }, }); it('isCurrentChannelDefault', () => { assert.ok(Selectors.isCurrentChannelDefault(testState) === false); const newState = { entities: { ...testState.entities, channels: { ...testState.entities.channels, currentChannelId: channel2.id, }, }, }; assert.ok(Selectors.isCurrentChannelDefault(newState) === true); }); }); describe('Selectors.Channels.getChannelsWithUserProfiles', () => { const team1 = TestHelper.fakeTeamWithId(); const channel1 = TestHelper.fakeChannelWithId(team1.id); const channel2 = { ...TestHelper.fakeChannelWithId(''), type: General.GM_CHANNEL, }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, }; const channelsInTeam = { [team1.id]: [channel1.id], // eslint-disable-next-line no-useless-computed-key ['']: [channel2.id], }; const user1 = TestHelper.fakeUserWithId(); const user2 = TestHelper.fakeUserWithId(); const user3 = TestHelper.fakeUserWithId(); const profiles = { [user1.id]: user1, [user2.id]: user2, [user3.id]: user3, }; const profilesInChannel = { [channel1.id]: new Set([user1.id, user2.id]), [channel2.id]: new Set([user1.id, user2.id, user3.id]), }; const testState = deepFreezeAndThrowOnMutation({ entities: { channels: { channels, channelsInTeam, }, users: { currentUserId: user1.id, profiles, profilesInChannel, }, preferences: { myPreferences: {}, }, general: { config: {}, }, }, }); it('getChannelsWithUserProfiles', () => { const channelWithUserProfiles = Selectors.getChannelsWithUserProfiles(testState); assert.equal(channelWithUserProfiles.length, 1); assert.equal(channelWithUserProfiles[0].profiles.length, 2); }); }); describe('Selectors.Channels.getRedirectChannelNameForTeam', () => { const team1 = TestHelper.fakeTeamWithId(); const team2 = TestHelper.fakeTeamWithId(); const teams = { [team1.id]: team1, [team2.id]: team2, }; const myTeamMembers = { [team1.id]: {}, [team2.id]: {}, }; const channel1 = TestHelper.fakeChannelWithId(team1.id); const channel2 = TestHelper.fakeChannelWithId(team1.id); const channel3 = TestHelper.fakeChannelWithId(team1.id); const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, }; const user1 = TestHelper.fakeUserWithId(); const profiles = { [user1.id]: user1, }; const myChannelMembers = { [channel1.id]: {channel_id: channel1.id}, [channel2.id]: {channel_id: channel2.id}, [channel3.id]: {channel_id: channel3.id}, }; const testState = deepFreezeAndThrowOnMutation({ entities: { teams: { teams, myMembers: myTeamMembers, }, channels: { channels, myMembers: myChannelMembers, }, users: { currentUserId: user1.id, profiles, }, general: {}, }, }); it('getRedirectChannelNameForTeam without advanced permissions', () => { const modifiedState = { ...testState, entities: { ...testState.entities, general: { ...testState.entities.general, serverVersion: '4.8.0', }, }, }; assert.equal(Selectors.getRedirectChannelNameForTeam(modifiedState, team1.id), General.DEFAULT_CHANNEL); }); it('getRedirectChannelNameForTeam with advanced permissions but without JOIN_PUBLIC_CHANNELS permission', () => { const modifiedState = { ...testState, entities: { ...testState.entities, channels: { ...testState.entities.channels, channels: { ...testState.entities.channels.channels, 'new-not-member-channel': { id: 'new-not-member-channel', display_name: '111111', name: 'new-not-member-channel', team_id: team1.id, }, [channel1.id]: { id: channel1.id, display_name: 'aaaaaa', name: 'test-channel', team_id: team1.id, }, }, }, roles: { roles: { system_user: {permissions: []}, }, }, general: { ...testState.entities.general, serverVersion: '5.12.0', }, }, }; assert.equal(Selectors.getRedirectChannelNameForTeam(modifiedState, team1.id), 'test-channel'); }); it('getRedirectChannelNameForTeam with advanced permissions and with JOIN_PUBLIC_CHANNELS permission', () => { const modifiedState = { ...testState, entities: { ...testState.entities, roles: { roles: { system_user: {permissions: ['join_public_channels']}, }, }, general: { ...testState.entities.general, serverVersion: '5.12.0', }, }, }; assert.equal(Selectors.getRedirectChannelNameForTeam(modifiedState, team1.id), General.DEFAULT_CHANNEL); }); it('getRedirectChannelNameForTeam with advanced permissions but without JOIN_PUBLIC_CHANNELS permission but being member of town-square', () => { const modifiedState = { ...testState, entities: { ...testState.entities, channels: { ...testState.entities.channels, channels: { ...testState.entities.channels.channels, 'new-not-member-channel': { id: 'new-not-member-channel', display_name: '111111', name: 'new-not-member-channel', team_id: team1.id, }, [channel1.id]: { id: channel1.id, display_name: 'Town Square', name: 'town-square', team_id: team1.id, }, }, }, roles: { roles: { system_user: {permissions: []}, }, }, general: { ...testState.entities.general, serverVersion: '5.12.0', }, }, }; assert.equal(Selectors.getRedirectChannelNameForTeam(modifiedState, team1.id), General.DEFAULT_CHANNEL); }); it('getRedirectChannelNameForTeam without advanced permissions in not current team', () => { const modifiedState = { ...testState, entities: { ...testState.entities, general: { ...testState.entities.general, serverVersion: '4.8.0', }, }, }; assert.equal(Selectors.getRedirectChannelNameForTeam(modifiedState, team2.id), General.DEFAULT_CHANNEL); }); it('getRedirectChannelNameForTeam with advanced permissions but without JOIN_PUBLIC_CHANNELS permission in not current team', () => { const modifiedState = { ...testState, entities: { ...testState.entities, channels: { ...testState.entities.channels, channels: { ...testState.entities.channels.channels, 'new-not-member-channel': { id: 'new-not-member-channel', display_name: '111111', name: 'new-not-member-channel', team_id: team2.id, }, [channel3.id]: { id: channel3.id, display_name: 'aaaaaa', name: 'test-channel', team_id: team2.id, }, }, }, roles: { roles: { system_user: {permissions: []}, }, }, general: { ...testState.entities.general, serverVersion: '5.12.0', }, }, }; assert.equal(Selectors.getRedirectChannelNameForTeam(modifiedState, team2.id), 'test-channel'); }); it('getRedirectChannelNameForTeam with advanced permissions and with JOIN_PUBLIC_CHANNELS permission in not current team', () => { const modifiedState = { ...testState, entities: { ...testState.entities, roles: { roles: { system_user: {permissions: ['join_public_channels']}, }, }, general: { ...testState.entities.general, serverVersion: '5.12.0', }, }, }; assert.equal(Selectors.getRedirectChannelNameForTeam(modifiedState, team2.id), General.DEFAULT_CHANNEL); }); it('getRedirectChannelNameForTeam with advanced permissions but without JOIN_PUBLIC_CHANNELS permission but being member of town-square in not current team', () => { const modifiedState = { ...testState, entities: { ...testState.entities, channels: { ...testState.entities.channels, channels: { ...testState.entities.channels.channels, 'new-not-member-channel': { id: 'new-not-member-channel', display_name: '111111', name: 'new-not-member-channel', team_id: team2.id, }, [channel3.id]: { id: channel3.id, display_name: 'Town Square', name: 'town-square', team_id: team2.id, }, }, }, roles: { roles: { system_user: {permissions: []}, }, }, general: { ...testState.entities.general, serverVersion: '5.12.0', }, }, }; assert.equal(Selectors.getRedirectChannelNameForTeam(modifiedState, team2.id), General.DEFAULT_CHANNEL); }); }); describe('Selectors.Channels.getDirectAndGroupChannels', () => { const user1 = TestHelper.fakeUserWithId(); const user2 = TestHelper.fakeUserWithId(); const user3 = TestHelper.fakeUserWithId(); const channel1 = { ...TestHelper.fakeChannelWithId(''), display_name: [user1.username, user2.username, user3.username].join(', '), type: General.GM_CHANNEL, }; const channel2 = { ...TestHelper.fakeChannelWithId(''), name: getDirectChannelName(user1.id, user2.id), type: General.DM_CHANNEL, }; const channel3 = { ...TestHelper.fakeChannelWithId(''), name: getDirectChannelName(user1.id, user3.id), type: General.DM_CHANNEL, }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, }; const profiles = { [user1.id]: user1, [user2.id]: user2, [user3.id]: user3, }; const profilesInChannel = { [channel1.id]: new Set([user1.id, user2.id, user3.id]), [channel2.id]: new Set([user1.id, user2.id]), [channel3.id]: new Set([user1.id, user3.id]), }; const testState = deepFreezeAndThrowOnMutation({ entities: { users: { currentUserId: user1.id, profiles, profilesInChannel, }, channels: { channels, }, preferences: { myPreferences: {}, }, general: { config: {}, }, }, }); it('will return no channels if there is no active user', () => { const state = { ...testState, entities: { ...testState.entities, users: { ...testState.entities.users, currentUserId: null, }, }, }; assert.deepEqual(Selectors.getDirectAndGroupChannels(state), []); }); it('will return only direct and group message channels', () => { const state = { ...testState, entities: { ...testState.entities, users: { ...testState.entities.users, }, }, }; assert.deepEqual(Selectors.getDirectAndGroupChannels(state), [ {...channel1, display_name: [user2.username, user3.username].sort(sortUsernames).join(', ')}, {...channel2, display_name: user2.username}, {...channel3, display_name: user3.username}, ]); }); it('will not error out on undefined channels', () => { const state = { ...testState, entities: { ...testState.entities, users: { ...testState.entities.users, }, channels: { ...testState.entities.channels, channels: { ...testState.entities.channels.channels, ['undefined']: undefined, //eslint-disable-line no-useless-computed-key }, }, }, }; assert.deepEqual(Selectors.getDirectAndGroupChannels(state), [ {...channel1, display_name: [user2.username, user3.username].sort(sortUsernames).join(', ')}, {...channel2, display_name: user2.username}, {...channel3, display_name: user3.username}, ]); }); }); describe('Selectors.Channels.getOrderedChannelIds', () => { const team1 = TestHelper.fakeTeamWithId(); const channel1 = { ...TestHelper.fakeChannelWithId(team1.id), type: General.OPEN_CHANNEL, last_post_at: 0, }; const channel2 = { ...TestHelper.fakeChannelWithId(team1.id), type: General.OPEN_CHANNEL, last_post_at: 0, }; const channel3 = { ...TestHelper.fakeChannelWithId(team1.id), type: General.OPEN_CHANNEL, last_post_at: 0, }; const channel4 = { ...TestHelper.fakeChannelWithId(team1.id), type: General.OPEN_CHANNEL, last_post_at: 0, }; const channel5 = { ...TestHelper.fakeChannelWithId(team1.id), type: General.OPEN_CHANNEL, last_post_at: 0, }; const channels = { [channel1.id]: channel1, [channel2.id]: channel2, [channel3.id]: channel3, [channel4.id]: channel4, [channel5.id]: channel5, }; const channelsInTeam = { [team1.id]: [channel1.id, channel2.id, channel3.id, channel4.id, channel5.id], }; const user1 = TestHelper.fakeUserWithId(); const profiles = { [user1.id]: user1, }; const myChannelMembers = { [channel1.id]: {}, [channel2.id]: {}, [channel3.id]: {}, [channel4.id]: {}, [channel5.id]: {}, }; const testState = deepFreezeAndThrowOnMutation({ entities: { teams: { currentTeamId: team1.id, }, channels: { channels, myMembers: myChannelMembers, channelsInTeam, }, users: { currentUserId: user1.id, profiles, }, posts: { posts: {}, postsInChannel: {}, }, preferences: { myPreferences: {}, }, general: { config: {}, }, }, }); it('get ordered channel ids by_type in current team strict equal', () => { const chan5 = {...testState.entities.channels.channels[channel5.id]}; chan5.header = 'This should not change the results'; const sidebarPrefs = { grouping: 'by_type', sorting: 'alpha', unreads_at_top: 'true', favorite_at_top: 'true', }; const modifiedState = { ...testState, entities: { ...testState.entities, channels: { ...testState.entities.channels, channels: { ...testState.entities.channels.channels, [channel5.id]: chan5, }, }, }, }; const fromOriginalState = Selectors.getOrderedChannelIds( testState, null, sidebarPrefs.grouping, sidebarPrefs.sorting, sidebarPrefs.unreads_at_top === 'true', sidebarPrefs.favorite_at_top === 'true', ); const fromModifiedState = Selectors.getOrderedChannelIds( modifiedState, null, sidebarPrefs.grouping, sidebarPrefs.sorting, sidebarPrefs.unreads_at_top === 'true', sidebarPrefs.favorite_at_top === 'true', ); assert.deepEqual(fromOriginalState, fromModifiedState); chan5.total_msg_count = 10; const unreadChannelState = { ...modifiedState, entities: { ...modifiedState.entities, channels: { ...modifiedState.entities.channels, channels: { ...modifiedState.entities.channels.channels, [channel5.id]: chan5, }, myMembers: { ...modifiedState.entities.channels.myMembers, [channel5.id]: { ...modifiedState.entities.channels.myMembers[channel5.id], mention_count: 1, }, }, }, }, }; const fromUnreadState = Selectors.getOrderedChannelIds(