Correctly add additional actions (#4174)

Co-authored-by: Miguel Alatzar <this.migbot@gmail.com>
This commit is contained in:
Mattermost Build
2020-04-18 06:39:44 +02:00
committed by GitHub
parent 9ffa3ee8f3
commit 023eb8c426
2 changed files with 58 additions and 3 deletions

View File

@@ -467,8 +467,8 @@ export function loadUnreadChannelPosts(channels, channelMembers) {
if (posts.length) {
actions.push(receivedPosts({posts}));
const additional = await dispatch(getPostsAdditionalDataBatch(posts));
if (additional.length) {
actions.push(...additional);
if (additional.data.length) {
actions.push(...additional.data);
}
dispatch(batchActions(actions));

View File

@@ -5,7 +5,7 @@ import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import {Client4} from '@mm-redux/client';
import {PostTypes} from '@mm-redux/action_types';
import {PostTypes, UserTypes} from '@mm-redux/action_types';
import * as PostSelectors from '@mm-redux/selectors/entities/posts';
import * as ChannelUtils from '@mm-redux/utils/channel_utils';
@@ -122,4 +122,59 @@ describe('Actions.Views.Post', () => {
const receivedPosts = actionTypes.filter((type) => type === PostTypes.RECEIVED_POSTS);
expect(receivedPosts.length).toBe(1);
});
test('loadUnreadChannelPosts dispatches additional actions for unread channels', async () => {
const posts = [{
user_id: 'user-id',
message: '@user post-1',
}];
ChannelUtils.isUnreadChannel = jest.fn().mockReturnValue(true);
PostSelectors.getPostIdsInChannel = jest.fn().mockReturnValue(['post-id-in-channel']);
Client4.getPostsSince = jest.fn().mockResolvedValue({posts});
Client4.getProfilesByIds = jest.fn().mockResolvedValue(['data']);
Client4.getProfilesByUsernames = jest.fn().mockResolvedValue(['data']);
Client4.getStatusesByIds = jest.fn().mockResolvedValue(['data']);
const lastGetPosts = {};
channels.forEach((channel) => {
lastGetPosts[channel.id] = Date.now();
});
const lastConnectAt = Date.now() + 1000;
store = mockStore({
...storeObj,
views: {
channel: {
lastGetPosts,
},
},
websocket: {
lastConnectAt,
},
});
await store.dispatch(loadUnreadChannelPosts(channels, channelMembers));
const actionTypes = store.getActions()[0].payload.map((action) => action.type);
// Actions dispatched:
// RECEIVED_POSTS_SINCE and RECEIVED_POSTS_FOR_CHANNEL_AT_TIME for each channel.
// RECEIVED_POSTS once, with all channel posts combined.
// RECEIVED_PROFILES_LIST twice, once for getProfilesByIds and once for getProfilesByUsernames
// RECEIVED_STATUSES for getStatusesByIds
expect(actionTypes.length).toBe((2 * channels.length) + 4);
const receivedPostsInChannelActions = actionTypes.filter((type) => type === PostTypes.RECEIVED_POSTS_SINCE);
expect(receivedPostsInChannelActions.length).toBe(channels.length);
const receivedPostsForChannelAtTimeActions = actionTypes.filter((type) => type === ViewTypes.RECEIVED_POSTS_FOR_CHANNEL_AT_TIME);
expect(receivedPostsForChannelAtTimeActions.length).toBe(channels.length);
const receivedPosts = actionTypes.filter((type) => type === PostTypes.RECEIVED_POSTS);
expect(receivedPosts.length).toBe(1);
const receivedProfiles = actionTypes.filter((type) => type === UserTypes.RECEIVED_PROFILES_LIST);
expect(receivedProfiles.length).toBe(2);
const receivedStatuses = actionTypes.filter((type) => type === UserTypes.RECEIVED_STATUSES);
expect(receivedStatuses.length).toBe(1);
});
});