OnClick Enzyme - test Error: Method “simulate” is

2019-08-29 03:49发布

问题:

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>

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

Pros are all set :

 // 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:

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

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

Though this can likely be simplified to just:

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

Unless you have multiple enrollment buttons on your page.