typescript - 在使用 React Native 测试和 TypeScript 时传递道具
问题描述
嘿,我正在尝试为也使用 TypeScript 的项目实现 react-native-testing-library。我的问题涉及将道具传递给我的测试的最佳方式。
例如,如果我的 src/example.tsx 是:
import React from 'react';
import { Button, View } from 'react-native';
export interface Props {
text: string;
func: () => void;
}
export const Example: React.FC<Props> = ({ text, func }: Props) => {
return (
<View>
<Button onPress={func} title={text} />
</View>
)
}
而我的 src/ test / example.test.tsx 是:
import React from 'react';
import { render } from '@testing-library/react-native';
import { Example, Props } from '../example';
describe('example', () => {
const { getAllBy } = render(<Example {...Props} />); <---- *Error
}
我显然得到了错误:
'Props' only refers to a type, but is being used as a value here.
做这个的最好方式是什么。我是否需要手动创建一个模拟道具对象来输入我的每个组件,还是有更好的方法来做到这一点?
解决方案
推荐阅读
- android - 使用 Android Studio 将导航抽屉中的背景颜色四舍五入
- python - Pandas isin() 函数无法正确识别数字匹配
- html - IE 网格模板列解决方法
- excel - 如何使用 VBA 重复/替换智能表中单元格中的数据以获取特定列名
- mule - 如何解决在 mule 4 中使用缓存范围时无法序列化对象导致错误
- node.js - 如何使用量角器阅读在新标签页中打开的 PDF 内容
- python - Django:如何从只有字段而不是 pk 和模型键的模型中获取 JSON 对象?
- r - 如何在因子变量的多个子集上循环线性回归
- scala - Akka HTTP 设置自定义内容类型响应标头
- mongodb - Mongo DB 比较两个数组并添加缺失的元素