Yos*_*ssi 2 jestjs react-native
在测试以下 react-native 代码时遇到问题。
我想做的是用我的模拟代码替换 Linking.openURL 和 Linking.OpenURL.catch 。
我正在为 Linking.openURL 执行以下操作:
jest.mock('Linking', () => {
return {
openURL: jest.fn()
}
})
Linking.openURL.mockImplementation(() => true)
Run Code Online (Sandbox Code Playgroud)
但我不断得到:
TypeError: _reactNative.Linking.openURL(...).catch is not a function
Run Code Online (Sandbox Code Playgroud)
知道如何替换/禁用 catch 子句吗?
这是我的代码:
func1() {
switch (this.props.a) {
case 'NO':
this.alertMessage(`msg`)
break
case 'YES':
default:
Linking.openURL(url1).catch(err => { Linking.openURL(url2)
})
}
}
alertMessage = (title) => {
Alert.alert(
title,
'',
[
{ text: 'OK',
onPress: () => {
Linking.openURL(url1).catch(err => {
Linking.openURL(url2)
})
} },
{ text: 'Cancel',
onPress: () => {
this.setState({
stateVar1: true
})
},
style: 'cancel' }
]
)
};
Run Code Online (Sandbox Code Playgroud)
以防万一有人收到错误消息 Cannot find module 'Linking' from
只需替换Linking为react-native/Libraries/Linking/Linking.
所以你的代码应该是这样的:
jest.mock('react-native/Libraries/Linking/Linking', () => ({
openURL: jest.fn(() => Promise.reject('some error reason'))
}));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1710 次 |
| 最近记录: |