Each test method is going to have a certain amount of information that is required to run a test. You need to generate a component object using the componentFactory (line 85) for all controller tests.

In the componentFactory, all the objects component.get interacts with are passed in.  All the component.find objects are also defined here.  In the example below, the eventObj, usrticket and salesOrderObj objects are passed in. Then the isModal in sent in as a component that returns a true or false response so that you can perform a component.find.

EventRegistrationFlowDetailsControllerSpec.js

@testMethod('shwoCancelModal - yeah shwo')
   shwoCancelModal() {
       const component = componentFactory({
           findMap: {
               cancelAttendeeSetup: 'Framework:Button',
               cancelAddTickets: 'Framework:Button',
           }
       });

       component.find('cancelAddTickets').showModal = sinon.spy();

       controller.shwoCancelModal(component);

       expect(component.find('cancelAttendeeSetup').stopIndicator).to.have.been.called;
   }
JS

 

Anything you do as an object (lines 87-91) is where you can use component.set('v.eventObj',{}). This is called in your Salesforce Lightning component object. These attributes are also available to your Lightning Component via the component.get method.

EventRegistrationFlowDetailsController.js

component.get('v.isModal')
JS


EventRegistrationFlowDetailsController.js

({
    doInit: function(component, event, helper) {
        document.title = $A.get('$Label.c.Attendee_Selection_Title')+' - '+component.get('v.eventObj.name');
        helper.setupFormDataAndBuildSOLEntries(component, true);
        if (component.get('v.isModal')) {
            helper.openModal(component);
        }
        if ($A.get("$Browser.isPhone")) {
            document.body.classList.add('noscroll')
        }
    },
JS


The isModal attribute is being set on line 91 of EventRegistrationFlowDetailsControllerSpec.js above, so you can do the following:

EventRegistrationFlowDetailsControllerSpec.js

component.get('v.isModal')
JS

 

After you call the controller, you send your mocked-out component object (line 94 below). It is not required to send an event in this scenario. Just send an empty object. If you are testing a scenario where the method is handling an Aura event, then you will need to provide the mock for that event type. The helper file has already been stubbed out. 

EventRegistrationFlowDetailsControllerSpec.js

controller.doInit(component, {}, helper);
JS

 

The above code will call the doInit method in the controller and execute all contained code. Once that is complete, you can execute your assertions to make sure everything executes correctly. When the controller ran, no matter the value of isModal, a helper method was called. The assertion to validate that is below:

EventRegistrationFlowDetailsControllerSpec.js

expect(helper.setupFormDataAndBuildSOLEntries).to.have.been.calledWith(component);
JS

 

If isModal is true, then in our example where a helper method is called, we can verify that it was called with the correct attributes. This way, you can make sure that the proper methods are being called with the correct values.

EventRegistrationFlowDetailsControllerSpec.js

if (isModal) {
   expect(helper.openModal).to.have.been.calledWith(component);
}
JS

classyRegister

 At the bottom, right outside of the class, enter classyRegister  and the name of the class (line 46 of EventRegistrationFlowDetailsControllerSpec.js).

EventRegistrationFlowDetailsControllerSpec.js

classyRegister(EventRegistrationFlowDetailsController);
JS

 You can put as many classes inside of a single JavaScript file as you want. Fonteva usually does a one to one: one controller to one controller with a helper.

Timer

If you need to wait a certain amount of time to execute specific code, use a timer, clock.tick.

You can simulate, for example, 501 milliseconds before it moves on and executes certain code.

The this.clock.tick is based on whatever the setTimeout (line 1) is set to.

this.clock.tick(501);
CODE
setTimeout($A.getCallback(function(){
   alert('Hello World');
}),500);
JS

 

 Previous: Calling Test Methods     |     First: Unit Testing