Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 你会如何嘲笑';onPress';处于警戒状态?_Javascript_Unit Testing_React Native_Jestjs_Enzyme - Fatal编程技术网

Javascript 你会如何嘲笑';onPress';处于警戒状态?

Javascript 你会如何嘲笑';onPress';处于警戒状态?,javascript,unit-testing,react-native,jestjs,enzyme,Javascript,Unit Testing,React Native,Jestjs,Enzyme,我能够模拟警报来测试它的警报方法是否被调用,但我真正想要测试的是在警报中按下ok按钮 import { Alert } from 'react-native'; it('Mocking Alert', () => { jest.mock('Alert', () => { return { alert: jest.fn() } }; }); const spy = jest.spyO

我能够模拟警报来测试它的警报方法是否被调用,但我真正想要测试的是在警报中按下ok按钮

import { Alert } from 'react-native';

it('Mocking Alert', () => {
    jest.mock('Alert', () => {
        return {
          alert: jest.fn()
          }
        };
      });

    const spy = jest.spyOn(Alert, 'alert');
    const wrapper = shallow(<Search />);

    wrapper.findWhere(n => n.props().title == 'Submit').simulate('Press');
    expect(spy).toHaveBeenCalled(); //passes
})
从'react native'导入{Alert};
它('模拟警报',()=>{
jest.mock('Alert',()=>{
返回{
警告:jest.fn()
}
};
});
const spy=jest.spyOn(警报,'Alert');
常量包装器=浅();
wrapper.findWhere(n=>n.props().title=='Submit').simulate('Press');
expect(spy).tohavebeencall();//传递
})
我完全不确定如何测试它。这是一个通用组件,我正试图测试它

export default class Search extends Component{

    state = {
      someState: false
    }

    confirmSubmit(){
      this.setState(state => ({someState: !state.someState}))
    }

    onPress = () => {
      Alert.alert(
        'Confirm',
        'Are you sure?'
        [{text: 'Ok', onPress: this.confirmSubmit}] //<-- want to test this
      )
    }

    render(){
      return(
       <View>
         <Button title='Submit' onPress={this.onPress}
       </View>
      )
    }
}
导出默认类搜索扩展组件{
状态={
someState:false
}
确认提交(){
this.setState(state=>({someState:!state.someState}))
}
onPress=()=>{
警惕,警惕(
“确认”,
“你确定吗?”

[{text:'Ok',onPress:this.confirmSubmit}]/我将模拟模块并将其导入spy上进行测试。然后触发click事件。这将调用spy。从spy中,您可以获取调用它的参数,以获取
onPress
方法并调用它。然后您可以测试组件的状态

import Alert from 'Alert'

jest.mock('Alert', () => {
    return {
      alert: jest.fn()
    }
});


it('Mocking Alert', () => {
    const wrapper = shallow(<Search />);
    wrapper.findWhere(n => n.props().title == 'Submit').simulate('Press');
    expect(Alert.alert).toHaveBeenCalled(); // passes
    Alert.alert.mock.calls[0][2][0].onPress() // trigger the function within the array
    expect(wrapper.state('someState')).toBe(true)
})
从“警报”导入警报
jest.mock('Alert',()=>{
返回{
警告:jest.fn()
}
});
它('模拟警报',()=>{
常量包装器=浅();
wrapper.findWhere(n=>n.props().title=='Submit').simulate('Press');
expect(Alert.Alert).tohavebeencall();//传递
Alert.Alert.mock.calls[0][2][0].onPress()//触发数组中的函数
expect(wrapper.state('someState')).toBe(true)
})

我在测试警报和尝试为警报模拟onPress时遇到了同样的问题。我正在使用TypeScript实现我的代码。我通过使用spyOn成功地解决了这个问题,比如:

const spyAlert = jest.spyOn(Alert, 'alert');
然后要使用onPress,您需要忽略行的类型检查,否则您将得到-无法调用可能是“未定义”的对象

// @ts-ignore
spyAlert.mock.calls[0][2][0].onPress();

对不起,我应该在我的问题中包括这一点。这基本上是我正在做的。我在测试中导入了警报。我要测试的是警报方法
Alert.Alert('Confirm','you sure?'[{text:'Ok',onPress:this.confirmsmit}]中的onPress//更新了答案以反映您的尖锐问题它不适用于我
expect(jest.fn())[.not]。TohaveBeenCall()jest.fn()值必须是模拟函数或间谍。
从模拟的第一次调用(
mock.calls[0]
)获取第三个参数([2]),即数组
[{text:'Ok',onPress:this.confirmSubmit]
,因此从中获取第一项([0])@Yossi请为此创建一个新问题