测试失败,未定义警报

问题描述 投票:0回答:1

我对本机项目使用笑话,我想测试具有“ onPress”的组件。单击它时,会出现警报。

<ListItem
      testID={'Contact'}
      onPress={() => alert('hello')}
/>

并对其进行测试;

it('test onPress functionality', () => {
    window.alert = jest.fn();
    const wrapper = shallow(
      <Contact
        onPress={window.alert}
      />,
    );
    wrapper
      .findWhere(node => node.prop('testID') === 'Contact')
      .props()
      .onPress();
    expect(window.alert).toHaveBeenCalledWith('hello');
  });

但是此测试给出警报错误。

ReferenceError:警报未定义onPress = {()=> alert('hello')}] >>

请帮助我解决此错误。

我对本机项目使用笑话,我想测试具有“ onPress”的组件。单击它时,会出现警报。 alert('hello')} ...

reactjs unit-testing react-native enzyme jest
1个回答
0
投票

您的测试不正确,因为您正在测试'onPress'作为参数。

© www.soinside.com 2019 - 2024. All rights reserved.