Commit 46dd5d74 authored by Saturnino Abril's avatar Saturnino Abril Committed by Carlos Tadeu Panato Junior

fix post type comparison (#625)

parent d3c10c3d
......@@ -197,18 +197,22 @@ export function comparePosts(a, b) {
return 0;
}
function extractUserActivityData(userActivities) {
const postTypePriority = {
[Posts.POST_TYPES.JOIN_TEAM]: 0,
[Posts.POST_TYPES.ADD_TO_TEAM]: 1,
[Posts.POST_TYPES.LEAVE_TEAM]: 2,
[Posts.POST_TYPES.REMOVE_FROM_TEAM]: 3,
[Posts.POST_TYPES.JOIN_CHANNEL]: 4,
[Posts.POST_TYPES.ADD_TO_CHANNEL]: 5,
[Posts.POST_TYPES.LEAVE_CHANNEL]: 6,
[Posts.POST_TYPES.REMOVE_FROM_CHANNEL]: 7,
};
export const postTypePriority = {
[Posts.POST_TYPES.JOIN_TEAM]: 0,
[Posts.POST_TYPES.ADD_TO_TEAM]: 1,
[Posts.POST_TYPES.LEAVE_TEAM]: 2,
[Posts.POST_TYPES.REMOVE_FROM_TEAM]: 3,
[Posts.POST_TYPES.JOIN_CHANNEL]: 4,
[Posts.POST_TYPES.ADD_TO_CHANNEL]: 5,
[Posts.POST_TYPES.LEAVE_CHANNEL]: 6,
[Posts.POST_TYPES.REMOVE_FROM_CHANNEL]: 7,
};
export function comparePostTypes(a, b) {
return postTypePriority[a.postType] - postTypePriority[b.postType];
}
function extractUserActivityData(userActivities) {
const messageData = [];
const allUserIds = [];
const allUsernames = [];
......@@ -237,7 +241,7 @@ function extractUserActivityData(userActivities) {
}
});
messageData.sort((a, b) => postTypePriority[a.postType] > postTypePriority[b.postType]);
messageData.sort(comparePostTypes);
function reduceUsers(acc, curr) {
if (!acc.includes(curr)) {
......
......@@ -12,7 +12,9 @@ import {
combineSystemPosts,
isSystemMessage,
isUserActivityPost,
postTypePriority,
shouldFilterJoinLeavePost,
comparePostTypes,
} from 'utils/post_utils';
describe('PostUtils', () => {
......@@ -1409,4 +1411,45 @@ describe('PostUtils', () => {
assert.deepEqual(out.nextPosts[combinedPostId].props.user_activity, expectedUserActivityPosts);
});
});
describe('comparePostTypes', () => {
const {
JOIN_TEAM,
ADD_TO_TEAM,
LEAVE_TEAM,
REMOVE_FROM_TEAM,
JOIN_CHANNEL,
ADD_TO_CHANNEL,
LEAVE_CHANNEL,
REMOVE_FROM_CHANNEL,
} = PostTypes;
const testCases = [
[],
[{postType: JOIN_TEAM}],
[{postType: JOIN_TEAM}, {postType: ADD_TO_TEAM}],
[{postType: ADD_TO_TEAM}, {postType: JOIN_TEAM}],
[{postType: ADD_TO_TEAM}, {postType: ADD_TO_TEAM}, {postType: JOIN_TEAM}],
[{postType: JOIN_TEAM}, {postType: ADD_TO_TEAM}, {postType: LEAVE_TEAM}, {postType: REMOVE_FROM_TEAM}],
[{postType: REMOVE_FROM_TEAM}, {postType: LEAVE_TEAM}, {postType: ADD_TO_TEAM}, {postType: JOIN_TEAM}],
[{postType: JOIN_CHANNEL}, {postType: ADD_TO_CHANNEL}, {postType: LEAVE_CHANNEL}, {postType: REMOVE_FROM_CHANNEL}],
[{postType: REMOVE_FROM_CHANNEL}, {postType: LEAVE_CHANNEL}, {postType: ADD_TO_CHANNEL}, {postType: JOIN_CHANNEL}],
[{postType: LEAVE_CHANNEL}, {postType: REMOVE_FROM_CHANNEL}, {postType: LEAVE_TEAM}, {postType: REMOVE_FROM_TEAM}],
[{postType: LEAVE_TEAM}, {postType: REMOVE_FROM_TEAM}, {postType: LEAVE_CHANNEL}, {postType: REMOVE_FROM_CHANNEL}],
[{postType: JOIN_CHANNEL}, {postType: LEAVE_CHANNEL}, {postType: JOIN_CHANNEL}, {postType: REMOVE_FROM_CHANNEL}, {postType: ADD_TO_CHANNEL}],
];
it('should sort post type correctly', () => {
for (const testCase of testCases) {
let previousType;
testCase.sort(comparePostTypes).forEach((sortedTestCase, index) => {
if (index > 0) {
assert.ok(postTypePriority[previousType] <= postTypePriority[sortedTestCase.postType], `${previousType} should come first before ${sortedTestCase.postType}`);
}
previousType = sortedTestCase.postType;
});
}
});
});
});
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