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



我对 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>

收到以下错误:">模拟"方法应在 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();

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

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

尽管这可以简化为:

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

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

最新更新