diff --git a/utils/post_utils.jsx b/utils/post_utils.jsx index a9d152652027..50a29dc4b942 100644 --- a/utils/post_utils.jsx +++ b/utils/post_utils.jsx @@ -225,18 +225,23 @@ export function isErrorInvalidSlashCommand(error) { return false; } +function isIdNotPost(postId) { + return ( + PostListUtils.isStartOfNewMessages(postId) || + PostListUtils.isDateLine(postId) || + postId === PostListRowListIds.CHANNEL_INTRO_MESSAGE || + postId === PostListRowListIds.MORE_MESSAGES_LOADER || + postId === PostListRowListIds.MANUAL_TRIGGER_LOAD_MESSAGES + ); +} + // getLastPostId returns the most recent post ID in the given list of post IDs. This function is copied from // mattermost-redux, except it also includes additional special IDs that are only used in the web app. export function getLastPostId(postIds) { for (let i = postIds.length - 1; i >= 0; i--) { const item = postIds[i]; - if ( - PostListUtils.isStartOfNewMessages(item) || - PostListUtils.isDateLine(item) || - item === PostListRowListIds.CHANNEL_INTRO_MESSAGE || - item === PostListRowListIds.MORE_MESSAGES_LOADER - ) { + if (isIdNotPost(item)) { // This is not a post at all continue; } diff --git a/utils/post_utils.test.jsx b/utils/post_utils.test.jsx index 06a76d415f9f..278ba0d0712a 100644 --- a/utils/post_utils.test.jsx +++ b/utils/post_utils.test.jsx @@ -4,6 +4,7 @@ import assert from 'assert'; import * as PostUtils from 'utils/post_utils.jsx'; +import {PostListRowListIds} from 'utils/constants.jsx'; describe('PostUtils.containsAtChannel', () => { test('should return correct @all (same for @channel)', () => { @@ -524,3 +525,30 @@ describe('PostUtils.postMessageOnKeyPress', () => { }); } }); + +describe('PostUtils.getLastPostId', () => { + test('Should not return MANUAL_TRIGGER_LOAD_MESSAGES', () => { + const postId = PostUtils.getLastPostId(['postId1', 'postId2', PostListRowListIds.MANUAL_TRIGGER_LOAD_MESSAGES]); + assert.equal(postId, 'postId2'); + }); + + test('Should not return MORE_MESSAGES_LOADER', () => { + const postId = PostUtils.getLastPostId(['postId1', 'postId2', PostListRowListIds.MORE_MESSAGES_LOADER]); + assert.equal(postId, 'postId2'); + }); + + test('Should not return CHANNEL_INTRO_MESSAGE', () => { + const postId = PostUtils.getLastPostId(['postId1', 'postId2', PostListRowListIds.CHANNEL_INTRO_MESSAGE]); + assert.equal(postId, 'postId2'); + }); + + test('Should not return dateline', () => { + const postId = PostUtils.getLastPostId(['postId1', 'postId2', 'date-1558290600000']); + assert.equal(postId, 'postId2'); + }); + + test('Should not return START_OF_NEW_MESSAGES', () => { + const postId = PostUtils.getLastPostId(['postId1', 'postId2', PostListRowListIds.START_OF_NEW_MESSAGES]); + assert.equal(postId, 'postId2'); + }); +});