如何单元测试绑定到滚动的角度指令

时间:2017-04-05 16:55:45

标签: angularjs unit-testing karma-jasmine

我有一个angular指令,它返回滚动事件中元素的滚动顶部属性,但我有一些问题单元测试它。

如何模拟滚动事件以便更改scrollTop属性?

angular
  .module('app')
  .directive('scrollTop', scrollTop);

/** @ngInject */
function scrollTop() {
  return {
    restrict: 'A',
    scope: {
      scroll: '=scrollTop'
    },
    link: function (scope, element) {
      element.bind('scroll', function () {
        scope.scroll = element[0].scrollTop;
      });
    }
  };
}

describe('tests for scroll top directive', function () {
  beforeEach(module('app'));

  it('should make the scrollTop of the element available', angular.mock.inject(function ($rootScope, $compile) {
    $rootScope.scroll = 0;
    var element = $compile('<div style="height: 1px;width: 5px;" scroll-top="scroll"></div>')($rootScope);
    $rootScope.$digest();
    element.css({scrollTop: 100})
    element.triggerHandler('scroll');
    expect($rootScope.scroll).toEqual(100);
  }));
});

0 个答案:

没有答案