简体   繁体   English

如何用 animation 开玩笑测试 React Native 屏幕?

[英]How to Jest test the React Native screen with an animation?

I have a React Native screen with some simple animation and I need to test it.我有一个带有一些简单 animation 的 React Native 屏幕,我需要对其进行测试。 Is there a way to test it using Jest?有没有办法使用 Jest 对其进行测试? Do I need to mock the animations or listeners?我需要模拟动画或听众吗? If yes how I can do that since there isn't much information about it.如果是的话,我该怎么做,因为没有太多关于它的信息。 Snapshot test is failing.快照测试失败。 How I can test the following screen:我如何测试以下屏幕:

import React, {useRef, useEffect} from 'react';
import {Text, View, Animated, TextInput} from 'react-native';
import styles from './style';

export const SomeScreen: React.FC<SomeScreenProps> = ({
  navigation,
}) => {
  const animation = useRef(new Animated.Value(0)).current;
  const inputRef = useRef<TextInput>();

  const AnimatedTextInput = Animated.createAnimatedComponent(TextInput);

  useEffect(() => {
    Animated.timing(animation, {
      toValue: 100,
      duration: 0,
      useNativeDriver: false,
    }).start(() => {
      navigation.navigate('next screen');
    });
    animation.addListener((v) => {
      if (inputRef?.current) {
        inputRef?.current.setNativeProps({
          text: `${Math.round(v.value)}`,
        });
      }
    });
    return () => {
      animation.removeAllListeners();
    };
  }, [animation, navigation]);

  const height = animation.interpolate({
    inputRange: [0, 100],
    outputRange: ['0%', '100%'],
    extrapolate: 'clamp',
  });

  return (
    <View>
      <View style={styles.container}>
          <Animated.View style={[styles.center, {height}]}>
              <AnimatedTextInput
                ref={inputRef}
                style={styles.number}
                editable={false}
              />
          </Animated.View>
        </View>
    </View>
  );
};

Here is the basic snapshot test:这是基本的快照测试:

test('Test description', () => {
    const component = (
      <Provider store={store}>
        <SomeScreen {...props} />
      </Provider>
    );
    const tree = render(component).toJSON();
    expect(tree).toMatchSnapshot();
});

If you have Animated component with ref property, react-test-renderer produce unreasonable huge snapshots.如果你有带有 ref 属性的 Animated 组件,react-test-renderer 会产生不合理的巨大快照。 For big components you can get lengthRangeError because of that.对于大型组件,您可以获得 lengthRangeError 因此。 Try to remove ref and check if snapshot passing.尝试删除 ref 并检查快照是否通过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM