Anto
Anto

Reputation: 4305

Checking whether a function is called when another function is triggered

I am new to AngularJS and Jasmine. Given the following controller, how do I test whether the allPanelsRetrieved() function is called when the $scope.getPanels is triggered?

angular.
module('panelList').
  controller('PanelListController', ['Panel', 'PanelSelection', '$scope', '$location', '$uibModal', '$rootScope',
    function PanelListController(PanelSelection, $scope, $location, $uibModal, $rootScope) {

      $scope.maxAbv = 2;
      $scope.minAbv = 12;
 

      this.allPanelsRetrieved = (index, before, filterParams) => {
        .....
      };

      $scope.getPanels = () => {
        const filterParams = {};
        filterParams.abv_lt = $scope.minAbv;
        filterParams.abv_gt = $scope.maxAbv;


        $scope.currentPagePanels = this.allPanelsRetrieved (1,[], filterParams);
      };

}]).
component('panelList', {
  templateUrl: '/components/panel-list/panel-list.template.html',
  controller:'PanelListController',
});

Upvotes: 0

Views: 55

Answers (2)

Aravind
Aravind

Reputation: 41581

I can see that allPanelsRetrieved seems to be a private(local) method and used inside that controller.

  1. You need not test private(local) methods execution.
  2. If you still want to check if the method is triggered or not you can use jasmine's toHaveBeenCalled() method

    execept(myMethod).toHaveBeenCalled(); passes when method is called.

Upvotes: 1

Christopher Powroznik
Christopher Powroznik

Reputation: 61

Assuming you want allPanelsRetrived to be called, then simply use a boolean.

var bool = false

this.allPanelsRetrieved = (index, before, filterParams) => {
        .....
        bool=true;
      };

      $scope.getPanels = () => {
        if (bool) {
        const filterParams = {};
        filterParams.abv_lt = $scope.minAbv;
        filterParams.abv_gt = $scope.maxAbv;

        $scope.currentPagePanels = this.allPanelsRetrieved (1,[], filterParams);

        } else {
            // allPanelsRetrieved was not called
        }
      };

Upvotes: 1

Related Questions