angularjs - Testing ui.router $stateChangeSuccess -
i have simple function in main controller updates $scope.loading
based on ui.router's $statechangestart
, $statechangesuccess
events.
$scope.$on('$statechangestart', function(event, tostate) { if (tostate.resolve) { $scope.loading = true; } }); $scope.$on('$statechangesuccess', function(event, tostate) { if (tostate.resolve) { $scope.loading = false; } });
this works great until try unit test. can't find way trigger $statechangesuccess
event.
my jasmine unit test looks this:
it('should set $scope.loading true', function () { expect(scope.loading).tobe(false); $state.go('home'); expect(scope.loading).tobe(true); scope.$digest(); expect(scope.loading).tobe(false);
});
the test fails because $statechangesuccess
never fires. ideas appreciated.
this works me.
you need $broadcast event. read below bit of pseudocode. hope helps you.
// note: pseudocode // controller // assumes have usual setup support controller. // assumes controller attached component or angular controller. function mycontroller() { var vm = this; vm.ishappy = false; $scope.$on('$statechangesuccess', function(event, tostate) { if (tostate.name === 'mystate') { vm.ishappy = true; } }); } // test // assumes several things // * ctrl mocked controller // * has necessary setup & injections (such $rootscope). // * testing framework jasmine describe('what happens when go happy place?', function() { it('should visible happy.', function() { expect(ctrl.ishappy).tobefalsy(); $rootscope.$broadcast('$statechangesuccess', { 'name': 'mystate' }); // or $scope.$broadcast expect(ctrl.ishappy).tobetruthy(); } });
Comments
Post a Comment