Files
react-native/Libraries/Lists/__tests__/FlatList-test.js
T
Nick Gerleman c03de97fb4 Make FlatList permissive of ArrayLike data (#36236)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/36236

D38198351 (https://github.com/facebook/react-native/commit/d574ea3526e713eae2c6e20c7a68fa66ff4ad7d2) addedd a guard to FlatList, to no-op if passed `data` that was not an array. This broke functionality where Realm had documented using `Realm.Results` with FlatList. `Real.Results` is an array-like JSI object, but not actually an array, and fails any `Array.isArray()` checks.

This change loosens the FlatList contract, to explicitly allow array-like non-array entities. The requirement align to Flow `ArrayLike`, which allows both arrays, and objects which provide a length and indexer. Flow `$ArrayLike` currently also requires an iterator, but this is seemingly a mistake in the type definition, and not enforced.

Though `Realm.Results` has all the methods of TS `ReadonlyArray`, RN has generally assumes its array inputs will pass `Array.isArray()`. This includes any array props still being checked [via prop-types](https://github.com/facebook/prop-types/blob/044efd7a108556c7660f6b62092756666e39d74b/factoryWithTypeCheckers.js#L548).

This change intentionally does not yet change the parameter type of `getItemLayout()`, which is already too loose (allowing mutable arrays). Changing this is a breaking change, that would be disruptive to backport, so we separate it into a different commit that will be landed as part of 0.72 (see next diff in the stack).

Changelog:
[General][Changed] - Make FlatList permissive of ArrayLike data

Reviewed By: yungsters

Differential Revision: D43465654

fbshipit-source-id: 3ed8c76c15da680560d7639b7cc43272f3e46ac3
2023-02-24 16:19:14 -08:00

211 lines
6.1 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/
'use strict';
const FlatList = require('../FlatList');
const React = require('react');
const ReactTestRenderer = require('react-test-renderer');
describe('FlatList', () => {
it('renders simple list', () => {
const component = ReactTestRenderer.create(
<FlatList
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
renderItem={({item}) => <item value={item.key} />}
/>,
);
expect(component).toMatchSnapshot();
});
it('renders simple list (multiple columns)', () => {
const component = ReactTestRenderer.create(
<FlatList
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
renderItem={({item}) => <item value={item.key} />}
numColumns={2}
/>,
);
expect(component).toMatchSnapshot();
});
it('renders simple list using ListItemComponent', () => {
function ListItemComponent({item}) {
return <item value={item.key} />;
}
const component = ReactTestRenderer.create(
<FlatList
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
ListItemComponent={ListItemComponent}
/>,
);
expect(component).toMatchSnapshot();
});
it('renders simple list using ListItemComponent (multiple columns)', () => {
function ListItemComponent({item}) {
return <item value={item.key} />;
}
const component = ReactTestRenderer.create(
<FlatList
data={[{key: 'i1'}, {key: 'i2'}, {key: 'i3'}]}
ListItemComponent={ListItemComponent}
numColumns={2}
/>,
);
expect(component).toMatchSnapshot();
});
it('renders empty list', () => {
const component = ReactTestRenderer.create(
<FlatList data={[]} renderItem={({item}) => <item value={item.key} />} />,
);
expect(component).toMatchSnapshot();
});
it('renders null list', () => {
const component = ReactTestRenderer.create(
<FlatList
data={undefined}
renderItem={({item}) => <item value={item.key} />}
/>,
);
expect(component).toMatchSnapshot();
});
it('renders all the bells and whistles', () => {
const component = ReactTestRenderer.create(
<FlatList
ItemSeparatorComponent={() => <separator />}
ListEmptyComponent={() => <empty />}
ListFooterComponent={() => <footer />}
ListHeaderComponent={() => <header />}
data={new Array(5).fill().map((_, ii) => ({id: String(ii)}))}
keyExtractor={(item, index) => item.id}
getItemLayout={({index}) => ({length: 50, offset: index * 50})}
numColumns={2}
refreshing={false}
onRefresh={jest.fn()}
renderItem={({item}) => <item value={item.id} />}
/>,
);
expect(component).toMatchSnapshot();
});
it('getNativeScrollRef for case where it returns a native view', () => {
jest.resetModules();
jest.unmock('../../Components/ScrollView/ScrollView');
const listRef = React.createRef(null);
ReactTestRenderer.create(
<FlatList
data={[{key: 'outer0'}, {key: 'outer1'}]}
renderItem={outerInfo => (
<FlatList
data={[
{key: outerInfo.item.key + ':inner0'},
{key: outerInfo.item.key + ':inner1'},
]}
renderItem={innerInfo => {
return <item title={innerInfo.item.key} />;
}}
ref={listRef}
/>
)}
/>,
);
const scrollRef = listRef.current.getNativeScrollRef();
// This is checking if the ref acts like a host component. If we had an
// `isHostComponent(ref)` method, that would be preferred.
expect(scrollRef.measure).toBeInstanceOf(jest.fn().constructor);
expect(scrollRef.measureLayout).toBeInstanceOf(jest.fn().constructor);
expect(scrollRef.measureInWindow).toBeInstanceOf(jest.fn().constructor);
});
it('getNativeScrollRef for case where it returns a native scroll view', () => {
jest.resetModules();
jest.unmock('../../Components/ScrollView/ScrollView');
function ListItemComponent({item}) {
return <item value={item.key} />;
}
const listRef = React.createRef(null);
ReactTestRenderer.create(
<FlatList
data={[{key: 'i4'}, {key: 'i2'}, {key: 'i3'}]}
ListItemComponent={ListItemComponent}
numColumns={2}
ref={listRef}
/>,
);
const scrollRef = listRef.current.getNativeScrollRef();
// This is checking if the ref acts like a host component. If we had an
// `isHostComponent(ref)` method, that would be preferred.
expect(scrollRef.measure).toBeInstanceOf(jest.fn().constructor);
expect(scrollRef.measureLayout).toBeInstanceOf(jest.fn().constructor);
expect(scrollRef.measureInWindow).toBeInstanceOf(jest.fn().constructor);
});
it('calls renderItem for all data items', () => {
const data = [
{key: 'i1'},
null,
undefined,
{key: 'i2'},
null,
undefined,
{key: 'i3'},
];
const renderItemInOneColumn = jest.fn();
ReactTestRenderer.create(
<FlatList data={data} renderItem={renderItemInOneColumn} />,
);
expect(renderItemInOneColumn).toHaveBeenCalledTimes(7);
const renderItemInThreeColumns = jest.fn();
ReactTestRenderer.create(
<FlatList
data={data}
renderItem={renderItemInThreeColumns}
numColumns={3}
/>,
);
expect(renderItemInThreeColumns).toHaveBeenCalledTimes(7);
});
it('renders array-like data', () => {
const arrayLike = {
length: 3,
0: {key: 'i1'},
1: {key: 'i2'},
2: {key: 'i3'},
};
const component = ReactTestRenderer.create(
<FlatList
data={arrayLike}
renderItem={({item}) => <item value={item.key} />}
/>,
);
expect(component).toMatchSnapshot();
});
it('ignores invalid data', () => {
const component = ReactTestRenderer.create(
<FlatList
data={123456}
renderItem={({item}) => <item value={item.key} />}
/>,
);
expect(component).toMatchSnapshot();
});
});