meliorence / meliorence/react-native-snap-carousel
`onSnapToItem` not running in Jest environment (or, how to use in Jest)
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 10.5k
- Forks
- 2.3k
- PR merge metrics
- No merged PRs in 30d
Description
Is 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 😅
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the carousel's _scrollTo and _onScroll paths, using the Jest and Expo versions in the issue to reproduce the behavior. Compare the documented setup in README.md with the reported test setup; done means establishing whether onScroll can be triggered in Jest and documenting a reproducible testing path or limitation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, react-native
- Domain
- mobile, testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100