@@ -821,6 +821,193 @@ describe('<FlatList>', () => {
821821 } ) ;
822822 } ) ;
823823
824+ describe ( 'rows whose height depends on the list width' , ( ) => {
825+ // Rows that are not mounted when the width changes keep their metrics
826+ // from the old width. Mixed with the new metrics of the mounted rows, they
827+ // made the spacer after the rows kept by initialNumToRender start
828+ // somewhere other than where those rows end, so the rows after it moved on
829+ // every layout and the content size never stopped changing.
830+ const ASPECT_RATIOS = [ 1.5 , 2 , 3 ] ;
831+ const DATA = Array . from ( { length : 200 } , ( _ , i ) => ( { key : String ( i ) } ) ) ;
832+
833+ for ( const [ initialWidth , newWidth ] of [
834+ [ 674 , 381 ] ,
835+ [ 381 , 674 ] ,
836+ ] ) {
837+ it ( `settle after the width changes from ${ initialWidth } to ${ newWidth } while scrolled deep` , ( ) => {
838+ const root = Fantom . createRoot ( {
839+ viewportWidth : 800 ,
840+ viewportHeight : 900 ,
841+ } ) ;
842+ const flatListRef = createRef < FlatList < { key : string } >> ( ) ;
843+ let contentSizeChanges = 0 ;
844+ const render = ( width : number ) =>
845+ Fantom . runTask ( ( ) => {
846+ root . render (
847+ < View style = { { width, height : 900 } } >
848+ < FlatList
849+ ref = { flatListRef }
850+ data = { DATA }
851+ ListHeaderComponent = { < View style = { { height : 600 } } /> }
852+ renderItem = { ( { index} ) => (
853+ < View style = { { padding : 8 } } >
854+ < View
855+ style = { {
856+ aspectRatio :
857+ ASPECT_RATIOS [ index % ASPECT_RATIOS . length ] ,
858+ } }
859+ />
860+ </ View >
861+ ) }
862+ onContentSizeChange = { ( ) => {
863+ contentSizeChanges ++ ;
864+ } }
865+ />
866+ </ View > ,
867+ ) ;
868+ } ) ;
869+
870+ const timers = Fantom . installTimerMock ( ) ;
871+ try {
872+ // Delivers layout and lets batched renders run until the content
873+ // size stops changing, and returns whether it did.
874+ const settle = ( ) : boolean => {
875+ const MAX_STEPS = 50 ;
876+ const QUIET_STEPS = 5 ;
877+ let quietSteps = 0 ;
878+ for ( let step = 0 ; step < MAX_STEPS ; step ++ ) {
879+ const changesBefore = contentSizeChanges ;
880+ Fantom . flushAllNativeEvents ( ) ;
881+ timers . advanceTimersByTime ( 100 ) ;
882+ quietSteps =
883+ contentSizeChanges === changesBefore ? quietSteps + 1 : 0 ;
884+ if ( quietSteps === QUIET_STEPS ) {
885+ return true ;
886+ }
887+ }
888+ return false ;
889+ } ;
890+
891+ render ( initialWidth ) ;
892+ expect ( settle ( ) ) . toBe ( true ) ;
893+
894+ // Scroll far enough that the rows after the ones kept by
895+ // initialNumToRender unmount.
896+ const scrollView = nullthrows (
897+ nullthrows ( flatListRef . current ) . getNativeScrollRef ( ) ,
898+ ) ;
899+ for ( let y = 2000 ; y <= 30000 ; y += 2000 ) {
900+ Fantom . scrollTo ( scrollView , { x : 0 , y, zoomScale : 1 } ) ;
901+ expect ( settle ( ) ) . toBe ( true ) ;
902+ }
903+
904+ render ( newWidth ) ;
905+ expect ( settle ( ) ) . toBe ( true ) ;
906+ } finally {
907+ timers . uninstall ( ) ;
908+ }
909+ } ) ;
910+ }
911+ } ) ;
912+
913+ it ( 'keeps the size of the rows after the window when one row grows on its own' , ( ) => {
914+ // A row that changes size while the list keeps its width doesn't change
915+ // how the other rows lay out, so the list keeps the metrics of the rows
916+ // that aren't mounted, and the spacer after the window keeps its size.
917+ const ROW_HEIGHT = 50 ;
918+ const DATA = Array . from ( { length : 200 } , ( _ , i ) => ( { key : String ( i ) } ) ) ;
919+ const root = Fantom . createRoot ( { viewportWidth : 400 , viewportHeight : 500 } ) ;
920+ const flatListRef = createRef < FlatList < { key : string } >> ( ) ;
921+ let contentHeight = 0 ;
922+ const render = ( firstRowSize : number ) =>
923+ Fantom . runTask ( ( ) => {
924+ root . render (
925+ < FlatList
926+ ref = { flatListRef }
927+ data = { DATA }
928+ extraData = { firstRowSize }
929+ windowSize = { 3 }
930+ contentContainerStyle = { { alignItems : 'flex-start' } }
931+ renderItem = { ( { index} ) => (
932+ < View
933+ style = {
934+ index === 0
935+ ? { width : firstRowSize , height : firstRowSize }
936+ : { width : 300 , height : ROW_HEIGHT }
937+ }
938+ />
939+ ) }
940+ onContentSizeChange = { ( width , height ) => {
941+ contentHeight = height ;
942+ } }
943+ /> ,
944+ ) ;
945+ } ) ;
946+
947+ const timers = Fantom . installTimerMock ( ) ;
948+ try {
949+ const settle = ( ) => {
950+ for ( let step = 0 ; step < 10 ; step ++ ) {
951+ Fantom . flushAllNativeEvents ( ) ;
952+ timers . advanceTimersByTime ( 100 ) ;
953+ }
954+ } ;
955+
956+ render ( ROW_HEIGHT ) ;
957+ settle ( ) ;
958+
959+ // Scroll to the end, so that every row is measured, and back to the top.
960+ const scrollView = nullthrows (
961+ nullthrows ( flatListRef . current ) . getNativeScrollRef ( ) ,
962+ ) ;
963+ for ( let y = 500 ; y <= DATA . length * ROW_HEIGHT - 500 ; y += 500 ) {
964+ Fantom . scrollTo ( scrollView , { x : 0 , y, zoomScale : 1 } ) ;
965+ settle ( ) ;
966+ }
967+ Fantom . scrollTo ( scrollView , { x : 0 , y : 0 , zoomScale : 1 } ) ;
968+ settle ( ) ;
969+ expect ( contentHeight ) . toBe ( DATA . length * ROW_HEIGHT ) ;
970+
971+ // The rows after the window keep the offsets they were measured at
972+ // until they are measured again, so the content can stay short by the
973+ // height row 0 gained, but not lose those rows.
974+ render ( 2 * ROW_HEIGHT ) ;
975+ settle ( ) ;
976+ expect ( contentHeight ) . toBeGreaterThanOrEqual ( DATA . length * ROW_HEIGHT ) ;
977+ } finally {
978+ timers . uninstall ( ) ;
979+ }
980+ } ) ;
981+
982+ it ( 'does not throw when the data is replaced with fewer items after scrolling' , ( ) => {
983+ // Cells measured past the end of the new data must not be used to
984+ // estimate the cells that are not measured yet.
985+ const root = Fantom . createRoot ( { viewportWidth : 400 , viewportHeight : 400 } ) ;
986+ const flatListRef = createRef < FlatList < { key : string } >> ( ) ;
987+ const render = ( keyPrefix : string , length : number ) =>
988+ Fantom . runTask ( ( ) => {
989+ root . render (
990+ < FlatList
991+ ref = { flatListRef }
992+ data = { Array . from ( { length} , ( _ , i ) => ( { key : `${ keyPrefix } ${ i } ` } ) ) }
993+ renderItem = { ( ) => < View style = { { height : 44 } } /> }
994+ windowSize = { 3 }
995+ /> ,
996+ ) ;
997+ } ) ;
998+
999+ render ( 'a' , 270 ) ;
1000+ Fantom . flushAllNativeEvents ( ) ;
1001+ Fantom . scrollTo (
1002+ nullthrows ( nullthrows ( flatListRef . current ) . getNativeScrollRef ( ) ) ,
1003+ { x : 0 , y : 5000 , zoomScale : 1 } ,
1004+ ) ;
1005+ Fantom . flushAllNativeEvents ( ) ;
1006+
1007+ render ( 'b' , 20 ) ;
1008+ expect ( ( ) => Fantom . flushAllNativeEvents ( ) ) . not . toThrow ( ) ;
1009+ } ) ;
1010+
8241011 describe ( 'imperative methods' , ( ) => {
8251012 const ITEM_HEIGHT = 50 ;
8261013 type Item = { key : string , title : string } ;
0 commit comments