Files
react-native/packages/react-native/Libraries/Components/ScrollView/__tests__/ScrollView-itest.js
T
Rubén Norte 030ca3c543 Fix bug when dispatching unique and non-unique events of the same type on the same target (#50988)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/50988

Changelog: [internal]

This fixes a potential bug where we coalesce unique events with non-unique ones of the same type and target.

Not marked as a bug fix in the changelog because this wouldn't happen in practice, as we always dispatch events of a given type the same way (all unique or all non-unique).

Reviewed By: sammy-SC, javache

Differential Revision: D73849222

fbshipit-source-id: 6f387d63b3a68dccc81c110287d42e15e31c181e
2025-04-29 10:13:09 -07:00

124 lines
2.7 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.
*
* @flow strict-local
* @format
* @oncall react_native
*/
import 'react-native/Libraries/Core/InitializeCore';
import type {HostInstance} from 'react-native';
import ensureInstance from '../../../../src/private/__tests__/utilities/ensureInstance';
import * as Fantom from '@react-native/fantom';
import * as React from 'react';
import {ScrollView} from 'react-native';
import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement';
describe('onScroll', () => {
it('delivers onScroll event', () => {
const root = Fantom.createRoot();
const scrollViewRef = React.createRef<HostInstance>();
const onScroll = jest.fn();
Fantom.runTask(() => {
root.render(
<ScrollView
onScroll={event => {
onScroll(event.nativeEvent);
}}
ref={scrollViewRef}
/>,
);
});
const element = ensureInstance(scrollViewRef.current, ReactNativeElement);
Fantom.runOnUIThread(() => {
Fantom.enqueueNativeEvent(
element,
'scroll',
{
contentOffset: {
x: 0,
y: 1,
},
},
{
isUnique: true,
},
);
});
Fantom.runWorkLoop();
expect(onScroll).toHaveBeenCalledTimes(1);
const [entry] = onScroll.mock.lastCall;
expect(entry.contentOffset).toEqual({
x: 0,
y: 1,
});
});
it('batches onScroll event per UI tick', () => {
const root = Fantom.createRoot();
const scrollViewRef = React.createRef<HostInstance>();
const onScroll = jest.fn();
Fantom.runTask(() => {
root.render(
<ScrollView
onScroll={event => {
onScroll(event.nativeEvent);
}}
ref={scrollViewRef}
/>,
);
});
const element = ensureInstance(scrollViewRef.current, ReactNativeElement);
Fantom.runOnUIThread(() => {
Fantom.enqueueNativeEvent(
element,
'scroll',
{
contentOffset: {
x: 0,
y: 1,
},
},
{
isUnique: true,
},
);
Fantom.enqueueNativeEvent(
element,
'scroll',
{
contentOffset: {
x: 0,
y: 2,
},
},
{
isUnique: true,
},
);
});
Fantom.runWorkLoop();
expect(onScroll).toHaveBeenCalledTimes(1);
const [entry] = onScroll.mock.lastCall;
expect(entry.contentOffset).toEqual({
x: 0,
y: 2,
});
});
});