forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add tests for ScrollView.onScroll (facebook#48891)
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
1 parent
f695411
commit 0353648
Showing
1 changed file
with
122 additions
and
0 deletions.
There are no files selected for viewing
122 changes: 122 additions & 0 deletions
122
packages/react-native/Libraries/Components/ScrollView/__tests__/ScrollView-itest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |