我有一个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);
}));
});