react-native - React-Testing-Library/Jest-Dom - 接收到的值必须是 HTMLElement 或 SVGElement
问题描述
我是单元测试的新手,我正在尝试渲染一个组件以了解有关该库的更多信息。
我正在尝试遵循本指南。
零件
<TouchableOpacity
style={style}
onPress={onPress}
accessibilityRole="button"
>
<AppText style={textStyle}>{title.toUpperCase()}</AppText>
</TouchableOpacity>
测试
it("Has the correct title in the button", () => {
const { getByText } = render(<AppButton title="Hello" />);
expect(getByText("HELLO")).toBeInTheDocument();
});
我只是想查看组件是否正确呈现,但出现错误
received value must be an HTMLElement or an SVGElement.
Received has type: object
Received has value: {"_fiber": {"_debugHookTypes": null, "_debugID": 40, "_debugIsCurrentlyTiming": false, "_debugNeedsRemount": false, "_debugOwner": [FiberNode], "_debugSource": null, "actualDuration": 0, "actualStartTime": -1, "alternate": null, "child": [FiberNode], "childExpirationTime": 0, "dependencies": null, "effectTag": 1, "elementType": [Function Component], "expirationTime": 0, "firstEffect": null, "index": 0, "key": null, "lastEffect": null, "memoizedProps": [Object], "memoizedState": null, "mode": 0, "nextEffect": null, "pendingProps": [Object], "ref": null, "return": [FiberNode], "selfBaseDuration": 0, "sibling": null, "stateNode": [Component], "tag": 1, "treeBaseDuration": 0, "type": [Function Component], "updateQueue": [Object]}}
关于我做错了什么有什么建议吗?
解决方案
您可以使用waitFor
样本:
import { waitFor } from "@testing-library/react-native";
waitFor(() => expect(getByText("Your-text")).toBeInTheDocument());
// or
waitFor(() => expect(getByTestId("Your-Test-Id")).toBeInTheDocument());
推荐阅读
- mysql - 如果 Id,如何获取名称
- python - 计算列表列表中的词频
- r - R(或Rcpp)中的并行稀疏矩阵rowSums
- c++ - 为什么不调用移动构造函数?
- c# - IDE0057 子串可以简化
- spring - 无法运行 Spring 应用程序
- python - 如何在 python 3.X 中要求数独板输入?,我制作了一个数独求解器程序,但它只适用于我声明的板
- python - 寻找一种简单的方法来访问和导出到 xarray 对象中的多个数据变量的新数组
- azure - 用于检索 Azure 订阅名称、资源组、资源名称、资源类型、标签和位置的 KQL 查询
- javascript - 在添加行时动态检查 for 循环内表的高度