如何单元测试输入和解析 UI 路由器状态

How to unit test onEnter and resolve of ui-router state

本文关键字:UI 路由器 状态 和解 单元测试 输入      更新时间:2023-09-26

我正在尝试测试从以下状态的onEnter调用的Angular UI Bootstrap模式:

.state("profile.index.edit.services", {
        url: "/edit/services/:serviceCode",
        parent:"profile.index",
        onEnter: ['$stateParams', '$state', '$modal',function($stateParams, $state, $modal) {
            $modal.open({
                templateUrl: 'profile/edit-services-modal.html',
                controller: 'ProfileEditServicesController',
                resolve: {
                    profileData: function(CacheService){
                        return CacheService.getItem(CacheService.Items.Profile.loadedProfile);
                    },
                    allServicesList: function(ProfileService){
                        return ProfileService.getAllServicesList($stateParams.serviceCode,$stateParams.orgId);
                    },
                    serviceCode: function() {
                        return $stateParams.serviceCode;
                    }
                }                   
            }).result.then(function(result) {
                if (result) {
                    return $state.transitionTo("profile.index", { orgId: $stateParams.orgId });
                }
                else { // cancel
                    return $state.transitionTo("profile.index", { orgId: $stateParams.orgId }); // don't reload
                }
            }, function () {
                // executes on close/cancel/ESC
                return $state.transitionTo("profile.index", { orgId: $stateParams.orgId });
            });
        }]
    })  

我一直在尝试许多不同的方法来设置测试,但似乎无法弄清楚。 非常感谢任何意见或建议!!!

顺便说一下,上面的状态是这些状态的后代,我已经能够为该传递编写测试:

.state('profile.index', {
        url: '/:orgId',
        views: {
            'profile-index@profile': {
                templateUrl: 'profile/view-profile.html',
                controller: 'ProfileController'
            },
            'header@': {
                templateUrl: 'common/layout/header.html',
                controller: 'HeaderController'
            },
            'footer@':{
                templateUrl: 'common/layout/footer.html',
                controller: 'FooterController'
            }
        },
        resolve: {
            profileData: function(ProfileService, $stateParams){
                return ProfileService.getProfile($stateParams.orgId, true);
            }
        }
    })
    .state('profile.index.edit', {
        url: '',
        abstract: true
    })

你有没有弄清楚这一点? 你应该传递一个命名的控制器,这样你就可以直接测试该控制器(因为它只是一个函数),而不是依赖于触发的 onEnter。