How to mock an ajax call in mocha using sinon without setting timeout on a ractive component using reflux store

Viewed 840

I have a working ractive component test case already with mocha using sinon ans able to mock an ajax call but with the help of setTimeout(function(){}, 100) and I dont like using it.

beforeEach(function () {
  this.container = document.createElement('div');
  document.body.appendChild(this.container);

  this.server = sinon.fakeServer.create();
  this.server.respondWith(
    "GET",
    "/api/url",
    [
      200,
      { "Content-Type": "application/json" },
      '{"data": []}'
    ]
  );
});

afterEach(function () {
  document.body.removeChild(this.container);
});

it("should fetch data from server", function (done) {
  var server = this.server;
  var Component = require('rvc!path/to/component');
  var component = new Component({
    el: this.container,
  });

  setTimeout( function() {
    server.respond();

    expect(component.findAll('.list li').length).to.equal(7);
    done();
  }, 100);

});

As you can see in the code above, I'm using the setTimeout to make sure that the ajax call (mock) was made before having the actual test of the component.

Is there a way that I can eliminate the setTimeout having the same effect? Thanks!

1 Answers
Related