简体   繁体   English

react-native中的onClick函数测试按钮

[英]Test button onClick function in react-native

I'm trying to write my first app using react native framework. 我正在尝试使用react native框架编写我的第一个应用程序。 I have problem with test onClick function for button. 我有按钮测试onClick功能的问题。

My App.js code: 我的App.js代码:

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Button } from 'react-native';

export default class App extends React.Component {
  refreshData() {
    console.log("download data from server placeholder");
  }
  render() {
    return (
      <View style={styles.container}>
        <Text>Shake your phone to open the developer menu.</Text>
        <Button
          id="refreshButton"
          onPress={this.refreshData}
          title="Refresh"
          color="#000000"
          accesibilityLabel="Refresh AQI data"
          testID="refreshButton"
        />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

My App.test.js code: 我的App.test.js代码:

import React from 'react';
import App from './App';

import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

Enzyme.configure({ adapter: new Adapter() });

import renderer from 'react-test-renderer';
import { shallow, mount, render } from 'enzyme';

it('should render without throwing an error', function() {
  const wrapper = shallow(<App />);
  const refreshData = jest.fn();
  expect(wrapper.find('#refreshButton')).toHaveLength(1); // pass
  wrapper.find('#refreshButton').first().simulate('click');
  expect(refreshData).toHaveBeenCalledTimes(1); // fail
});

When I run npm test my test fail because refreshData has been called zero times. 当我运行npm测试时,我的测试失败,因为refreshData被调用了零次。 Looks like simulate function doesn't work, why? 看起来模拟功能不起作用,为什么? I tried call simulate without first() function but it didn't work. 我试过没有first()函数调用simulate,但它没有用。 I'm not familiar with javascript, maybe I did an obvious mistake? 我不熟悉javascript,也许我犯了一个明显的错误? I found that code in documentation and some tutorials which i found in internet. 我在网络上找到了文档中的代码和一些教程。 I'm writting simple ios application only for me and for training myself and I'm big fan of TDD. 我正在为我编写简单的ios应用程序并进行自我训练,而且我是TDD的忠实粉丝。 It is the reason that Unit Tests are important for me. 这就是单元测试对我来说很重要的原因。

使用以下内容更改onPress = {this.refreshData}部分:

onClick={this.refreshData}

I am not sure enzyme support ID and # selectors. 我不确定酶支持ID和#选择器。 Try adding a testID prop and calling it like this: 尝试添加一个testID prop并像这样调用它:

wrapper.dive().find("[testID='yourTestID']").simulate("press");

or 要么

wrapper.dive().find("[testID='yourTestID']").onPress();

I am using both and they work fine. 我正在使用它们并且它们工作正常。

Edit: now your problem is this 编辑:现在你的问题是这个

const refreshData = jest.fn(); const refreshData = jest.fn();

Change to: 改成:

const refreshData = jest.spyOn(wrapper.instance(), "refreshData"); const refreshData = jest.spyOn(wrapper.instance(),“refreshData”);

Edit2: EDIT2:

Update button code to this: 将按钮代码更新为:

<Button
          id="refreshButton"
          onPress={this.refreshData}
          title="Refresh"
          color="#000000"
          accesibilityLabel="Refresh AQI data"
          testID="refreshButton"
        />

and test code to this: 并测试代码:

it('should render without throwing an error', function() {
  const wrapper = shallow(<App />);
  let refreshData = jest.spyOn(wrapper.instance(), "refreshData");
  expect(wrapper.find('#refreshButton')).toHaveLength(1);
  wrapper.find("[testID='refreshButton']").onPress();
  expect(refreshData).toHaveBeenCalledTimes(1);
});

If that does not work, I am clueless. 如果这不起作用,我无能为力。

Yes, it will now work because you haven't bind the function. 是的,它现在可以工作,因为你没有绑定该功能。 Try to use arrow function instead like below so that you don't want to do that. 尝试使用箭头功能,如下所示,这样你就不想这样做了。

import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { Button } from 'react-native';

export default class App extends React.Component {
  refreshData = () => {
    console.log("download data from server placeholder");
  }
  render() {
    return (
      <View style={styles.container}>
        <Text>Shake your phone to open the developer menu.</Text>
        <Button
          id="refreshButton"`enter code here`
          onPress={this.refreshData}
          title="Refresh"
          color="#000000"
          accesibilityLabel="Refresh AQI data"
          testID="refreshButton"
        />
      </View>
    );
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM