cjp666 cjp666 - 2 months ago 9
AngularJS Question

Writing JaveScript tests that test other functions are called, without actually calling them

I have been tasked with writing unit tests for some AngularJS code that was written by another team, who didn't write any tests

They have written the following function but I cannot figure out how to test it

function showCallAlerts(callRecord, isInEditMode, callBack) {
var callAlerts = populateCallAlertOnEditCall(callRecord.callAlert);
var callModalInstance = openAlertModalInstance('Call', callAlerts, callBack);
if (callModalInstance !== undefined && callModalInstance !== null) {
callModalInstance.result.then(function() {
// Show equipment alerts based on company details
showEquipmentAlertsBasedOnCompanyDetails(callRecord, isInEditMode, callBack);
});
} else {
// Show equipment alerts based on company details
showEquipmentAlertsBasedOnCompanyDetails(callRecord, isInEditMode, callBack);
}
}


I need to test that each of the functions are called, not worrying about what they do as I'll test them separate, just that they are called.

When populateCallAlertOnEditCall is called it needs to either return an empty array or an array with some items in it

When openAlertModalInstance is called it needs to either return undefined or something that passes through to showEquipmentAlertsBasedOnCompanyDetails

showEquipmentAlertsBasedOnCompanyDetails should actually be called, I'll test that method separate, just that it was called

I have manged to write code to test simple functions but nothing like this one so any help will be much appreciated, I spent most of this afternoon trying to figure it out

Answer

You can use jasmine to mock the function calls that you are not interested in testing. For example, you can tell jasmine to return an empty array every time 'populateCallAlertOnEditCall' is called. I will write an example that might give you an insight:

describe('My Test Spec', function() {
   var myController;

   ...

   beforeEach( inject(($controller) => {
        myController = $controller("myControllerName");
   }));

  it('Testing showCallAlerts when populateCallAlertOnEditCall returns an empty array', inject(function($controller) {
        //setup
        //this will replace every call to populateCallAlertOnEditCall with
        //the function inside callFake
        spyOn(myController, 'populateCallAlertOnEditCall ').and.callFake(function() {
              return []; //returning an empty array.
        });

        //action
        myController.showCallAlerts(...);

        //assert
        //Do your checking here.
  }));

  it('Testing showCallAlerts when populateCallAlertOnEditCall returns a non-empty array', inject(function($controller) {
        //setup
        //this will replace every call to populateCallAlertOnEditCall with
        //the function inside callFake
        spyOn(myController, 'populateCallAlertOnEditCall ').and.callFake(function() {
              return [1,2,3,4]; //returning a non-empty array.
        });

        //action
        myController.showCallAlerts(...);

        //assert
        //Do your checking here.
  }));

 });