Skip to content
This repository has been archived by the owner on Aug 1, 2019. It is now read-only.

Async Operations

Yuriy Sannikov edited this page Jul 23, 2017 · 2 revisions

Mocha allow you to test asynchronous code. However it's better to avoid it. mocha-aura designed in a such a way that getCallback, createComponent, createComponents and enqueueAction invoke their callbacks immediately. So you don't need to deal with additional complexity. However there are a lot of Aura code that uses setTimeout and setInterval calls. In order to continue using simpler approach to test timeouts and make tests faster use sinon.useFakeTimers() call. It returns a handle which later on you can use to simulate timeout. In this example tested code using setTimeout(..., 500). Following call to clock.tick(501) invokes setTimeout callback in a sync manner.

before(function() {
  clock = sinon.useFakeTimers();
});
 
describe('buildInvitationInputField', function() {
  it('should build radio input', function() {

    // Check created component has been placed into proper div
    expect(acceptInvitationDiv.get('v.body')[0]).to.equal(createdComponent);

    // showLoadedData should not be called right after component creation
    expect(global.FontevaHelper.showLoadedData).not.to.have.been.called;

    // showLoadedData should be called after 500ms timeout
    clock.tick(501);
    expect(global.FontevaHelper.showLoadedData).to.have.been.calledWith(component);
  });
});
Clone this wiki locally