angularjs - Testing event chains and module.run() in Angular + Jasmine -


i trying test module has following code:

angular.module('angularenterpriseauthorization').run(['$rootscope', '$state', 'appconfig',   function($rootscope, $state, appconfig) {      // on every time user changes state check see if user has permissions go new state     $rootscope.$on("$statechangestart", function(event, tostate, toparams, fromstate, fromparams) {         // if state not 1 of public states defined in modules config         if (appconfig.publicstates.indexof(tostate.name) < 0) {             event.preventdefault();              $state.go(tostate, toparams, {notify: false}).then(function() {                       $rootscope.$broadcast('$statechangesuccess', tostate, toparams, fromstate, fromparams);             });          }     }); ]); 

my test looks this:

beforeeach(module('angularenterpriseauthorization', 'coreconfiguration'));   beforeeach(inject(function(_$rootscope_, _$httpbackend_, _appconfig_) {     $scope = _$rootscope_.$new();     $httpbackend = _$httpbackend_;     appconfig = _appconfig_     spyon($scope, '$broadcast').andcallthrough(); }));   it('should allow navigation public states', function() {       $scope.$broadcast('$statechangestart', [{},{name:appconfig.publicstates[0]}]);     expect($scope.$broadcast).tohavebeencalledwith('$statechangestart', [{}, {name:     appconfig.publicstates[0]}]);     $scope.$broadcast.reset();     expect($scope.$broadcast).tohavebeencalledwith('$statechangesuccess'); }); 

the problem having second expect returning false. think issues module not being initialized same $rootscope.

any appreciated! thanks

in run block, subscribe $statechangestart on $rootscope , broadcast $statechangesuccess event $rootscope.

in test, have same, using $rootscope. may change line:

$scope = _$rootscope_.$new(); 

to this:

$scope = _$rootscope_; 

and have remove $scope.$broadcast.reset(), clear remembered calls.

to test second call of same method, this:

it('should allow navigation public states', function() {       $scope.$broadcast('$statechangestart', [{},{name:appconfig.publicstates[0]}]);     expect($scope.$broadcast).tohavebeencalledwith('$statechangestart', [{}, {name:     appconfig.publicstates[0]}]);     $scope.$apply();     expect($scope.$broadcast.calls[1].args[0]).toequal('$statechangesuccess'); }); 

hope helps.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -