jeudi 7 janvier 2016

How to test a function that get a funciton as parameter?

I'm trying to do the following: I have a service that has a function called loadingWrapper, that gets a function as parameter (this function returns a promise). before and after the given function - i toggle loading spinner on and off. My goal is to test this flow.

My current code:

Service:

// Wrapping a promise function with loading spinner
function loadingWrapper(scope,spinnerName,func) {
  $timeout(function(){
    toggleSpinner(scope, spinnerName, true);

    func.then(function (){
      toggleSpinner(scope, spinnerName, false);
    });

  }, 100);
}

Jasmine:

describe('Loading wrapper works',function(){
it('language list', function() {
  httpBackend.when('GET','dictionaries/dict-en.json').respond();
  httpBackend.when('GET','scripts/login/login.tpl.html').respond();

  var func = function()
  {
    var defer = q.defer();
    console.log('BLAH');
    defer.resolve('d');
    return defer.promise;
  }


  spyOn(layoutService, 'toggleSpinner');
  layoutService.loadingWrapper(scope,"main",func);

  timeout.flush();

  timeout.verifyNoPendingTasks();
  // spyOn(this, 'func');
  scope.$apply();
  scope.$digest();
  expect(layoutService.toggleSpinner).toHaveBeenCalled();
  httpBackend.flush();
  });
});

Currently im getting "func.this" is not a function (inside the service)

Thank yall.

Aucun commentaire:

Enregistrer un commentaire