react-native - 如何在 React Native 中使用 toBeVisible 匹配器
问题描述
我正在react-native-testing-library
使用"@testing-library/jest-dom/extend-expect"
.
看起来toBeVisible
只适用于 ReactJS:
const nameField = component.getByPlaceholder("Name");
expect(nameField).toBeVisible();
// Test Error:
● NewCustomerScreen › the basics › encountered a declaration exception
expect(received).toBeVisible()
received value must be an HTMLElement or an SVGElement.
Received has type: object
Received has value: {"_fiber": {"_debugHookTypes": null, "_debugID": 26, "_debugIsCurrentlyTiming": false, "_debugOwner": [FiberNode], "_debugSource": null, "alternate": null, "child": [FiberNode], "childExpirationTime": 0, "contextDependencies": null, "effectTag": 133, "elementType": [Function Component], "expirationTime": 0, "firstEffect": null, "index": 1, "key": null, "lastEffect": null, "memoizedProps": [Object], "memoizedState": null, "mode": 0, "nextEffect": [FiberNode], "pendingProps": [Object], "ref": [Function ref], "return": [FiberNode], "sibling": null, "stateNode": [Component], "tag": 1, "type": [Function Component], "updateQueue": null}}
有没有办法让它工作?
解决方案
你可以用这个
在这里你可以看到与此相关的解释。
import { render } from 'react-native-testing-library';
...
const { getByPlaceholder } = render(
<View>
<TextInput placeholder="Name" />
</View>
);
..
expect(getByPlaceholder("Name")).toBeVisible();
可见示例
<View>
<View testID="not-empty">
<Text testID="empty" />
</View>
<Text testID="visible">Visible Example</Text>
</View>;
expect(queryByTestId(baseElement, 'not-empty')).not.toBeEmpty();
expect(getByText(baseElement, 'Visible Example')).toBeVisible();
推荐阅读
- python - 如何获得相对于列表长度(即 100)的彩虹 RGV 值
- php - PHP Lighthouse Laravel - 通过认证用户获取资源
- c - 单读单写固定大小的环形缓冲区,没有锁和原子变量,对于任何 CPU 拱门总是安全的吗?
- python - 通过 __init__ 传递 configparser.ConfigParser() 对象?
- firebase - 如何从 firebase REST API 持久化 firebase 身份验证
- c++ - Qt Creator +CMake+ gmock build issue“gmock.h no such file or diretory”在Qt creator中构建项目时,如果通过命令行构建则没有问题
- sparql - SPARQL 和 blazegraph 的最短路径
- typescript - 缩小回调参数类型
- python - IPython(jupyter)中的完成现在可以工作(意外的关键字参数'column')
- python - 如何使用python从csv文件中提取最小值或最大值出现的年份