From 1398a4ea0f2d5afc93053b10f8f02c4cc8a81a53 Mon Sep 17 00:00:00 2001 From: Miguel Alatzar Date: Wed, 1 Apr 2020 19:10:27 -0700 Subject: [PATCH] Correctly add additional actions (#4108) --- app/actions/views/post.js | 4 +-- app/actions/views/post.test.js | 57 +++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/app/actions/views/post.js b/app/actions/views/post.js index 494f9dc2a..54bc928fe 100644 --- a/app/actions/views/post.js +++ b/app/actions/views/post.js @@ -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)); diff --git a/app/actions/views/post.test.js b/app/actions/views/post.test.js index 8bae16b0d..4c5e6222c 100644 --- a/app/actions/views/post.test.js +++ b/app/actions/views/post.test.js @@ -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); + }); }); \ No newline at end of file