From be2bb51c70a6885cd6607f0ea5df35f053645817 Mon Sep 17 00:00:00 2001 From: Nick Gerleman Date: Tue, 15 Aug 2023 21:56:25 -0700 Subject: [PATCH] ListMetricAggregator UTs - cell offset APIs (#38732) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/38732 Unit tests for cell offset APIs. Mostly the same as previous UTs for VirtualizedList a layer higher. Changelog: [Internal] Reviewed By: rozele Differential Revision: D47978633 fbshipit-source-id: 8cb8a2e8125bc7370eabf9f01a3f7529043171c2 --- .../__tests__/ListMetricsAggregator-test.js | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/packages/virtualized-lists/Lists/__tests__/ListMetricsAggregator-test.js b/packages/virtualized-lists/Lists/__tests__/ListMetricsAggregator-test.js index ef253fb94636e6..8b22cb18649bca 100644 --- a/packages/virtualized-lists/Lists/__tests__/ListMetricsAggregator-test.js +++ b/packages/virtualized-lists/Lists/__tests__/ListMetricsAggregator-test.js @@ -894,4 +894,148 @@ describe('ListMetricsAggregator', () => { isMounted: false, }); }); + + it('resolves integral offset of already measured cell', () => { + const listMetrics = new ListMetricsAggregator(); + const orientation = {horizontal: false, rtl: false}; + const props: CellMetricProps = { + data: [1, 2, 3, 4, 5], + getItemCount: () => nullthrows(props.data).length, + getItem: (i: number) => nullthrows(props.data)[i], + }; + + listMetrics.notifyCellLayout({ + cellIndex: 0, + cellKey: '0', + orientation, + layout: { + height: 10, + width: 5, + x: 0, + y: 0, + }, + }); + + listMetrics.notifyCellLayout({ + cellIndex: 1, + cellKey: '1', + orientation, + layout: { + height: 20, + width: 5, + x: 0, + y: 10, + }, + }); + + expect(listMetrics.getCellOffsetApprox(1, props)).toEqual(10); + }); + + it('estimates integral offset of unmeasured cell', () => { + const listMetrics = new ListMetricsAggregator(); + const orientation = {horizontal: false, rtl: false}; + const props: CellMetricProps = { + data: [1, 2, 3, 4, 5], + getItemCount: () => nullthrows(props.data).length, + getItem: (i: number) => nullthrows(props.data)[i], + }; + + listMetrics.notifyCellLayout({ + cellIndex: 0, + cellKey: '0', + orientation, + layout: { + height: 10, + width: 5, + x: 0, + y: 0, + }, + }); + + listMetrics.notifyCellLayout({ + cellIndex: 1, + cellKey: '1', + orientation, + layout: { + height: 20, + width: 5, + x: 0, + y: 10, + }, + }); + + expect(listMetrics.getCellOffsetApprox(2, props)).toEqual(30); + }); + + it('resolves fractional offset of already measured cell', () => { + const listMetrics = new ListMetricsAggregator(); + const orientation = {horizontal: false, rtl: false}; + const props: CellMetricProps = { + data: [1, 2, 3, 4, 5], + getItemCount: () => nullthrows(props.data).length, + getItem: (i: number) => nullthrows(props.data)[i], + }; + + listMetrics.notifyCellLayout({ + cellIndex: 0, + cellKey: '0', + orientation, + layout: { + height: 10, + width: 5, + x: 0, + y: 0, + }, + }); + + listMetrics.notifyCellLayout({ + cellIndex: 1, + cellKey: '1', + orientation, + layout: { + height: 20, + width: 5, + x: 0, + y: 10, + }, + }); + + expect(listMetrics.getCellOffsetApprox(1.5, props)).toEqual(20); + }); + + it('estimates fractional offset of unmeasured cell', () => { + const listMetrics = new ListMetricsAggregator(); + const orientation = {horizontal: false, rtl: false}; + const props: CellMetricProps = { + data: [1, 2, 3, 4, 5], + getItemCount: () => nullthrows(props.data).length, + getItem: (i: number) => nullthrows(props.data)[i], + }; + + listMetrics.notifyCellLayout({ + cellIndex: 0, + cellKey: '0', + orientation, + layout: { + height: 10, + width: 5, + x: 0, + y: 0, + }, + }); + + listMetrics.notifyCellLayout({ + cellIndex: 1, + cellKey: '1', + orientation, + layout: { + height: 20, + width: 5, + x: 0, + y: 10, + }, + }); + + expect(listMetrics.getCellOffsetApprox(2.5, props)).toEqual(37.5); + }); });