Skip to content

Commit a5756c3

Browse files
fix(state): fire $stateChangeError if onEnter/onExit throws.
closes #2772
1 parent 8222fb0 commit a5756c3

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

test/stateSpec.js

+33-21
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ describe('state', function () {
3737
ISS2101 = { params: { bar: { squash: false, value: 'qux'}}, url: '/2101/{bar:string}' };
3838
AppInjectable = {};
3939

40-
beforeEach(module(function ($stateProvider, $provide) {
40+
beforeEach(module(function ($stateProvider, $provide, $exceptionHandlerProvider) {
41+
var x = this;
42+
var foo = jasmine;
43+
$exceptionHandlerProvider.mode('log')
4144
angular.forEach([ A, B, C, D, DD, E, H, HH, HHH ], function (state) {
4245
state.onEnter = callbackLogger('onEnter');
4346
state.onExit = callbackLogger('onExit');
@@ -120,11 +123,6 @@ describe('state', function () {
120123
},
121124
controller: function() { log += "controller;"}
122125
})
123-
.state('onEnterFail', {
124-
onEnter: function() {
125-
throw new Error('negative onEnter');
126-
}
127-
})
128126
.state('badParam', {
129127
url: "/bad/{param:int}"
130128
})
@@ -556,20 +554,6 @@ describe('state', function () {
556554
'A.onEnter;');
557555
}));
558556

559-
it('sends $stateChangeError for exceptions in onEnter', inject(function ($state, $q, $rootScope) {
560-
var called;
561-
$rootScope.$on('$stateChangeError', function (ev, to, toParams, from, fromParams, options) {
562-
called = true;
563-
});
564-
565-
initStateTo(A);
566-
$state.transitionTo('onEnterFail');
567-
$q.flush();
568-
569-
expect(called).toBeTruthy();
570-
expect($state.current.name).toEqual(A.name);
571-
}));
572-
573557
it('doesn\'t transition to parent state when child has no URL', inject(function ($state, $q) {
574558
$state.transitionTo('about.sidebar'); $q.flush();
575559
expect($state.current.name).toEqual('about.sidebar');
@@ -1063,7 +1047,6 @@ describe('state', function () {
10631047
'logA',
10641048
'logA.logB',
10651049
'logA.logB.logC',
1066-
'onEnterFail',
10671050
'resolveFail',
10681051
'resolveTimeout',
10691052
'root',
@@ -1670,6 +1653,35 @@ describe('state queue', function(){
16701653
});
16711654
});
16721655

1656+
describe('exceptions in onEnter', function() {
1657+
beforeEach(module(function ($stateProvider, $exceptionHandlerProvider) {
1658+
$exceptionHandlerProvider.mode('log');
1659+
$stateProvider
1660+
.state('A', { })
1661+
.state('onEnterFail', {
1662+
onEnter: function() {
1663+
throw new Error('negative onEnter');
1664+
}
1665+
});
1666+
}));
1667+
1668+
it('sends $stateChangeError for exceptions in onEnter', inject(function ($state, $q, $rootScope) {
1669+
var called;
1670+
$rootScope.$on('$stateChangeError', function (ev, to, toParams, from, fromParams, options) {
1671+
called = true;
1672+
});
1673+
1674+
$state.go('A'); $q.flush();
1675+
expect($state.current.name).toEqual('A');
1676+
1677+
$state.transitionTo('onEnterFail');
1678+
$q.flush();
1679+
1680+
expect(called).toBeTruthy();
1681+
expect($state.current.name).toEqual('A');
1682+
}))
1683+
});
1684+
16731685
describe('$stateParams', function () {
16741686
beforeEach(module('ui.router.state'));
16751687

0 commit comments

Comments
 (0)