因此,在我的程序中,当用户登录时,如果所有凭据均正确,如果缺少任何详细信息或格式不正确,他们将进入下一页,并且屏幕上会显示警报。
如果按下按钮后已显示警报,如何在 React Native 中使用 Jest 进行测试,并确认警报文本是否正确?
我的一些组件如下所示:
...
.catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
alert(errorMessage)
});
错误文本是由 Google 的 Firebase 生成的,但我知道它是什么。
假设您正在使用react-native的Alert并自己调用它,您可以像这样监视它:
import { Alert } from 'react-native';
jest.spyOn(Alert, 'alert');
然后您可以检查它是否已被调用,以及使用了哪些参数:
expect(Alert.alert).toHaveBeenCalledWith(errorMessageText)
[更新] 还可以测试与警报的交互:
const mockOnPress = jest.fn()
Alert.alert('Title', 'Message', [{text: 'OK', onPress: mockOnPress}])
/**
* here we inspect the mock to get:
* - the latest call to Alert.alert (`calls[0]`)
* - its third argument (`calls[0][2]`), which is the buttons config array
* - the first button in the array and its onPress (`calls[0][2][0].onPress()`)
*/
Alert.alert.mock.calls[0][2][0].onPress()
expect(mockOnPress).toBeCalled()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)