Skip to content

Commit 78d2b3c

Browse files
sasurau4facebook-github-bot
authored andcommitted
refine invariant error message at scrollToIndex (#28464)
Summary: I refined the error message of scrollToIndex. When I used scrollToIndex with `index:0` and data that length is 0, I met the odd error message `Invariant Violation scrollToIndex out of range: requested index 0 but maximum is -1`. Next, I thought that scrollToIndex with `index:-1` meant scrollToTop without checking data length. I met `Invariant Violation: scrollToIndex out of range: requested index -1 but maximum is -1` Finally, I wondered what will happen to use scrollToIndex with `index:-1` and data that length is `5`. The result is `Invariant Violation: scrollToIndex out of range: requested index -1 but maximum is 5` The above error messages will confuse us. I clarified the boudaries and separated the error messages ## Changelog [General] [Fixed] - Clarified the boundaries in error message of scrollToIndex Pull Request resolved: #28464 Test Plan: I added 3 test cases to cover the new error messages for VirtualizedList. Run `yarn test` and confirm it passes. Reviewed By: cpojer Differential Revision: D21140133 Pulled By: TheSavior fbshipit-source-id: 9a7a704f7ec599d833d2ed3ca2be059d950539b5
1 parent 23d6b8d commit 78d2b3c

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

Libraries/Lists/VirtualizedList.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,18 @@ class VirtualizedList extends React.PureComponent<Props, State> {
418418
} = this.props;
419419
const {animated, index, viewOffset, viewPosition} = params;
420420
invariant(
421-
index >= 0 && index < getItemCount(data),
422-
`scrollToIndex out of range: requested index ${index} but maximum is ${getItemCount(
421+
index >= 0,
422+
`scrollToIndex out of range: requested index ${index} but minimum is 0`,
423+
);
424+
invariant(
425+
getItemCount(data) >= 1,
426+
`scrollToIndex out of range: item length ${getItemCount(
427+
data,
428+
)} but minimum is 1`,
429+
);
430+
invariant(
431+
index < getItemCount(data),
432+
`scrollToIndex out of range: requested index ${index} is out of 0 to ${getItemCount(
423433
data,
424434
) - 1}`,
425435
);

Libraries/Lists/__tests__/VirtualizedList-test.js

+48
Original file line numberDiff line numberDiff line change
@@ -456,4 +456,52 @@ describe('VirtualizedList', () => {
456456
console.error.mockRestore();
457457
}
458458
});
459+
460+
it('throws if using scrollToIndex with index less than 0', () => {
461+
const component = ReactTestRenderer.create(
462+
<VirtualizedList
463+
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
464+
renderItem={({item}) => <item value={item.key} />}
465+
getItem={(data, index) => data[index]}
466+
getItemCount={data => data.length}
467+
/>,
468+
);
469+
const instance = component.getInstance();
470+
471+
expect(() => instance.scrollToIndex({index: -1})).toThrow(
472+
'scrollToIndex out of range: requested index -1 but minimum is 0',
473+
);
474+
});
475+
476+
it('throws if using scrollToIndex when item length is less than 1', () => {
477+
const component = ReactTestRenderer.create(
478+
<VirtualizedList
479+
data={[]}
480+
renderItem={({item}) => <item value={item.key} />}
481+
getItem={(data, index) => data[index]}
482+
getItemCount={data => data.length}
483+
/>,
484+
);
485+
const instance = component.getInstance();
486+
487+
expect(() => instance.scrollToIndex({index: 1})).toThrow(
488+
'scrollToIndex out of range: item length 0 but minimum is 1',
489+
);
490+
});
491+
492+
it('throws if using scrollToIndex when requested index is bigger than or equal to item length', () => {
493+
const component = ReactTestRenderer.create(
494+
<VirtualizedList
495+
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
496+
renderItem={({item}) => <item value={item.key} />}
497+
getItem={(data, index) => data[index]}
498+
getItemCount={data => data.length}
499+
/>,
500+
);
501+
const instance = component.getInstance();
502+
503+
expect(() => instance.scrollToIndex({index: 3})).toThrow(
504+
'scrollToIndex out of range: requested index 3 is out of 0 to 2',
505+
);
506+
});
459507
});

0 commit comments

Comments
 (0)