OnClick酶-测试错误:“模拟”方法应在1个节点上运行。找到0个代替

时间:2019-02-11 21:22:38

标签: reactjs unit-testing enzyme

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

        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>

出现以下错误:方法“ simulate”应在1个节点上运行。改为找到0。我在此处发布了大部分测试文件,但我相信主要错误来自此行:

     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();

1 个答案:

答案 0 :(得分:1)

可能只是在其他类名的前面缺少了.

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

尽管可以将其简化为:

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

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