diff --git a/components/toast_wrapper/toast_wrapper.jsx b/components/toast_wrapper/toast_wrapper.jsx index abfc4888c7a4..fdaefcc4a7d5 100644 --- a/components/toast_wrapper/toast_wrapper.jsx +++ b/components/toast_wrapper/toast_wrapper.jsx @@ -14,6 +14,7 @@ import Constants from 'utils/constants'; import {browserHistory} from 'utils/browser_history'; const TOAST_TEXT_COLLAPSE_WIDTH = 500; +const THRESHOLD_FROM_BOTTOM = 1000; const TOAST_REL_RANGES = [ RelativeRanges.TODAY_YESTERDAY, @@ -87,25 +88,26 @@ class ToastWrapper extends React.PureComponent { } else { unreadCount = prevState.unreadCountInChannel + props.newRecentMessagesCount; } + if (props.atBottom !== null) { + // show unread toast on mount when channel is not at bottom and unread count greater than 0 + if (typeof showUnreadToast === 'undefined') { + showUnreadToast = unreadCount > 0 && props.initScrollOffsetFromBottom > THRESHOLD_FROM_BOTTOM; + } - // show unread toast on mount when channel is not at bottom and unread count greater than 0 - if (typeof showUnreadToast === 'undefined' && props.atBottom !== null) { - showUnreadToast = unreadCount > 0 && !props.atBottom; - } - - if (typeof showMessageHistoryToast === 'undefined' && props.focusedPostId !== '' && props.atBottom !== null) { - showMessageHistoryToast = props.initScrollOffsetFromBottom > 1000 || !props.atLatestPost; - } + if (typeof showMessageHistoryToast === 'undefined' && props.focusedPostId !== '') { + showMessageHistoryToast = props.initScrollOffsetFromBottom > THRESHOLD_FROM_BOTTOM || !props.atLatestPost; + } - // show unread toast when a channel is marked as unread - if (props.channelMarkedAsUnread && !props.atBottom && !prevState.channelMarkedAsUnread && !prevState.showUnreadToast) { - showUnreadToast = true; - } + // show unread toast when a channel is marked as unread + if (props.channelMarkedAsUnread && !prevState.channelMarkedAsUnread && !prevState.showUnreadToast) { + showUnreadToast = props.initScrollOffsetFromBottom > THRESHOLD_FROM_BOTTOM; + } - // show unread toast when a channel is remarked as unread using the change in lastViewedAt - // lastViewedAt changes only if a channel is remarked as unread in channelMarkedAsUnread state - if (props.channelMarkedAsUnread && props.lastViewedAt !== prevState.lastViewedAt && !props.atBottom) { - showUnreadToast = true; + // show unread toast when a channel is remarked as unread using the change in lastViewedAt + // lastViewedAt changes only if a channel is remarked as unread in channelMarkedAsUnread state + if (props.channelMarkedAsUnread && props.lastViewedAt !== prevState.lastViewedAt) { + showUnreadToast = props.initScrollOffsetFromBottom > THRESHOLD_FROM_BOTTOM; + } } if (!showUnreadToast && unreadCount > 0 && !props.atBottom && (props.lastViewedBottom < props.latestPostTimeStamp)) { diff --git a/components/toast_wrapper/toast_wrapper.test.jsx b/components/toast_wrapper/toast_wrapper.test.jsx index 40866915a4fa..ceea3d3bd04d 100644 --- a/components/toast_wrapper/toast_wrapper.test.jsx +++ b/components/toast_wrapper/toast_wrapper.test.jsx @@ -88,16 +88,28 @@ describe('components/ToastWrapper', () => { }); describe('toasts state', () => { - test('Should have unread toast if unreadCount > 0', () => { + test('Should have unread toast if unreadCount > 0 and initScrollOffsetFromBottom is greater than 1000', () => { const props = { ...baseProps, unreadCountInChannel: 10, newRecentMessagesCount: 5, + initScrollOffsetFromBottom: 1100, }; const wrapper = shallowWithIntl(); expect(wrapper.state('showUnreadToast')).toBe(true); }); + test('Should have not have unread toast if initScrollOffsetFromBottom is less than 1000', () => { + const props = { + ...baseProps, + unreadCountInChannel: 10, + newRecentMessagesCount: 5, + initScrollOffsetFromBottom: 850, + }; + + const wrapper = shallowWithIntl(); + expect(wrapper.state('showUnreadToast')).toBe(false); + }); test('Should set state of have unread toast when atBottom changes from undefined', () => { const props = { @@ -105,6 +117,7 @@ describe('components/ToastWrapper', () => { unreadCountInChannel: 10, newRecentMessagesCount: 5, atBottom: null, + initScrollOffsetFromBottom: 1100, }; const wrapper = shallowWithIntl(); @@ -132,7 +145,7 @@ describe('components/ToastWrapper', () => { const wrapper = shallowWithIntl(); expect(wrapper.state('showUnreadToast')).toBe(false); wrapper.setProps({channelMarkedAsUnread: true, atBottom: false}); - expect(wrapper.state('showUnreadToast')).toBe(true); + expect(wrapper.state('showUnreadToast')).toBe(false); }); test('Should have unread toast channel is marked as unread again', () => { @@ -157,12 +170,12 @@ describe('components/ToastWrapper', () => { ], }); - expect(wrapper.state('showUnreadToast')).toBe(true); + expect(wrapper.state('showUnreadToast')).toBe(false); wrapper.setProps({atBottom: true}); expect(wrapper.state('showUnreadToast')).toBe(false); wrapper.setProps({atBottom: false}); wrapper.setProps({lastViewedAt: 12342}); - expect(wrapper.state('showUnreadToast')).toBe(true); + expect(wrapper.state('showUnreadToast')).toBe(false); }); test('Should have archive toast if channel is not atLatestPost and focusedPostId exists', () => { @@ -241,6 +254,7 @@ describe('components/ToastWrapper', () => { 'post4', 'post5', ], + initScrollOffsetFromBottom: 1220, }; const wrapper = shallowWithIntl(); @@ -298,6 +312,7 @@ describe('components/ToastWrapper', () => { 'post4', 'post5', ], + initScrollOffsetFromBottom: 1005, }; const wrapper = shallowWithIntl(); @@ -345,6 +360,7 @@ describe('components/ToastWrapper', () => { 'post4', 'post5', ], + initScrollOffsetFromBottom: 1500, }; const wrapper = shallowWithIntl(); @@ -405,6 +421,7 @@ describe('components/ToastWrapper', () => { ...baseProps, unreadCountInChannel: 10, newRecentMessagesCount: 5, + initScrollOffsetFromBottom: 1008, }; const updateToastStatus = baseProps.actions.updateToastStatus;