unit testing fading out directive - cannot see the right CSS class added -angular & jasmine

旧街凉风 提交于 2019-12-12 02:53:35

问题


i'm trying to test the very simple directive i have made.

.directive('cssnotification', [ '$timeout', function($timeout) {
return {
    restrict:'A',
    link:function(scope, element, attrs) {

        scope.$watch('success',function(newVal ,oldVal){

            if(newVal){                                
                $timeout(function(){                        

                         scope.success = false;                                                      

                },1000);
            }
        },true)
    }  
}
 }])

This directive makes some element to be visible for 1 second and after that - fades it out. (here is the fiddle that demonstrates the problem)

I know that for perform the fadingOut animation angular should add "ng-hide-add" class to the element, and that is that i'm trying to test. In the browser it works (remove angular-mocks from fiddler and it will run) but in unit tests - this test fails for some reason.

I guess it has something to do with injecting of ng-animate...

Thanks forwards


回答1:


Found the solution: to wait some time until nganimate adds the class:

      $timeout(function(){
          expect(e.hasClass('ng-hide-add')).toEqual(true);
      },100)

here is fiddle



来源:https://stackoverflow.com/questions/20781057/unit-testing-fading-out-directive-cannot-see-the-right-css-class-added-angula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!