From 0f67b470261fb7b1a1650fbd80e75ba218d8503f Mon Sep 17 00:00:00 2001 From: Sudheer Date: Fri, 20 Sep 2019 00:09:52 +0530 Subject: [PATCH] Revert "Call scrollToIndex only when ref is set and index is in range (#3282)" (#3284) This reverts commit ecbbb3f0e5c1a48fe793009a243c1e4f5510fafe. --- app/components/post_list/post_list.js | 31 ++++++++++------------ app/components/post_list/post_list.test.js | 26 ------------------ 2 files changed, 14 insertions(+), 43 deletions(-) diff --git a/app/components/post_list/post_list.js b/app/components/post_list/post_list.js index 588e8ae8c..d55b77126 100644 --- a/app/components/post_list/post_list.js +++ b/app/components/post_list/post_list.js @@ -299,33 +299,30 @@ export default class PostList extends PureComponent { scrollToBottom = () => { setTimeout(() => { - if (this.flatListRef?.current) { + if (this.flatListRef && this.flatListRef.current) { this.flatListRef.current.scrollToOffset({offset: 0, animated: true}); } }, 250); }; - flatListScrollToIndex = (index) => { - this.flatListRef.current.scrollToIndex({ - animated: false, - index, - viewOffset: 0, - viewPosition: 1, // 0 is at bottom - }); - } - scrollToIndex = (index) => { - this.animationFrameInitialIndex = requestAnimationFrame(() => { - if (this.flatListRef?.current && index > 0 && index <= this.getItemCount()) { - this.flatListScrollToIndex(index); - } - }); + if (this.flatListRef?.current) { + this.animationFrameInitialIndex = requestAnimationFrame(() => { + this.flatListRef.current.scrollToIndex({ + animated: false, + index, + viewOffset: 0, + viewPosition: 1, // 0 is at bottom + }); + }); + } }; scrollToInitialIndexIfNeeded = (index, count = 0) => { - if (!this.hasDoneInitialScroll) { + if (!this.hasDoneInitialScroll && this.flatListRef?.current) { + this.hasDoneInitialScroll = true; + if (index > 0 && index <= this.getItemCount()) { - this.hasDoneInitialScroll = true; this.scrollToIndex(index); } else if (count < 3) { setTimeout(() => { diff --git a/app/components/post_list/post_list.test.js b/app/components/post_list/post_list.test.js index ffcf4e76b..a4219bf89 100644 --- a/app/components/post_list/post_list.test.js +++ b/app/components/post_list/post_list.test.js @@ -55,30 +55,4 @@ describe('PostList', () => { expect(baseProps.actions.handleSelectChannelByName).toHaveBeenCalled(); expect(wrapper.getElement()).toMatchSnapshot(); }); - - test('should call flatListScrollToIndex only when ref is set and index is in range', () => { - jest.spyOn(global, 'requestAnimationFrame').mockImplementation((cb) => cb()); - - const instance = wrapper.instance(); - const flatListScrollToIndex = jest.spyOn(instance, 'flatListScrollToIndex'); - const indexInRange = baseProps.postIds.length; - const indexOutOfRange = [-1, indexInRange + 1]; - - instance.flatListRef = {}; - instance.scrollToIndex(indexInRange); - expect(flatListScrollToIndex).not.toHaveBeenCalled(); - - instance.flatListRef = { - current: { - scrollToIndex: jest.fn(), - }, - }; - for (const index of indexOutOfRange) { - instance.scrollToIndex(index); - expect(flatListScrollToIndex).not.toHaveBeenCalled(); - } - - instance.scrollToIndex(indexInRange); - expect(flatListScrollToIndex).toHaveBeenCalled(); - }); });