Javascript 为浏览器调整大小事件编写测试。幻影、摩卡、柴和;西农
我没有对事件做过很多测试,主要是业务逻辑&我试图找到测试类似浏览器调整大小事件的示例 所讨论的代码如下Javascript 为浏览器调整大小事件编写测试。幻影、摩卡、柴和;西农,javascript,phantomjs,mocha.js,sinon,chai,Javascript,Phantomjs,Mocha.js,Sinon,Chai,我没有对事件做过很多测试,主要是业务逻辑&我试图找到测试类似浏览器调整大小事件的示例 所讨论的代码如下 ui.resized = function(c, t) { var curr, prev; window.addEventListener('resize', function() { curr = ui.viewport(); if ( prev && curr != prev )
ui.resized = function(c, t) {
var curr,
prev;
window.addEventListener('resize',
function() {
curr = ui.viewport();
if ( prev && curr != prev ) {
clearTimeout(t);
t = setTimeout(c, 50);
}
prev = curr || prev;
}, false);
return c;
};
对此有什么想法吗?或者有人能给我指出正确的方向吗?我在构建图表的指令中做了类似的事情。我希望在调整窗口大小时重新绘制图表,以便适当缩放。该指令如下所示:
(function () {
'use strict';
angular.module('ad.common')
.directive('zxChart', chartDirective);
/** @ngInject */
function chartDirective($window, MyService) {
function link(scope, element, attrs) {
scope.id = attrs.id || 'default-id';
scope.width = attrs.width || $window.innerWidth - ($window.innerWidth * 0.1);
attrs.$set('id', scope.id);
angular.element($window).bind('resize', function () {
scope.width = attrs.width || $window.innerWidth - ($window.innerWidth * 0.1);
MyService.whenWindowIsResized(element[0], scope.width);
});
scope.$watch('chartData', function () {
MyService.drawChart(element[0], scope.width, scope.chartData);
});
}
return {
restrict: 'E',
replace: true,
template: '<div id="" class="my-chart"></div>',
scope: {
chartData: '=',
},
link: link
};
}
(函数(){
"严格使用",;
角度模块('ad.common')
.指令(“zxChart”,chartDirective);
/**@ngInject*/
函数chartDirective($window,MyService){
功能链接(范围、元素、属性){
scope.id=attrs.id | |“默认id”;
scope.width=attrs.width | |$window.innerWidth-($window.innerWidth*0.1);
属性$set('id',scope.id);
angular.element($window.bind('resize',function(){
scope.width=attrs.width | |$window.innerWidth-($window.innerWidth*0.1);
MyService.whenWindowIsResized(元素[0],作用域.width);
});
范围.$watch('chartData',函数(){
MyService.drawChart(元素[0],scope.width,scope.chartData);
});
}
返回{
限制:'E',
替换:正确,
模板:“”,
范围:{
图表数据:'=',
},
链接:链接
};
}
})()
我的测试分两部分进行,首先我测试绑定是否正确:
describe('Check binding to event', function () {
var $scope,
$compile,
stubWindow;
beforeEach(function () {
module('ad.common', function ($provide) {
stubWindow = {
addEventListener: sinonSandbox.stub()
};
$provide.value('$window', stubWindow);
});
inject(function ($injector) {
$scope = $injector.get('$rootScope').$new();
$compile = $injector.get('$compile');
});
});
it('should have added a resize event listener to $window', function () {
var element = angular.element('<zx-chart id="foo"></zx-chart>');
$compile(element)($scope);
$scope.$digest();
stubWindow.addEventListener.should.have.been.calledOnce;
stubWindow.addEventListener.should.have.been.calledWith("resize");
});
});
description('Check binding to event',function(){
var$scope,
$compile,
短柱窗;
beforeach(函数(){
模块('ad.common',函数($provide){
存根窗口={
addEventListener:sinonSandbox.stub()
};
$provide.value('$window',stubWindow);
});
注入(函数($injector){
$scope=$injector.get(“$rootScope”)。$new();
$compile=$injector.get(“$compile”);
});
});
它('应该在$window'函数()中添加一个调整大小事件侦听器'){
变量元素=角度元素(“”);
$compile(元素)($scope);
$scope.$digest();
stubWindow.addEventListener.should.have.been.calledOnce;
stubWindow.addEventListener.property.have.been.calledWith(“resize”);
});
});
然后,我测试触发resize事件是否会导致调用我希望在resize事件触发时触发的方法:
describe('Check call to event', function () {
var $scope,
$compile,
$window,
stubMyService;
beforeEach(function () {
module('ad.common', function ($provide) {
stubMyService = {
whenWindowIsResized: sinonSandbox.stub(),
};
$provide.value('MyService', stubMyService);
});
inject(function ($injector) {
$scope = $injector.get('$rootScope').$new();
$compile = $injector.get('$compile');
$window = $injector.get('$window');
});
});
it('a resize event should trigger a call to MyService.whenWindowIsResized', function () {
var element = angular.element('<zx-chart id="foo"></zx-chart>');
$compile(element)($scope);
$scope.$digest();
stubMyService.whenWindowIsResized.should.not.have.been.called;
angular.element($window).triggerHandler('resize');
stubMyService.whenWindowIsResized.should.have.been.calledOnce;
});
description('Check call to event',function(){
var$scope,
$compile,
$window,
stubMyService;
beforeach(函数(){
模块('ad.common',功能($provide){
stubMyService={
当INDOWISIZED时:sinonSandbox.stub(),
};
$provide.value('MyService',stubMyService);
});
注入(函数($injector){
$scope=$injector.get(“$rootScope”)。$new();
$compile=$injector.get(“$compile”);
$window=$injector.get(“$window”);
});
});
它('调整大小事件应触发对MyService.whenWindowIsResized'函数()的调用){
变量元素=角度元素(“”);
$compile(元素)($scope);
$scope.$digest();
stubMyService.whenWindowIsResized.should.not.have.been.called;
angular.element($window.triggerHandler('resize');
stubMyService.whenWindowIsResized.should.have.been.calledOnce;
});
当然,您需要提取resize调用中的功能,并将其放入服务中。但在我看来,这只是使其更易于测试。我在一个生成图表的指令中做了类似的操作。我希望在调整窗口大小时重新绘制图表,以便适当缩放。该指令看起来像e这是:
(function () {
'use strict';
angular.module('ad.common')
.directive('zxChart', chartDirective);
/** @ngInject */
function chartDirective($window, MyService) {
function link(scope, element, attrs) {
scope.id = attrs.id || 'default-id';
scope.width = attrs.width || $window.innerWidth - ($window.innerWidth * 0.1);
attrs.$set('id', scope.id);
angular.element($window).bind('resize', function () {
scope.width = attrs.width || $window.innerWidth - ($window.innerWidth * 0.1);
MyService.whenWindowIsResized(element[0], scope.width);
});
scope.$watch('chartData', function () {
MyService.drawChart(element[0], scope.width, scope.chartData);
});
}
return {
restrict: 'E',
replace: true,
template: '<div id="" class="my-chart"></div>',
scope: {
chartData: '=',
},
link: link
};
}
(函数(){
"严格使用",;
角度模块('ad.common')
.指令(“zxChart”,chartDirective);
/**@ngInject*/
函数chartDirective($window,MyService){
功能链接(范围、元素、属性){
scope.id=attrs.id | |“默认id”;
scope.width=attrs.width | |$window.innerWidth-($window.innerWidth*0.1);
属性$set('id',scope.id);
angular.element($window.bind('resize',function(){
scope.width=attrs.width | |$window.innerWidth-($window.innerWidth*0.1);
MyService.whenWindowIsResized(元素[0],作用域.width);
});
范围.$watch('chartData',函数(){
MyService.drawChart(元素[0],scope.width,scope.chartData);
});
}
返回{
限制:'E',
替换:正确,
模板:“”,
范围:{
图表数据:'=',
},
链接:链接
};
}
})()
我的测试分两部分进行,首先我测试绑定是否正确:
describe('Check binding to event', function () {
var $scope,
$compile,
stubWindow;
beforeEach(function () {
module('ad.common', function ($provide) {
stubWindow = {
addEventListener: sinonSandbox.stub()
};
$provide.value('$window', stubWindow);
});
inject(function ($injector) {
$scope = $injector.get('$rootScope').$new();
$compile = $injector.get('$compile');
});
});
it('should have added a resize event listener to $window', function () {
var element = angular.element('<zx-chart id="foo"></zx-chart>');
$compile(element)($scope);
$scope.$digest();
stubWindow.addEventListener.should.have.been.calledOnce;
stubWindow.addEventListener.should.have.been.calledWith("resize");
});
});
description('Check binding to event',function(){
var$scope,
$compile,
短柱窗;
beforeach(函数(){
模块('ad.common',函数($provide){
存根窗口={
addEventListener:sinonSandbox.stub()
};
$provide.value('$window',stubWindow);
});
注入(函数($injector){
$scope=$injector.get(“$rootScope”)。$new();
$compile=$injector.get(“$compile”);
});
});
它('应该在$window'函数()中添加一个调整大小事件侦听器'){
变量元素=角度元素(“”);
$compile(元素)($scope);
$scope.$digest();
stubWindow.addEventListener.should.have.been.calledOnce;
stubWindow.addEventListener.property.have.been.calledWith(“resize”);
});
});
然后,我测试触发resize事件是否会导致调用我希望在resize事件触发时触发的方法:
describe('Check call to event', function () {
var $scope,
$compile,
$window,
stubMyService;
beforeEach(function () {
module('ad.common', function ($provide) {
stubMyService = {
whenWindowIsResized: sinonSandbox.stub(),
};
$provide.value('MyService', stubMyService);
});
inject(function ($injector) {
$scope = $injector.get('$rootScope').$new();
$compile = $injector.get('$compile');
$window = $injector.get('$window');
});
});
it('a resize event should trigger a call to MyService.whenWindowIsResized', function () {
var element = angular.element('<zx-chart id="foo"></zx-chart>');
$compile(element)($scope);
$scope.$digest();
stubMyService.whenWindowIsResized.should.not.have.been.called;
angular.element($window).triggerHandler('resize');
stubMyService.whenWindowIsResized.should.have.been.calledOnce;
});
描述