vendredi 2 septembre 2016

Angular reloading page every $rootScope.$digest

I have an angular app with ui-router and I writing my unit tests using jasmine.

State for the / URL which performs a resolve to have some data available in the controller.

$stateProvider
      .state('teams', {
        url: '/',
        templateUrl: 'app/teams/teams.html',
        controller: 'CarsController as vm',
        resolve: {
          teams: function (teamService) {
            return teamService.getAll();
          },
          players: function (playerService) {
            return playerService.getAll();
          }
        }
      });

Funny thing is, when running my unit tests, every time I call $rootScope.$digest, the apps get reloaded and I get calls to the services defined in the resolve!

So for ie, a simple test for a directive ends up calling them:

describe('navbar directive', function() {
  var $compile,
      $rootScope,
      $templateCache;

  beforeEach(module('app'));

  beforeEach(inject(function(_$compile_, _$rootScope_, _$templateCache_){
    $compile = _$compile_;
    $rootScope = _$rootScope_;
    $templateCache = _$templateCache_;
  }));

  it('displays the template', function() {
    var text = 'MOCK HEADER';
    var mockTemplate = '<div>' + text + '</div>';
    $templateCache.put('app/navbar/navbar.html', mockTemplate);
    var element = $compile('<navbar></navbar>')($rootScope);
    $rootScope.$digest();
    expect(element.html()).toContain(text);
  });

});

I have checked my whole app almost statement per statement and for sure I am not refreshing the browser at any time.

Aucun commentaire:

Enregistrer un commentaire