Files
react-native/Libraries/Lists/__tests__/VirtualizeUtils-test.js
T
islandryu 13a72e0ccc Fix items disappear when zooming VirtualizedList (#33765)
Summary:
fix https://github.com/facebook/react-native/issues/33705
Fixed the disappearance of items when scrolling after zooming VirtualizedList.
example https://github.com/islandryu/zoomVirtualizedList

Before modification

https://user-images.githubusercontent.com/65934663/166849127-9fc3ba84-5172-4ae1-bd44-dd6312f283ec.mov

After modification

https://user-images.githubusercontent.com/65934663/166868632-2f78e118-f705-442d-b94e-ff165bed26c7.mov

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

[General] [Fixed] - Fixed the disappearance of items when scrolling after zooming VirtualizedList.

Pull Request resolved: https://github.com/facebook/react-native/pull/33765

Test Plan:
Make the VirtualizedList zoomable with a prop such as maximumZoomScale.
Apply the patch and make sure that items in the VirtualizedList do not disappear when you scroll after zooming the VirtualizedList.

Or apply the patch from this repository and check it.
https://github.com/islandryu/zoomVirtualizedList

Reviewed By: javache

Differential Revision: D36169686

Pulled By: yungsters

fbshipit-source-id: 0f86255c2864be13f6d2dc5a58af1d11c9eedac3
2022-05-06 09:30:15 -07:00

91 lines
2.8 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
* @emails oncall+react_native
*/
'use strict';
import {elementsThatOverlapOffsets, newRangeCount} from '../VirtualizeUtils';
describe('newRangeCount', function () {
it('handles subset', function () {
expect(newRangeCount({first: 1, last: 4}, {first: 2, last: 3})).toBe(0);
});
it('handles forward disjoint set', function () {
expect(newRangeCount({first: 1, last: 4}, {first: 6, last: 9})).toBe(4);
});
it('handles reverse disjoint set', function () {
expect(newRangeCount({first: 6, last: 8}, {first: 1, last: 4})).toBe(4);
});
it('handles superset', function () {
expect(newRangeCount({first: 1, last: 4}, {first: 0, last: 5})).toBe(2);
});
it('handles end extension', function () {
expect(newRangeCount({first: 1, last: 4}, {first: 1, last: 8})).toBe(4);
});
it('handles front extension', function () {
expect(newRangeCount({first: 1, last: 4}, {first: 0, last: 4})).toBe(1);
});
it('handles forward intersect', function () {
expect(newRangeCount({first: 1, last: 4}, {first: 3, last: 6})).toBe(2);
});
it('handles reverse intersect', function () {
expect(newRangeCount({first: 3, last: 6}, {first: 1, last: 4})).toBe(2);
});
});
describe('elementsThatOverlapOffsets', function () {
it('handles fixed length', function () {
const offsets = [0, 250, 350, 450];
function getFrameMetrics(index: number) {
return {
length: 100,
offset: 100 * index,
};
}
expect(
elementsThatOverlapOffsets(offsets, 100, getFrameMetrics, 1),
).toEqual([0, 2, 3, 4]);
});
it('handles variable length', function () {
const offsets = [150, 250, 900];
const frames = [
{offset: 0, length: 50},
{offset: 50, length: 200},
{offset: 250, length: 600},
{offset: 850, length: 100},
{offset: 950, length: 150},
];
expect(
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii], 1),
).toEqual([1, 1, 3]);
});
it('handles out of bounds', function () {
const offsets = [150, 900];
const frames = [
{offset: 0, length: 50},
{offset: 50, length: 150},
{offset: 250, length: 100},
];
expect(
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii], 1),
).toEqual([1]);
});
it('errors on non-increasing offsets', function () {
const offsets = [150, 50];
const frames = [
{offset: 0, length: 50},
{offset: 50, length: 150},
{offset: 250, length: 100},
];
expect(() => {
elementsThatOverlapOffsets(offsets, frames.length, ii => frames[ii], 1);
}).toThrowErrorMatchingSnapshot();
});
});