Javascript 如何用动画测试React本机屏幕?
我有一个带有一些简单动画的React原生屏幕,我需要对它进行测试。 有没有办法用玩笑来测试它? 我需要模仿动画还是听众?如果是的话,我怎么做,因为没有太多关于它的信息。 快照测试失败。 如何测试以下屏幕:Javascript 如何用动画测试React本机屏幕?,javascript,reactjs,react-native,jestjs,Javascript,Reactjs,React Native,Jestjs,我有一个带有一些简单动画的React原生屏幕,我需要对它进行测试。 有没有办法用玩笑来测试它? 我需要模仿动画还是听众?如果是的话,我怎么做,因为没有太多关于它的信息。 快照测试失败。 如何测试以下屏幕: import React, {useRef, useEffect} from 'react'; import {Text, View, Animated, TextInput} from 'react-native'; import styles from './style'; export
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>
);
};
import React,{useRef,useffect}来自“React”;
从“react native”导入{Text,View,Animated,TextInput};
从“./style”导入样式;
导出常量SomeScreen:React.FC=({
航行
}) => {
const animation=useRef(新的动画.Value(0)).current;
const inputRef=useRef();
const animatedteddeput=Animated.createAnimatedComponent(TextInput);
useffect(()=>{
动画。计时(动画{
toValue:100,
持续时间:0,
useNativeDriver:错误,
}).start(()=>{
导航。导航(“下一屏幕”);
});
animation.addListener((v)=>{
如果(输入?.current){
inputRef?.current.setNativeProps({
文本:`${Math.round(v.value)}`,
});
}
});
return()=>{
removeAllListeners();
};
},[动画、导航];
常量高度=animation.interpolate({
输入范围:[01100],
输出范围:['0%,'100%,],
外推:“夹具”,
});
返回(
);
};
以下是基本的快照测试:
test('Test description', () => {
const component = (
<Provider store={store}>
<SomeScreen {...props} />
</Provider>
);
const tree = render(component).toJSON();
expect(tree).toMatchSnapshot();
});
test('testdescription',()=>{
常数分量=(
);
const tree=render(component.toJSON();
expect(tree.toMatchSnapshot();
});
如果您的动画组件具有ref属性,则react test渲染器会生成不合理的巨大快照。对于大型组件,您可能会因此而获得lengthRangeError。
尝试删除ref并检查快照是否通过。无法删除它,因为我需要它来设置文本动画。你知道文本动画的其他方法吗?有没有解决方法?谢谢