【问题标题】:react-native-reanimated useSharedValue does not update in jest testsreact-native-reanimated useSharedValue 在开玩笑测试中不更新
【发布时间】:2022-01-13 00:53:57
【问题描述】:

我目前正在尝试弄清楚如何使用 useSharedValue 测试恢复的 2 个动画。

对我来说 0 有意义的是 reanimated 中给出的例子。

https://github.com/software-mansion/react-native-reanimated/blob/master/tests/SharedValue.test.js

如果按钮应该在每次按下时将其 sharedValue 增加 1。你为什么要写一个测试表明它不会改变???

我自己试过了,是的,值不会自行更新。

我想断言我的测试中的值已经改变:

ParallaxScrollView.tsx

const scrollY = useSharedValue(0);

const onScroll = useAnimatedScrollHandler((event) => {
    scrollY.value = event.contentOffset.y;
});
return (
    <Animated.Image
        style={{height: scrollY}}
        testID="header-image"
        source={{ uri: headerImage }}
        resizeMode="cover"
    />
)

ParallaxScrollView.test.tsx

const { getByTestId } = render(<ParallaxScrollView {...defaultProps} />);
const headerImage = getByTestId('header-image');
const content = getByTestId('parallax-content');

const eventData = {
  nativeEvent: {
    contentOffset: {
      y: 100,
    },
  },
};

fireEvent.scroll(content, eventData);

expect(headerImage).toHaveAnimatedStyle({ height: 100 }); //Received is 0

【问题讨论】:

    标签: react-native jestjs react-native-reanimated react-native-testing-library


    【解决方案1】:

    useAnimatedScrollHandler 使用react-native-gesture-handler 处理事件,但目前手势处理程序还不支持测试中的事件,这就是我正在做的事情。看看这个 - https://github.com/software-mansion/react-native-gesture-handler/pull/1762
    我认为这将很快推出。如果您想了解最新信息,请在 Reanimated Github 中打开一个问题。

    【讨论】:

      猜你喜欢
      • 2020-02-15
      • 2017-08-25
      • 2021-01-09
      • 2020-12-15
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 2015-02-06
      • 1970-01-01
      相关资源
      最近更新 更多