`onSnapToItem` not running in Jest environment (or, how to use in Jest)
See original GitHub issueIs this a bug report, a feature request, or a question?
Question!
- I have read the guidelines regarding bug report.
- I have reviewed the documentation in its entirety, including the dedicated documentations 📚.
- I have searched for existing issues and made sure that the problem hasn’t already been reported.
- I am using the latest plugin version.
- I am following the issue template closely in order to produce a useful bug report.
Environment
{
"react": "16.11.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz",
"react-native-snap-carousel": "^3.9.1",
"jest": "^26.4.2",
"jest-expo": "^38.0.2",
}
Question
TL;DR I think: In Jest, _onScroll is never called which means that the “onSnapToItem” callback is never fired.
I have a snap-carousel component that I would like to test the logic happening in onSnapToItem
. So I have a button that makes the carousel go forward a step, I can click that and my logic in onSnapToItem is never called. I see the code in this library runs all the way down to triggering the scroll at “_scrollTo”. Although the scroll listener, “_onScroll” is never called on the component so none of that other part of the component . I dug into the source and added some logging like:
...
_onScroll (event) {
console.log("_onScroll")
const { callbackOffsetMargin, enableMomentum, onScroll } = this.props;
...
And this console.log is never called!
So my question is, am I able to test this kind of behaviour in Jest? I know it could be a limitation of Jest, or maybe it’s me setting up Jest incorrectly. If so then I’ll just work something else out 😅
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:5
Top GitHub Comments
+1 Would love to know about this too. I’m right studying jest + testing library and working on a CarouselComponent.
I am also running into this issue on the
4.0.0-beta.6
, I think it comes down to the testing environment being similar to android in that it won’t have any of the momentum scrolling effects, which means that theonSnapToItem
in_onMomentemScrolled
Won’t be called:However, on Android we run into this same problem, which we alleviate by manually calling
onSnapToItem
inside of the_snapToItem
:In my tests, mocking
Platform.OS
to return'android'
with a little:Causes my
onSnapToItem
to be called!I kinda hate this as this feels like it could break at any moment, but I can’t see another way around this, and I don’t think this library is actively under development.