Friday, 9 June 2017

Testing code inside of a then() block

So I am using React and testing with Mocha/Chai/Enzyme/Sinon. I have a lot of asynchronous calls in my components and I want to test the code that comes after but can't figure out how.

So for example this is my reserveSession() method inside of my AddToCart component:

reserveSession() { 
  this.props.reserveExam(id).then((json) => {
    // Code I want to test 
   }).catch(...);
}

And here is the current setup of my test:

describe('AddToCartButton', () => {
  beforeEach(() => { 
    props = { 
      // set all props to empty or () => null 
    }
    wrapper = mount(<AddToCartButton {...props} />)
  }); 
});

So what I have found so far is that when I mock this.props.reserveExam in the before each, since it doesn't ever call the actual function, it never calls the code in the then, but if i just call the method it doesn't understand what the then is. Any ideas on how to test this? Thanks!



via Alioo

No comments:

Post a Comment