meliorence / meliorence/react-native-snap-carousel

`onSnapToItem` not running in Jest environment (or, how to use in Jest)

Open
#784 5 comments 4 reactions 0 assignees View on GitHub

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!

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.