mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
add tests for ScrollView.onScroll (#48891)
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/48891 changelog: [internal] add two tests covering onScroll: one for the case where onScroll is triggered multiple times during one UI tick and one where it is triggered once per UI tick. Reviewed By: rubennorte Differential Revision: D68499566 fbshipit-source-id: ee25227b620569e3a43038575f04b0a325e5e38b
This commit is contained in:
committed by
Facebook GitHub Bot
parent
f695411bf3
commit
0353648f46
+122
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* 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
|
||||
* @fantom_flags enableAccessToHostTreeInFabric:true
|
||||
*/
|
||||
|
||||
import '../../../Core/InitializeCore.js';
|
||||
import ensureInstance from '../../../../src/private/utilities/ensureInstance';
|
||||
import ReactNativeElement from '../../../../src/private/webapis/dom/nodes/ReactNativeElement';
|
||||
import ScrollView from '../ScrollView';
|
||||
import * as Fantom from '@react-native/fantom';
|
||||
import * as React from 'react';
|
||||
|
||||
describe('onScroll', () => {
|
||||
it('delivers onScroll event', () => {
|
||||
const root = Fantom.createRoot();
|
||||
let maybeNode;
|
||||
const onScroll = jest.fn();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(
|
||||
<ScrollView
|
||||
onScroll={event => {
|
||||
onScroll(event.nativeEvent);
|
||||
}}
|
||||
ref={node => {
|
||||
maybeNode = node;
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
});
|
||||
|
||||
const element = ensureInstance(maybeNode, ReactNativeElement);
|
||||
|
||||
Fantom.runOnUIThread(() => {
|
||||
Fantom.dispatchNativeEvent(
|
||||
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,
|
||||
});
|
||||
|
||||
root.destroy();
|
||||
});
|
||||
|
||||
it('batches onScroll event per UI tick', () => {
|
||||
const root = Fantom.createRoot();
|
||||
let maybeNode;
|
||||
const onScroll = jest.fn();
|
||||
|
||||
Fantom.runTask(() => {
|
||||
root.render(
|
||||
<ScrollView
|
||||
onScroll={event => {
|
||||
onScroll(event.nativeEvent);
|
||||
}}
|
||||
ref={node => {
|
||||
maybeNode = node;
|
||||
}}
|
||||
/>,
|
||||
);
|
||||
});
|
||||
|
||||
const element = ensureInstance(maybeNode, ReactNativeElement);
|
||||
|
||||
Fantom.runOnUIThread(() => {
|
||||
Fantom.dispatchNativeEvent(element, 'scroll', {
|
||||
contentOffset: {
|
||||
x: 0,
|
||||
y: 1,
|
||||
},
|
||||
});
|
||||
Fantom.dispatchNativeEvent(
|
||||
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,
|
||||
});
|
||||
|
||||
root.destroy();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user