From 4423e8575ebdc562040ff36d5a02e6e1b73d6171 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Thu, 5 Mar 2026 06:46:42 -0800 Subject: [PATCH] Expand Fantom integration tests for FlatList coverage (#55930) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/55930 Expand FlatList Fantom tests to improve FlatList.js code coverage from 44% to 62% line coverage. Changelog: [Internal] Reviewed By: zeyap Differential Revision: D94361042 fbshipit-source-id: 4923886eb7adfa5e5513e1e0a58c6b6f6ff72a2a --- .../Lists/__tests__/FlatList-itest.js | 815 +++++++++++++++++- 1 file changed, 803 insertions(+), 12 deletions(-) diff --git a/packages/react-native/Libraries/Lists/__tests__/FlatList-itest.js b/packages/react-native/Libraries/Lists/__tests__/FlatList-itest.js index 9a54004221f..d5816d9f670 100644 --- a/packages/react-native/Libraries/Lists/__tests__/FlatList-itest.js +++ b/packages/react-native/Libraries/Lists/__tests__/FlatList-itest.js @@ -11,9 +11,13 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; import type {FlatListProps} from 'react-native/Libraries/Lists/FlatList'; +import ensureInstance from '../../../src/private/__tests__/utilities/ensureInstance'; import * as Fantom from '@react-native/fantom'; +import nullthrows from 'nullthrows'; import * as React from 'react'; -import {FlatList, Text} from 'react-native'; +import {createRef} from 'react'; +import {FlatList, Text, View} from 'react-native'; +import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement'; function testPropPropagatedToMountingLayer({ propName, @@ -87,17 +91,13 @@ describe('', () => { ); }); - expect( - JSON.stringify(root.getRenderedOutput({props: []}).toJSX()), - ).toEqual( - JSON.stringify( - - - Title Text - Title Text 2 - - , - ), + expect(root.getRenderedOutput({props: []}).toJSX()).toEqual( + + + Title Text + Title Text 2 + + , ); }); }); @@ -187,4 +187,795 @@ describe('', () => { defaultValue: true, }); }); + + describe('ListHeaderComponent', () => { + it('renders a header before list items', () => { + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render( + {item.title}} + ListHeaderComponent={() => Header} + />, + ); + }); + + expect(root.getRenderedOutput({props: []}).toJSX()).toEqual( + + + + Header + + Item 1 + + , + ); + }); + }); + + describe('ListFooterComponent', () => { + it('renders a footer after list items', () => { + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render( + {item.title}} + ListFooterComponent={() => Footer} + />, + ); + }); + + expect(root.getRenderedOutput({props: []}).toJSX()).toEqual( + + + Item 1 + Footer + + , + ); + }); + }); + + describe('ListEmptyComponent', () => { + it('renders the empty component when data is empty', () => { + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render( + should not render} + ListEmptyComponent={() => No items} + />, + ); + }); + + expect(root.getRenderedOutput({props: []}).toJSX()).toEqual( + + + No items + + , + ); + }); + + it('does not render the empty component when data has items', () => { + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render( + {item.title}} + ListEmptyComponent={() => No items} + />, + ); + }); + + expect(root.getRenderedOutput({props: []}).toJSX()).toEqual( + + + Item 1 + + , + ); + }); + }); + + describe('numColumns', () => { + it('lays out items in a grid when numColumns > 1', () => { + const root = Fantom.createRoot({ + viewportWidth: 400, + viewportHeight: 600, + }); + Fantom.runTask(() => { + root.render( + ( + + )} + />, + ); + }); + + // Items 1 and 2 share the first row (each 200px wide), + // item 3 is alone on the second row (full 400px width). + expect( + root + .getRenderedOutput({ + includeLayoutMetrics: true, + props: ['layoutMetrics-frame'], + }) + .toJSX(), + ).toEqual( + + + + + + + , + ); + }); + }); + + describe('ItemSeparatorComponent', () => { + it('renders separators between items', () => { + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render( + {item.title}} + ItemSeparatorComponent={() => } + />, + ); + }); + + expect(root.getRenderedOutput({props: []}).toJSX()).toEqual( + + + First + + Second + + Third + + , + ); + }); + }); + + describe('keyExtractor', () => { + it('uses custom keyExtractor for item keys', () => { + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render( + {item.title}} + keyExtractor={(item: {id: string, title: string}) => item.id} + />, + ); + }); + + expect(root.getRenderedOutput({props: []}).toJSX()).toEqual( + + + Item 1 + Item 2 + + , + ); + }); + }); + + describe('getItemLayout', () => { + it('uses getItemLayout to determine item positions', () => { + const root = Fantom.createRoot({ + viewportWidth: 400, + viewportHeight: 600, + }); + Fantom.runTask(() => { + root.render( + } + getItemLayout={( + _data: ?Readonly<$ArrayLike<{key: string}>>, + index: number, + ) => ({ + length: 50, + offset: 50 * index, + index, + })} + />, + ); + }); + + expect( + root + .getRenderedOutput({ + includeLayoutMetrics: true, + props: ['layoutMetrics-frame'], + }) + .toJSX(), + ).toEqual( + + + + + + , + ); + }); + }); + + describe('initialNumToRender', () => { + it('initially renders limited items then renders more on scroll', () => { + const root = Fantom.createRoot({ + viewportWidth: 400, + viewportHeight: 200, + }); + const flatListRef = createRef>(); + Fantom.runTask(() => { + root.render( + {item.title}} + getItemLayout={( + _data: ?Readonly<$ArrayLike<{key: string, title: string}>>, + index: number, + ) => ({ + length: 50, + offset: 50 * index, + index, + })} + initialNumToRender={2} + />, + ); + }); + + expect(root.getRenderedOutput({props: []}).toJSX()).toEqual( + + + Item 1 + Item 2 + + , + ); + + // Scroll down to trigger rendering of additional items. + const scrollView = ensureInstance( + nullthrows(flatListRef.current).getNativeScrollRef(), + ReactNativeElement, + ); + Fantom.scrollTo(scrollView, {x: 0, y: 100}); + + const output = root.getRenderedOutput({props: []}).toJSONObject(); + const innerView = output.children[0]; + expect(typeof innerView).toBe('object'); + if (typeof innerView === 'object') { + expect(innerView.children.length).toBeGreaterThan(2); + } + }); + }); + + describe('onEndReached', () => { + it('calls onEndReached when scrolled near the end', () => { + const root = Fantom.createRoot({ + viewportWidth: 400, + viewportHeight: 200, + }); + const onEndReached = jest.fn(); + const flatListRef = createRef>(); + Fantom.runTask(() => { + root.render( + ({key: String(i)}))} + renderItem={() => ( + + )} + getItemLayout={( + _data: ?Readonly<$ArrayLike<{key: string}>>, + index: number, + ) => ({ + length: 100, + offset: 100 * index, + index, + })} + onEndReached={onEndReached} + onEndReachedThreshold={0.5} + />, + ); + }); + + const scrollView = ensureInstance( + nullthrows(flatListRef.current).getNativeScrollRef(), + ReactNativeElement, + ); + + Fantom.scrollTo(scrollView, {x: 0, y: 800}); + + expect(onEndReached).toHaveBeenCalled(); + }); + }); + + describe('extraData', () => { + it('triggers re-render when extraData changes', () => { + const root = Fantom.createRoot(); + let suffix = 'v1'; + const renderItem = ({ + item, + }: { + item: {key: string, title: string}, + ... + }) => {item.title + ' ' + suffix}; + + Fantom.runTask(() => { + root.render( + , + ); + }); + + expect(root.getRenderedOutput({props: []}).toJSX()).toEqual( + + + Item v1 + + , + ); + + suffix = 'v2'; + Fantom.runTask(() => { + root.render( + , + ); + }); + + expect(root.getRenderedOutput({props: []}).toJSX()).toEqual( + + + Item v2 + + , + ); + }); + }); + + describe('contentContainerStyle', () => { + it('propagates style to the content container', () => { + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render( + {item.title}} + contentContainerStyle={{backgroundColor: 'red', padding: 10}} + />, + ); + }); + + expect( + root.getRenderedOutput({props: ['backgroundColor']}).toJSX(), + ).toEqual( + + + Item 1 + + , + ); + }); + }); + + describe('imperative methods', () => { + const ITEM_HEIGHT = 50; + type Item = {key: string, title: string}; + const DATA: Array = Array.from({length: 20}, (_, i) => ({ + key: String(i), + title: `Item ${i}`, + })); + const getItemLayout = ( + _data: ?Readonly<$ArrayLike>, + index: number, + ) => ({ + length: ITEM_HEIGHT, + offset: ITEM_HEIGHT * index, + index, + }); + + describe('scrollToOffset', () => { + it('dispatches scrollTo command for vertical list', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + getItemLayout={getItemLayout} + />, + ); + }); + + root.takeMountingManagerLogs(); + + Fantom.runTask(() => { + nullthrows(flatListRef.current).scrollToOffset({ + offset: 100, + animated: false, + }); + }); + + expect(root.takeMountingManagerLogs()).toEqual([ + 'Command {type: "ScrollView", nativeID: "flat-list", name: "scrollTo, args: [0,100,false]"}', + ]); + }); + + it('dispatches scrollTo command for horizontal list', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + getItemLayout={getItemLayout} + />, + ); + }); + + root.takeMountingManagerLogs(); + + Fantom.runTask(() => { + nullthrows(flatListRef.current).scrollToOffset({ + offset: 200, + animated: false, + }); + }); + + expect(root.takeMountingManagerLogs()).toEqual([ + 'Command {type: "ScrollView", nativeID: "flat-list", name: "scrollTo, args: [200,0,false]"}', + ]); + }); + + it('passes animated flag through to the native command', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + getItemLayout={getItemLayout} + />, + ); + }); + + root.takeMountingManagerLogs(); + + Fantom.runTask(() => { + nullthrows(flatListRef.current).scrollToOffset({ + offset: 50, + animated: true, + }); + }); + + expect(root.takeMountingManagerLogs()).toEqual([ + 'Command {type: "ScrollView", nativeID: "flat-list", name: "scrollTo, args: [0,50,true]"}', + ]); + }); + }); + + describe('scrollToEnd', () => { + it('dispatches scrollTo command to scroll to end of content', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + getItemLayout={getItemLayout} + />, + ); + }); + + root.takeMountingManagerLogs(); + + Fantom.runTask(() => { + nullthrows(flatListRef.current).scrollToEnd({animated: false}); + }); + + // 20 items * 50px = 1000px total content height + expect(root.takeMountingManagerLogs()).toEqual([ + 'Command {type: "ScrollView", nativeID: "flat-list", name: "scrollTo, args: [0,1000,false]"}', + ]); + }); + }); + + describe('scrollToIndex', () => { + it('dispatches scrollTo command for a specific index', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + getItemLayout={getItemLayout} + />, + ); + }); + + root.takeMountingManagerLogs(); + + Fantom.runTask(() => { + nullthrows(flatListRef.current).scrollToIndex({ + index: 5, + animated: false, + }); + }); + + // Index 5 with ITEM_HEIGHT=50 should scroll to offset 250 + expect(root.takeMountingManagerLogs()).toEqual([ + 'Command {type: "ScrollView", nativeID: "flat-list", name: "scrollTo, args: [0,250,false]"}', + ]); + }); + }); + + describe('scrollToItem', () => { + it('dispatches scrollTo command for a specific item', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + getItemLayout={getItemLayout} + />, + ); + }); + + root.takeMountingManagerLogs(); + + Fantom.runTask(() => { + nullthrows(flatListRef.current).scrollToItem({ + item: DATA[3], + animated: false, + }); + }); + + // Item at index 3 with ITEM_HEIGHT=50 should scroll to offset 150 + expect(root.takeMountingManagerLogs()).toEqual([ + 'Command {type: "ScrollView", nativeID: "flat-list", name: "scrollTo, args: [0,150,false]"}', + ]); + }); + + it('does not dispatch command when item is not in data', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + getItemLayout={getItemLayout} + />, + ); + }); + + root.takeMountingManagerLogs(); + + Fantom.runTask(() => { + nullthrows(flatListRef.current).scrollToItem({ + item: {key: 'not-in-data', title: 'Missing'}, + animated: false, + }); + }); + + expect(root.takeMountingManagerLogs()).toEqual([]); + }); + }); + + describe('recordInteraction', () => { + it('can be called without throwing', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + />, + ); + }); + + expect(() => { + nullthrows(flatListRef.current).recordInteraction(); + }).not.toThrow(); + }); + }); + + describe('flashScrollIndicators', () => { + it('dispatches flashScrollIndicators command', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + />, + ); + }); + + root.takeMountingManagerLogs(); + + Fantom.runTask(() => { + nullthrows(flatListRef.current).flashScrollIndicators(); + }); + + expect(root.takeMountingManagerLogs()).toEqual([ + 'Command {type: "ScrollView", nativeID: "flat-list", name: "flashScrollIndicators"}', + ]); + }); + }); + + describe('getScrollResponder', () => { + it('returns a non-null scroll responder', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + />, + ); + }); + + const responder = nullthrows(flatListRef.current).getScrollResponder(); + expect(responder).not.toBeNull(); + }); + }); + + describe('getNativeScrollRef', () => { + it('returns a non-null native scroll ref', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + />, + ); + }); + + const nativeRef = nullthrows(flatListRef.current).getNativeScrollRef(); + expect(nativeRef).not.toBeNull(); + }); + }); + + describe('getScrollableNode', () => { + it('returns a non-null scrollable node', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + />, + ); + }); + + const node = nullthrows(flatListRef.current).getScrollableNode(); + expect(node).not.toBeNull(); + }); + }); + + describe('setNativeProps', () => { + it('can be called without throwing', () => { + const root = Fantom.createRoot(); + const flatListRef = createRef>(); + + Fantom.runTask(() => { + root.render( + } + />, + ); + }); + + expect(() => { + nullthrows(flatListRef.current).setNativeProps({ + scrollEnabled: false, + }); + }).not.toThrow(); + }); + }); + }); });