use*_*437 2 unit-testing jasmine phantomjs angularjs karma-runner
如何在Jasmine单元测试中模拟或模拟窗口滚动并设置window.pageYOffset属性?
我正在使用Angular 1.3,Jasmine 2.1 + Karma 0.12.28和PhantomJS 1.9.12
这是我的指令:
'use strict';
(function () {
angular
.module('myApp')
.directive('scrollNews', scrollNews);
function scrollNews(){
var directive = {
restrict: 'A',
scope: false,
link: link
};
return directive;
function link(scope, element) {
scope.limit = 2;
//add one to the limit
scope.loadMore = function() {
scope.limit += 1;
};
var raw = element[0];
angular.element(window).on('scroll', function () {
var scrollFrontier = this.pageYOffset + 800;
// when scrollFrontier has reached raw.scrollHeight, run loadMore()
if (scrollFrontier >= raw.scrollHeight) {
scope.loadMore(); // run the function
scope.$digest(); // update the HTML
}
});
}
}
})();
Run Code Online (Sandbox Code Playgroud)
这是我的testSpec:
describe('Directive: scroll news', function(){
var element, $scope, $window;
beforeEach(module('myApp'));
beforeEach(inject(function($compile, $rootScope, _$window_){
$scope = $rootScope;
$window = _$window_;
element = angular.element('<div scroll-news></div>');
$compile(element)($scope);
$scope.$digest();
}));
it('should have a limit of 2 when no scrolling have occurred', function(){
expect($scope.limit).toBe(2);
});
it('should add one to the limit with loadMore function', function(){
$scope.loadMore();
expect($scope.limit).toBe(3);
});
it('should run loadMore() when scrolling has reached a specific height', function(){
element.scrollHeight = 1400;
var spy = spyOn($window, 'scrollTo');
$window.scrollTo(0, 1400);
expect(spy).toHaveBeenCalled();
expect($scope.limit).toBe(3); // Logs: Expected 2 to be 3. - so loadMore() have not called
});
});
Run Code Online (Sandbox Code Playgroud)
我想模拟或模拟到element.scrollHeight的窗口滚动事件,因为如果这两个达到相同的高度,我的指令将运行loadMore()函数。但是我该如何进行单元测试?
顺便说一句,该指令在生产中效果很好:)
最近有同样的问题。为了使滚动正常进行,您需要在body标签上设置一些尺寸,以便可以滚动窗口。
var scrollEvent = document.createEvent( 'CustomEvent' ); // MUST be 'CustomEvent'
scrollEvent.initCustomEvent( 'scroll', false, false, null );
var expectedLeft = 123;
var expectedTop = 456;
mockWindow.document.body.style.minHeight = '9000px';
mockWindow.document.body.style.minWidth = '9000px';
mockWindow.scrollTo( expectedLeft, expectedTop );
mockWindow.dispatchEvent( scrollEvent );
Run Code Online (Sandbox Code Playgroud)
不幸的是,这在PhantomJS中不起作用。
如果要在Travis CI上运行测试,则还可以通过将以下内容添加到.travis.yml中来使用Chrome。
before_install:
- export CHROME_BIN=chromium-browser
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
Run Code Online (Sandbox Code Playgroud)
在业力配置中添加一个自定义Chrome启动器:
module.exports = function(config) {
var configuration = {
// ... your default content
// This is the new content for your travis-ci configuration test
// Custom launcher for Travis-CI
customLaunchers: {
Chrome_travis_ci: {
base: 'Chrome',
flags: ['--no-sandbox']
}
},
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: true
};
if(process.env.TRAVIS){
configuration.browsers = ['Chrome_travis_ci'];
}
config.set( configuration );
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6415 次 |
| 最近记录: |