OnClick酶-测试错误:“模拟”方法应在1个节点上运行。找到0个代替 [英] OnClick Enzyme - test Error: Method “simulate” is meant to be run on 1 node. 0 found instead

查看:60
本文介绍了OnClick酶-测试错误:“模拟”方法应在1个节点上运行。找到0个代替的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于React JS,我有以下情况。之前我问过类似的问题,并尝试对它应用相同的方法:

I have the following situation for React JS. I asked a similar question before and Tried to apply the same method to this:

        className={'custom-grid-buttons tran-button enrollment-button'} 
                  onClick={() => {this.props.openBatchUpdateLOAModal()}}
                >
                  Batch Update
                </button>
                <button 
                  className={'custom-grid-buttons tran-button enrollment-button'} 
                  onClick={() => {this.props.getSelectedLOAs().then(() => {
                    this.props.selectedLOAs && this.props.selectedLOAs.length > 0 ? this.props.openDownloadLOAModal() : alert('Please select at least one LOA.')})}}
                >
                  Download By Custodian
                </button>

出现以下错误:方法模拟应在1个节点上运行。而是找到0。我在此处发布了大多数测试文件,但我相信主要错误是来自于此行:

Getting the following error: Method "simulate" is meant to be run on 1 node. 0 found instead. I posted most of the test file here but I believe the main error is coming from this line :

     wrapper.find(".custom-grid-buttons tran-button enrollment-button").simulate("click"); 

所有优点均已设置:

 // jest mock functions (mocks this.props.func)
const setFromStatusList = jest.fn();
const openBatchUpdateLOAModal = jest.fn();
const getSelectedLOAs = jest.fn();
const getDynamicRender = jest.fn();
const openDownloadLOAModal = jest.fn();
const onClick =  jest.fn();
 // defining this.props
const baseProps = {
  location: {
    pathname:[],
 },
 services :{
    Counterparty :{
        URL : "TEST URL",
        subscription_key: "test key",
    },
},
 setFromStatusList,
 openBatchUpdateLOAModal,
 getSelectedLOAs,
 backgroundapp:{},
 getDynamicRender,
 openDownloadLOAModal,
 onClick,
  selectedLOAS:[],
  }

   beforeEach(() => wrapper = shallow(<BrowserRouter><LOA {...baseProps} /></BrowserRouter>));

      it("should call openBatchUpdateLOAModal click", () => {
// Reset info from possible previous calls of these mock functions:
baseProps.openBatchUpdateLOAModal.mockClear();
baseProps.getSelectedLOAs.mockClear();

wrapper.setProps({
 selectedLOAS: null
   });

// Find the button and call the onClick handler
wrapper.find(".custom-grid-buttons tran-button enrollment-button").simulate("click");
// Test to make sure prop functions were called via simulating the button click
expect(baseProps.openBatchUpdateLOAModal).toHaveBeenCalled();
 expect(baseProps.getSelectedLOAs).toHaveBeenCalled();


推荐答案

可能只是缺少其他类名称:

Likely just missing the . in front of the other class names:

 wrapper.find(".custom-grid-buttons .tran-button .enrollment-button").simulate("click"); 

尽管可以将其简化为j ust:

Though this can likely be simplified to just:

 wrapper.find(".enrollment-button").simulate("click"); 

除非页面上有多个注册按钮。

Unless you have multiple enrollment buttons on your page.

这篇关于OnClick酶-测试错误:“模拟”方法应在1个节点上运行。找到0个代替的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆