Commit 3cfc42ce authored by Elias Nahum's avatar Elias Nahum Committed by Harrison Healey
Browse files

Handle pending posts when dispatching RECEIVED_NEW_POST (#780)

* Handle pending posts when dispatching RECEIVED_NEW_POST

* Add unit tests
parent e1551565
......@@ -43,6 +43,23 @@ function handleReceivedPost(posts = {}, postsInChannel = {}, postsInThread = {},
];
}
// Remove any temporary posts
if (nextPosts[post.pending_post_id] && post.id !== post.pending_post_id) {
Reflect.deleteProperty(nextPosts, post.pending_post_id);
const channelIndex = nextPostsInChannel[channelId].indexOf(post.pending_post_id);
if (channelIndex !== -1) {
nextPostsInChannel[channelId].splice(channelIndex, 1);
}
if (post.root_id && postsInThread[post.root_id]) {
const threadIndex = nextPostsInThread[post.root_id].indexOf(post.pending_post_id);
if (threadIndex !== -1) {
nextPostsInThread[post.root_id].splice(threadIndex, 1);
}
}
}
const withCombineSystemPosts = combineSystemPosts(nextPostsInChannel[channelId], nextPosts);
nextPostsInChannel[channelId] = withCombineSystemPosts.postsForChannel;
return {posts: withCombineSystemPosts.nextPosts, postsInChannel: nextPostsInChannel, postsInThread: nextPostsInThread};
......
......@@ -125,6 +125,89 @@ describe('Reducers.posts', () => {
});
});
describe('RECEIVED_NEW_POST for a previous pending post', () => {
it('should remove unneeded metadata', () => {
const pendingPostId = 'pending_post_id';
const state = deepFreeze({
posts: {},
postsInChannel: {},
});
const action = {
type: PostTypes.RECEIVED_NEW_POST,
data: {
id: pendingPostId,
pending_post_id: pendingPostId,
metadata: {
emojis: [{name: 'emoji'}],
files: [{id: 'file', post_id: 'post'}],
},
},
};
let nextState = postsReducer(state, action);
assert.deepEqual(nextState.posts, {
pending_post_id: {
id: pendingPostId,
pending_post_id: pendingPostId,
metadata: {},
},
});
const action2 = {
type: PostTypes.RECEIVED_NEW_POST,
data: {
id: 'post',
pending_post_id: pendingPostId,
metadata: {
emojis: [{name: 'emoji'}],
files: [{id: 'file', post_id: 'post'}],
},
},
};
nextState = postsReducer(state, action2);
assert.deepEqual(nextState.posts, {
post: {
id: 'post',
pending_post_id: pendingPostId,
metadata: {},
},
});
});
it('should add postId to postsInChannel when postsInChannel[channelId] is set', () => {
const state = deepFreeze({
posts: {},
postsInChannel: {
channelId: [],
},
});
const post = {
id: 'postId',
channel_id: 'channelId',
};
const action = {
type: PostTypes.RECEIVED_NEW_POST,
data: post,
};
const nextState = postsReducer(state, action);
assert.deepEqual(nextState.posts, {
postId: {
id: 'postId',
channel_id: 'channelId',
},
});
assert.deepEqual(nextState.postsInChannel, {
channelId: ['postId'],
});
});
});
describe('RECEIVED_POSTS', () => {
it('should remove unneeded metadata', () => {
const state = deepFreeze({
......
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