Skip to content

Commit

Permalink
migrate increasePostVisibility into redux-style action (mattermost#1958)
Browse files Browse the repository at this point in the history
  • Loading branch information
saturninoabril authored Oct 30, 2018
1 parent 44e7eda commit 3a6f733
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
9 changes: 5 additions & 4 deletions actions/post_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,12 @@ const POST_INCREASE_AMOUNT = Constants.POST_CHUNK_SIZE / 2;
// Returns true if there are more posts to load
export function increasePostVisibility(channelId, focusedPostId) {
return async (doDispatch, doGetState) => {
if (doGetState().views.channel.loadingPosts[channelId]) {
const state = doGetState();
if (state.views.channel.loadingPosts[channelId]) {
return true;
}

const currentPostVisibility = doGetState().views.channel.postVisibility[channelId];
const currentPostVisibility = state.views.channel.postVisibility[channelId];

if (currentPostVisibility >= Constants.MAX_POST_VISIBILITY) {
return true;
Expand All @@ -203,9 +204,9 @@ export function increasePostVisibility(channelId, focusedPostId) {

let result;
if (focusedPostId) {
result = await PostActions.getPostsBefore(channelId, focusedPostId, page, POST_INCREASE_AMOUNT)(dispatch, getState);
result = await doDispatch(PostActions.getPostsBefore(channelId, focusedPostId, page, POST_INCREASE_AMOUNT));
} else {
result = await PostActions.getPosts(channelId, page, POST_INCREASE_AMOUNT)(doDispatch, doGetState);
result = await doDispatch(PostActions.getPosts(channelId, page, POST_INCREASE_AMOUNT));
}
const posts = result.data;

Expand Down
53 changes: 53 additions & 0 deletions tests/redux/actions/post_actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import {Constants, ActionTypes} from 'utils/constants';

const mockStore = configureStore([thunk]);

jest.mock('mattermost-redux/actions/posts', () => ({
getPosts: (...args) => ({type: 'MOCK_GET_POSTS', args}),
getPostsBefore: (...args) => ({type: 'MOCK_GET_POSTS_BEFORE', args}),
}));

const RECEIVED_POSTS = {
channelId: 'current_channel_id',
data: {order: [], posts: {new_post_id: {channel_id: 'current_channel_id', id: 'new_post_id', message: 'new message', type: ''}}},
Expand Down Expand Up @@ -74,6 +79,10 @@ describe('Actions.Posts', () => {
posts: {
editingPost: {},
},
channel: {
loadingPosts: {},
postVisibility: {current_channel_id: 60},
},
},
};

Expand Down Expand Up @@ -128,4 +137,48 @@ describe('Actions.Posts', () => {
await testStore.dispatch(Actions.hideEditPostModal());
expect(testStore.getActions()).toEqual([{type: ActionTypes.HIDE_EDIT_POST_MODAL}]);
});

test('increasePostVisibility', async () => {
const testStore = await mockStore(initialState);

await testStore.dispatch(Actions.increasePostVisibility('current_channel_id'));
expect(testStore.getActions()).toEqual([
{
meta: {batch: true},
payload: [
{channelId: 'current_channel_id', data: true, type: 'LOADING_POSTS'},
{amount: 30, data: 'current_channel_id', type: 'INCREASE_POST_VISIBILITY'},
],
type: 'BATCHING_REDUCER.BATCH',
},
{args: ['current_channel_id', 2, 30], type: 'MOCK_GET_POSTS'},
{channelId: 'current_channel_id', data: false, type: 'LOADING_POSTS'},
]);

await testStore.dispatch(Actions.increasePostVisibility('current_channel_id', 'latest_post_id'));
expect(testStore.getActions()).toEqual([
{
meta: {batch: true},
payload: [
{channelId: 'current_channel_id', data: true, type: 'LOADING_POSTS'},
{amount: 30, data: 'current_channel_id', type: 'INCREASE_POST_VISIBILITY'},
],
type: 'BATCHING_REDUCER.BATCH',
},
{args: ['current_channel_id', 2, 30], type: 'MOCK_GET_POSTS'},
{channelId: 'current_channel_id', data: false, type: 'LOADING_POSTS'},
{
meta: {batch: true},
payload: [
{channelId: 'current_channel_id', data: true, type: 'LOADING_POSTS'},
{amount: 30, data: 'current_channel_id', type: 'INCREASE_POST_VISIBILITY'},
],
type: 'BATCHING_REDUCER.BATCH',
},
{
args: ['current_channel_id', 'latest_post_id', 2, 30],
type: 'MOCK_GET_POSTS_BEFORE',
},
{channelId: 'current_channel_id', data: false, type: 'LOADING_POSTS'}]);
});
});

0 comments on commit 3a6f733

Please sign in to comment.