说明
我正在使用react-native-reanimated,并且在测试时,useSharedValue会引发一个错误,即在使用jest和react-native-testing-library时,它不是函数。我通过使用作为软件包一部分提供的模拟来模拟react-native-reanimated。我还注意到VS Code突出显示,复活没有导出成员useSharedValue,useAnimatedStyle和withTiming,但是该组件仍然可以正常运行,没有任何错误。
有人知道要正确模拟它需要做什么吗?
代码
测试以查看组件是否渲染
import React from "react";
import { render } from "react-native-testing-library";
import { Switch } from "../src/native/Switch";
describe("Switch Package", () => {
it("renders", () => {
render();
});
});
放置在名为react-native-reanimated.js的模拟文件中的模拟文件
jest.mock("react-native-reanimated", () => require("react-native-reanimated/mock") );
使用复活的组件-Switch.tsx
import { Button } from 'react-native';
import {
useSharedValue,
useAnimatedStyle,
withTiming,
Easing,
} from 'react-native-reanimated';
function Switch() {
const opacity = useSharedValue(0);
const style = useAnimatedStyle(() => {
return {
opacity: withTiming(opacity.value, {
duration: 200,
easing: sing.out(Easing.cubic),
}),
};
});
return (
<Animated.View style={[{width: 100, height: 100, backgroundColor: 'green'}, style]} />
<Button onPress={() => (opacity.value = 1)} title="Hey" />
);
}
打包版本
反应:16.11.0 React Native:.062.2 重新启用本机的React:2.0.0-alpha.3
屏幕截图