Skip to content

Commit

Permalink
add tests for ScrollView.onScroll (facebook#48891)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#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
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Jan 23, 2025
1 parent f695411 commit 0353648
Showing 1 changed file with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit 0353648

Please sign in to comment.