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

How to mock Helper calls

Yuriy Sannikov edited this page Jul 23, 2017 · 1 revision

In this example Sinon creates a stub function for some of Aura helper methods. Original controller code then might call these mocked methods without worrying about underlying helper implementation complexities. This helps to isolate testing scope and reduce test complexity by concentrating only on particular controller code.

Controller Spec Code

const ctl = require('...Controller');
const helper = require('....Helper');
beforeEach(function() {
  component = componentFactory();

  sinon.stub(helper, 'buildInvitationInputField');
  sinon.stub(helper, 'updateRSVPPane');
  sinon.stub(helper, 'loadTicketTypes');
  sinon.stub(helper, 'checkSelection');
  sinon.stub(helper, 'calculateWaitlistCount');
  sinon.stub(helper, 'declineAttendeeRSVP');
  sinon.stub(helper, 'registerForTickets');


});

afterEach(function() {
  helper.buildInvitationInputField.restore();
  helper.updateRSVPPane.restore();
  helper.loadTicketTypes.restore();
  helper.checkSelection.restore();
  helper.calculateWaitlistCount.restore();
  helper.declineAttendeeRSVP.restore();
  helper.registerForTickets.restore();
});

describe('doInit', function() {
  it('should hide RSVP for incorrect state', function() {
    const event = eventFactory();
    const invOnlyRegistrationContainer = componentFactory()
    component = componentFactory({
      attendeeObj: {
        invitationSent: false,
        invitationAccepted: false,
        invitationDeclined: false
 },
      findMap: { invOnlyRegistrationContainer }
    });
    ctl.doInit(component, event, helper);
    expect($A.util.addClass).to.have.been.calledWith(invOnlyRegistrationContainer, 'slds-hide');

  });

Controller Method Code

doInit: function(component, event, helper) {
  var attendeeObj = component.get('v.attendeeObj');
  if (component.get('v.isPreview') === true) {
     attendeeObj = _.assign({}, attendeeObj, {
         invitationSent: true,
         invitationAccepted: false,
         invitationDeclined: false,
         maxGuestsAllowed: 1
     });
     component.set('v.attendeeObj', attendeeObj);
  }
  if (!attendeeObj || !(attendeeObj.invitationSent || attendeeObj.invitationAccepted || attendeeObj.invitationDeclined)) {
    $A.util.addClass(component.find('invOnlyRegistrationContainer'), 'slds-hide');
    return;
  }
  var invitationDeclined = attendeeObj.invitationDeclined;
  var rsvpStatus = invitationDeclined ? helper.STATUS_DECLINE : helper.STATUS_ACCEPT;
  component.set('v.rsvpObject', {
    rsvpStatus: rsvpStatus,
    numberOfGuests: 0
 });

  helper.buildInvitationInputField(component);
  helper.updateRSVPPane(component, rsvpStatus);
  helper.loadTicketTypes(component);
},
Clone this wiki locally