Commit da7ee448 authored by Valentin Hervieu's avatar Valentin Hervieu

test(keyboard-controls): Handle all simple cases

Also refactor the tests architecture to have real tests interacting from outside the directive.
parent c644f7e5
...@@ -78,7 +78,7 @@ ...@@ -78,7 +78,7 @@
return factory; return factory;
}) })
.value('rzThrottle', .factory('rzThrottle', function($timeout) {
/** /**
* rzThrottle * rzThrottle
* *
...@@ -89,7 +89,7 @@ ...@@ -89,7 +89,7 @@
* @param {ThrottleOptions} options * @param {ThrottleOptions} options
* @returns {Function} * @returns {Function}
*/ */
function throttle(func, wait, options) { return function(func, wait, options) {
'use strict'; 'use strict';
var getTime = (Date.now || function() { var getTime = (Date.now || function() {
return new Date().getTime(); return new Date().getTime();
...@@ -113,16 +113,17 @@ ...@@ -113,16 +113,17 @@
context = this; context = this;
args = arguments; args = arguments;
if (remaining <= 0) { if (remaining <= 0) {
clearTimeout(timeout); $timeout.cancel(timeout);
timeout = null; timeout = null;
previous = now; previous = now;
result = func.apply(context, args); result = func.apply(context, args);
context = args = null; context = args = null;
} else if (!timeout && options.trailing !== false) { } else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining); timeout = $timeout(later, remaining);
} }
return result; return result;
}; };
}
}) })
.factory('RzSlider', function($timeout, $document, $window, $compile, RzSliderOptions, rzThrottle) { .factory('RzSlider', function($timeout, $document, $window, $compile, RzSliderOptions, rzThrottle) {
...@@ -348,7 +349,7 @@ ...@@ -348,7 +349,7 @@
}, true); }, true);
this.scope.$watch('rzSliderModel', function(newValue, oldValue) { this.scope.$watch('rzSliderModel', function(newValue, oldValue) {
if(self.internalChange) if (self.internalChange)
return; return;
if (newValue === oldValue) if (newValue === oldValue)
return; return;
...@@ -356,7 +357,7 @@ ...@@ -356,7 +357,7 @@
}); });
this.scope.$watch('rzSliderHigh', function(newValue, oldValue) { this.scope.$watch('rzSliderHigh', function(newValue, oldValue) {
if(self.internalChange) if (self.internalChange)
return; return;
if (newValue === oldValue) if (newValue === oldValue)
return; return;
...@@ -1574,7 +1575,7 @@ ...@@ -1574,7 +1575,7 @@
}, },
link: function(scope, elem) { link: function(scope, elem) {
scope.service = new RzSlider(scope, elem); //attach on scope so we can test it scope.slider = new RzSlider(scope, elem); //attach on scope so we can test it
} }
}; };
}); });
......
...@@ -5,86 +5,162 @@ describe('rzslider - ', function() { ...@@ -5,86 +5,162 @@ describe('rzslider - ', function() {
$rootScope, $rootScope,
scope, scope,
$compile, $compile,
$timeout,
element, element,
service; slider;
beforeEach(module('rzModule')); beforeEach(module('rzModule'));
beforeEach(module('appTemplates')); beforeEach(module('appTemplates'));
beforeEach(inject(function(_RzSlider_, _$rootScope_, _$compile_) { beforeEach(inject(function(_RzSlider_, _$rootScope_, _$compile_, _$timeout_) {
RzSlider = _RzSlider_; RzSlider = _RzSlider_;
$rootScope = _$rootScope_; $rootScope = _$rootScope_;
$compile = _$compile_; $compile = _$compile_;
$timeout = _$timeout_;
})); }));
describe('initialisation', function() { describe('single slider initialisation', function() {
beforeEach(function() { beforeEach(function() {
var slider = { var sliderConf = {
value: 10, value: 10,
options: { options: {
floor: 0, floor: 0,
ceil: 1000, ceil: 100,
step: 100 step: 10
} }
}; };
createSlider(slider); createSlider(sliderConf);
}); });
it('should exist compiled', function() { it('should exist compiled', function() {
expect(element.find('span')).to.have.length(11); expect(element.find('span')).to.have.length(11);
}); });
it('should round the model value to the step', function() {
scope.slider.value = 54;
scope.$digest();
expect(scope.slider.value).to.equal(50);
scope.slider.value = 55;
scope.$digest();
$timeout.flush(); //to flush the throttle function
expect(scope.slider.value).to.equal(60);
});
}); });
describe('keyboard controls', function() { describe('keyboard controls', function() {
describe('simple cases', function() {
beforeEach(function() { beforeEach(function() {
var slider = { var sliderConf = {
value: 10, value: 100,
options: { options: {
floor: 0, floor: 0,
ceil: 1000, ceil: 200
step: 100
} }
}; };
createSlider(slider); createSlider(sliderConf);
});
it('should increment by 1 when RIGHT is pressed', function() {
slider.minH.triggerHandler('focus');
pressKeydown(slider.minH, 'RIGHT');
expect(scope.slider.value).to.equal(101);
});
it('should decrement by 1 when LEFT is pressed', function() {
slider.minH.triggerHandler('focus');
pressKeydown(slider.minH, 'LEFT');
expect(scope.slider.value).to.equal(99);
});
it('should increment by 10% when PAGEUP is pressed', function() {
slider.minH.triggerHandler('focus');
pressKeydown(slider.minH, 'PAGEUP');
expect(scope.slider.value).to.equal(120);
});
it('should decrement by 10% when PAGEDOWN is pressed', function() {
slider.minH.triggerHandler('focus');
pressKeydown(slider.minH, 'PAGEDOWN');
expect(scope.slider.value).to.equal(80);
});
it('should set value to min when HOME is pressed', function() {
slider.minH.triggerHandler('focus');
pressKeydown(slider.minH, 'HOME');
expect(scope.slider.value).to.equal(0);
});
it('set value to max when END is pressed', function() {
slider.minH.triggerHandler('focus');
pressKeydown(slider.minH, 'END');
expect(scope.slider.value).to.equal(200);
});
}); });
it('should trigger a left arrow respecting step values and not go below 0', function() { it('should not go below 0', function() {
var event = pressLeftArrow(); var sliderConf = {
service.onPointerFocus(element, 'rzSliderModel', event); value: 10,
service.onKeyboardEvent(event); options: {
floor: 0,
ceil: 1000,
step: 10
}
};
createSlider(sliderConf);
slider.minH.triggerHandler('focus');
pressKeydown(slider.minH, 'PAGEDOWN');
expect(scope.slider.value).to.equal(0); expect(scope.slider.value).to.equal(0);
}); });
function pressLeftArrow() { function pressKeydown(element, key, oldAPI) {
var evt = document.createEvent('CustomEvent'); // MUST be 'CustomEvent' key = key.toUpperCase();
evt.initCustomEvent('keydown', false, false, null); var event = {
evt.which = 37; type: 'keydown'
return evt; };
var keys = {
'UP': 38,
'DOWN': 40,
'LEFT': 37,
'RIGHT': 39,
'PAGEUP': 33,
'PAGEDOWN': 34,
'HOME': 36,
'END': 35
};
var keyCode = keys[key];
if (oldAPI)
eent.which = keyCode;
else event.keyCode = keyCode;
element.triggerHandler(event);
} }
}); });
function createSlider(sliderObj) { function createSlider(sliderObj) {
scope = $rootScope.$new();
scope.slider = sliderObj;
var template = ''; var template = '';
if (sliderObj.options) if (sliderObj.options)
template = '<rzslider rz-slider-model="slider.value" rz-slider-options="slider.options"></rzslider>'; template = '<rzslider rz-slider-model="slider.value" rz-slider-options="slider.options"></rzslider>';
else else
template = '<rzslider rz-slider-model="slider.value"></rzslider>'; template = '<rzslider rz-slider-model="slider.value"></rzslider>';
element = $compile(template)(scope); initSlider(sliderObj, template);
scope.$apply();
service = element.isolateScope().service;
} }
function createRangeSlider(sliderObj) { function createRangeSlider(sliderObj) {
scope = $rootScope.$new();
scope.slider = sliderObj;
var template = ''; var template = '';
if (sliderObj.options) if (sliderObj.options)
template = '<rzslider rz-slider-model="slider.min" rz-slider-high="slider.max"' + template = '<rzslider rz-slider-model="slider.min" rz-slider-high="slider.max"' +
'rz-slider-options="slider.options"></rzslider>'; 'rz-slider-options="slider.options"></rzslider>';
else else
template = '<rzslider rz-slider-model="slider.value" rz-slider-high="slider.max"></rzslider>'; template = '<rzslider rz-slider-model="slider.value" rz-slider-high="slider.max"></rzslider>';
initSlider(sliderObj, template);
}
function initSlider(sliderObj, template) {
scope = $rootScope.$new();
scope.slider = sliderObj;
element = $compile(template)(scope); element = $compile(template)(scope);
scope.$apply(); scope.$apply();
service = element.isolateScope().service; slider = element.isolateScope().slider;
$timeout.flush();
} }
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment