Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refine invariant error message at scrollToIndex #28464

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,18 @@ class VirtualizedList extends React.PureComponent<Props, State> {
} = this.props;
const {animated, index, viewOffset, viewPosition} = params;
invariant(
index >= 0 && index < getItemCount(data),
`scrollToIndex out of range: requested index ${index} but maximum is ${getItemCount(
index >= 0,
`scrollToIndex out of range: requested index ${index} but minimum is 0`,
);
invariant(
getItemCount(data) >= 1,
`scrollToIndex out of range: item length ${getItemCount(
data,
)} but minimum is 1`,
);
invariant(
index < getItemCount(data),
`scrollToIndex out of range: requested index ${index} is out of 0 to ${getItemCount(
data,
) - 1}`,
);
Expand Down
48 changes: 48 additions & 0 deletions Libraries/Lists/__tests__/VirtualizedList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,52 @@ describe('VirtualizedList', () => {
console.error.mockRestore();
}
});

it('throws if using scrollToIndex with index less than 0', () => {
const component = ReactTestRenderer.create(
<VirtualizedList
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>,
);
const instance = component.getInstance();

expect(() => instance.scrollToIndex({index: -1})).toThrow(
'scrollToIndex out of range: requested index -1 but minimum is 0',
);
});

it('throws if using scrollToIndex when item length is less than 1', () => {
const component = ReactTestRenderer.create(
<VirtualizedList
data={[]}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>,
);
const instance = component.getInstance();

expect(() => instance.scrollToIndex({index: 1})).toThrow(
'scrollToIndex out of range: item length 0 but minimum is 1',
);
});

it('throws if using scrollToIndex when requested index is bigger than or equal to item length', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one I’m not sure about. With VirtualizedList you can do tail loading. So when you scroll to the bottom of the list you can fetch more data and render it. If scrollToIndex is greater than the data length I’d expect fail loading to be possible and for it to continue trying to scroll to the index. I’m not sure what the current behavior is but I’m a bit concerned about throwing in this case.

Can you either remove this condition or do a bit more investigation into that edge case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remove this condition.

const component = ReactTestRenderer.create(
<VirtualizedList
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
renderItem={({item}) => <item value={item.key} />}
getItem={(data, index) => data[index]}
getItemCount={data => data.length}
/>,
);
const instance = component.getInstance();

expect(() => instance.scrollToIndex({index: 3})).toThrow(
'scrollToIndex out of range: requested index 3 is out of 0 to 2',
);
});
});