question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 😅

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

4reactions
diegovfedercommented, Dec 3, 2020

+1 Would love to know about this too. I’m right studying jest + testing library and working on a CarouselComponent.

0reactions
tophernutscommented, Sep 20, 2022

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 the onSnapToItem in _onMomentemScrolled Won’t be called:

// WARNING: everything in this condition will probably need to be called on _snapToItem as well because:
// 1. `onMomentumScrollEnd` won't be called if the scroll isn't animated
// 2. `onMomentumScrollEnd` won't be called at all on Android when scrolling programmatically
if (nextActiveItem !== this._activeItem) {
  this._activeItem = nextActiveItem;
  onSnapToItem && onSnapToItem(this._getDataIndex(nextActiveItem));

  if (hasSnapped) {
    this._repositionScroll(nextActiveItem);
  }
}

However, on Android we run into this same problem, which we alleviate by manually calling onSnapToItem inside of the _snapToItem:

// On both platforms, `onMomentumScrollEnd` won't be triggered if the scroll isn't animated
// so we need to trigger the callback manually
// On Android `onMomentumScrollEnd` won't be triggered when scrolling programmatically
// Therefore everything critical needs to be manually called here as well, even though the timing might be off
const requiresManualTrigger = !animated || IS_ANDROID;
if (requiresManualTrigger) {
  this._activeItem = index;

  if (fireCallback) {
    onSnapToItem && onSnapToItem(this._getDataIndex(index));
  }

In my tests, mocking Platform.OS to return 'android' with a little:

jest.mock('react-native/Libraries/Utilities/Platform', () => {
  const Platform = jest.requireActual(
    'react-native/Libraries/Utilities/Platform',
  );
  Platform.OS = 'android';
  return Platform;
});

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to determine if JEST is running the code or not?
jest sets an environment variable called JEST_WORKER_ID so you check if this is set: function areWeTestingWithJest() { return process.env.
Read more >
typeerror cannot read properties of undefined (reading ... - You.com
Hi,. when I try to run react-native run-ios or react-native run-android from my project folder in VSCode I get the following error:.
Read more >
react-native-snap-carousel - npm
Swiper/carousel component for React Native with previews, multiple layouts, parallax images, performant handling of huge numbers of items, ...
Read more >
How to integrate Carousel in React Native with pagination
To use the Carousel component, we need to provide the SliderWidth and ItemWidth of the Carousel elements. We will use the dimension property...
Read more >
Jest test is not running properly with react-navigation-React ...
Coding example for the question Jest test is not running properly with react-navigation-React Native.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found