简体   繁体   中英

Mocha not calling my function in my test

I have this test:

it('calls addTheItem when clicked', () => {
  wrapper.find('.addBtn').simulate('click')
  expect(addTheItemStub.called).to.equal(true)
});

This is my code

<button
  className="addBtn"
  onClick={() => {this.addTheItem(inputValue); this.clearInput()}}
  >
  Add Item
</button>

This code works when I play around with it. just the test isn't passing.

also in my beforeEach:

    const addTheItemStub = sinon.spy();
    const addItemStub = sinon.spy();
    const usingEnterKeyStub = sinon.spy();
    beforeEach(() => {
      wrapper = shallow(
        <Container
          addTheItem={addTheItemStub}
          addItem={addItemStub}
          items={itemsStub}
          usingEnterKey={usingEnterKeyStub}
        />
      )
    });

I'm stubbing it out. I'm expecting the stub to be called.

but it's expecting it to be not called or false. what am i doing wrong?

I see that in button's onClick handler, you are calling your methods as this.addTheItem and this.clearInput . But you are passing the stubs for these methods as props to the parent Container element.

The stubs won't be called unless you use the methods from props, ie, this.props.addTheItem and this.props.clearInput .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM