angular-ui / ui-router

The de-facto solution to flexible routing with nested views in AngularJS
http://ui-router.github.io/
MIT License
13.54k stars 3k forks source link

Not able to test transitions hooks with karma #3502

Closed floreks closed 4 years ago

floreks commented 7 years ago

This is a:

My version of UI-Router is: 1.0.5

I have tried to seek for help on StackOverflow already but there was no answer. Maybe one of devs that know what was changed will know why $scope.$broadcast() is not triggering transition hooks.

My post from SO:

I'm trying to migrate to ui-router 1.0.5 and have done most of the work but there are no examples of how to test new transition hooks that replaced $stateChangeXXX events listeners.

Code before:

scope.$on('$stateChangeSuccess', this.hideSpinner_.bind(this));

After:

this.transitions_ is a $transitions service exposed by ui-router this.transitions_.onSuccess({}, this.hideSpinner_.bind(this));

Before I was able to test it by using scope.$broadcast($stateChangeSuccess) and then scope.$apply(). This worked with ui-router 0.x:

expect(ctrl.loading).toBe(true);
expect(ctrl.showLoadingSpinner).toBe(true);

// when
scope.$broadcast('$stateChangeSuccess');
scope.$apply();

// then
expect(ctrl.loading).toBe(false);
expect(ctrl.showLoadingSpinner).toBe(false);
Any idea how to rewrite tests to work with new version of ui-router?
parky128 commented 6 years ago

@floreks - did you ever find a way to test the $transitions hooks? I am facing the exact same issue, where I now have custom logic within a hook I want to cover in my tests. I read that the hooks get triggered when calling $state.go(), which I tried in my tests, but it made no difference for me

floreks commented 6 years ago

did you ever find a way to test the $transitions hooks?

Unfortunately not. Clark.Hsu posted a workaround that works for tests. I have not found a clear way of testing custom logic in the hook.

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

This does not mean that the issue is invalid. Valid issues may be reopened.

Thank you for your contributions.