【问题标题】:Simulate a button click in react test case在反应测试用例中模拟按钮单击
【发布时间】:2019-03-26 11:53:26
【问题描述】:

您好,我有以下代码,我需要在使用按钮和链接时更改窗口位置。

export default class CampaignAdSection extends Component {
  constructor(props) {
    super(props)
    this.customiseClickable = this.customiseClickable.bind(this);
  }
  customiseClickable(e) {
    this.props.history.push(`/customize/aus`);
  }

  render() {
    let { packages, history, campaignDetails } = this.props;
    return (
     <div className="xs-text-center">
          <button
            type="button"
            className="btn btn-primary pkg-adbtn"
            onClick={e => this.customiseClickable(e)}
          >
            Trip
          </button>{' '}
          <b className="color-grey-secondary">
            {' '}
            or,{' '}
            <Link
              to={
                `${
                  history.location.pathname.indexOf('request-callback') > 0
                    ? history.location.pathname.split('request-callback')[0]
                    : history.location.pathname + '/'
                }request-callback` +
                `${history.location.search ? history.location.search : ''}`
              }
              className="tracker-talkbtn"
            >
              Expert
            </Link>
          </b>
        </div>

    );
  }
}

根据我的代码,它一直在工作。但是我怎样才能为此添加规范?我是反应测试的初学者。能不能给点意见?

我想单击该按钮并检查位置是否已更改?

【问题讨论】:

    标签: reactjs unit-testing jestjs


    【解决方案1】:

    您可以使用 Jest 模拟按钮单击以进行测试。

    查看此内容以了解如何: Simulate a button click in Jest

    【讨论】:

    • 嗨,我可以触发按钮单击,但它引发了以下错误,VM2313:1 Uncaught TypeError: this.props.history.push is not a function 在浏览器中运行时它工作正常,但是当我尝试检查测试用例(节点)时,它显示该错误.所以我无法测试
    • 没有人说“使用酶”
    猜你喜欢
    • 2016-09-12
    • 2016-09-10
    • 2019-12-22
    • 2017-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2018-07-17
    相关资源
    最近更新 更多