how do I test a class in mocha?

Viewed 6417

In my journey towards TDD, I am using Mocha, chai and sinon. There certainly is a learning curve there.

My goal is to write a test to verify that method4 was executed. How do I achieve that ?

//MyData.js

 class MyData {

      constructor(input) {
         this._runMethod4 = input; //true or false
         this.underProcessing = this.init();
      }     

      method1() { return this.method2() }

      method2() {

        if (this._runMethod4) {
          return this.method4();
        } else {
         return this.method3();
       }

      method4(){
        return thirdPartyAPI.getData();
      }
      method3(){
        return someAPI.fetchData();
      }

      init(){
         return this.method1();
      }

    }

MyData.spec.js

describe('MyData', () => {

  it('should execute method 4', function() {
      let foo = new MyData(true);

      foo.underProcessing.then(()=>{
       // How do I verify that method4 was executed ??
        expect(foo.method4.callCount).to.equal(1);
      });


   });
 })
1 Answers
Related