Skip to content

Commit 069c77a

Browse files
author
Valentin Hervieu
committed
test(helper functions): Add tests for helper functions
1 parent 52c12dc commit 069c77a

File tree

2 files changed

+203
-4
lines changed

2 files changed

+203
-4
lines changed

src/rzslider.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,6 @@
465465
break;
466466
case 1:
467467
this.selBar = jElem;
468-
this.selBarChild = this.selBar.children('rz-selection');
469468
break;
470469
case 2:
471470
this.minH = jElem;
@@ -992,7 +991,8 @@
992991
* @returns {number}
993992
*/
994993
roundStep: function(value) {
995-
var steppedValue = Math.round(value / this.step) * this.step;
994+
var steppedValue = parseFloat(value / this.step).toPrecision(12)
995+
steppedValue = Math.round(steppedValue) * this.step;
996996
steppedValue = steppedValue.toFixed(this.precision);
997997
return +steppedValue;
998998
},

tests/spec/rz-slider-service-test.js

Lines changed: 201 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,51 @@ describe('rzslider - ', function() {
397397
expect(slider.maxValue).to.equal(100);
398398
});
399399

400+
it('should set the correct background-color on selection bar for single slider', function() {
401+
var sliderConf = {
402+
value: 10,
403+
options: {
404+
floor: 0,
405+
ceil: 10,
406+
showSelectionBar: true,
407+
getSelectionBarColor: function(v) {
408+
if (v < 5) return 'red';
409+
return 'green';
410+
}
411+
}
412+
};
413+
createSlider(sliderConf);
414+
var selBarChild = angular.element(slider.selBar[0].querySelector('.rz-selection'));
415+
expect(selBarChild.css('background-color')).to.equal('green');
416+
417+
scope.slider.value = 2;
418+
scope.$digest();
419+
expect(selBarChild.css('background-color')).to.equal('red');
420+
});
421+
422+
it('should set the correct background-color on selection bar for range slider', function() {
423+
var sliderConf = {
424+
min: 2,
425+
max: 8,
426+
options: {
427+
floor: 0,
428+
ceil: 10,
429+
getSelectionBarColor: function(min, max) {
430+
if (max - min < 5) return 'red';
431+
return 'green';
432+
}
433+
}
434+
};
435+
createRangeSlider(sliderConf);
436+
var selBarChild = angular.element(slider.selBar[0].querySelector('.rz-selection'));
437+
expect(selBarChild.css('background-color')).to.equal('green');
438+
439+
scope.slider.min = 4;
440+
scope.slider.max = 6;
441+
scope.$digest();
442+
expect(selBarChild.css('background-color')).to.equal('red');
443+
});
444+
400445
it('should call the correct callback for onStart', function() {
401446
var sliderConf = {
402447
value: 10,
@@ -481,10 +526,9 @@ describe('rzslider - ', function() {
481526
});
482527
});
483528

484-
485529
/*
486530
******************************************************************************
487-
Slider with ticks
531+
Accessibility
488532
******************************************************************************
489533
*/
490534
describe('accessibility - ', function() {
@@ -1311,4 +1355,159 @@ describe('rzslider - ', function() {
13111355
element.triggerHandler(event);
13121356
}
13131357
});
1358+
1359+
/*
1360+
******************************************************************************
1361+
HELPER FUNCTIONS
1362+
******************************************************************************
1363+
*/
1364+
describe('helper functions - ', function() {
1365+
beforeEach(function() {
1366+
var sliderConf = {
1367+
value: 50,
1368+
options: {
1369+
floor: 0,
1370+
ceil: 100,
1371+
step: 10
1372+
}
1373+
};
1374+
createSlider(sliderConf);
1375+
});
1376+
1377+
it('should have a valid roundStep for integer values', function() {
1378+
expect(slider.roundStep(10)).to.equal(10);
1379+
expect(slider.roundStep(9)).to.equal(10);
1380+
expect(slider.roundStep(11)).to.equal(10);
1381+
expect(slider.roundStep(15)).to.equal(20);
1382+
expect(slider.roundStep(14)).to.equal(10);
1383+
expect(slider.roundStep(-10)).to.equal(-10);
1384+
expect(slider.roundStep(-9)).to.equal(-10);
1385+
expect(slider.roundStep(-11)).to.equal(-10);
1386+
expect(slider.roundStep(-16)).to.equal(-20);
1387+
expect(slider.roundStep(-15)).to.equal(-10);
1388+
expect(slider.roundStep(-14)).to.equal(-10);
1389+
});
1390+
1391+
it('should have a valid roundStep for floating values', function() {
1392+
scope.slider.options.precision = 1;
1393+
scope.slider.options.step = 0.1;
1394+
scope.$digest();
1395+
1396+
expect(slider.roundStep(10)).to.equal(10);
1397+
expect(slider.roundStep(1.1)).to.equal(1.1);
1398+
expect(slider.roundStep(1.09)).to.equal(1.1);
1399+
expect(slider.roundStep(1.11)).to.equal(1.1);
1400+
expect(slider.roundStep(1.15)).to.equal(1.2);
1401+
expect(slider.roundStep(1.14)).to.equal(1.1);
1402+
1403+
expect(slider.roundStep(-10)).to.equal(-10);
1404+
expect(slider.roundStep(-1.1)).to.equal(-1.1);
1405+
expect(slider.roundStep(-1.09)).to.equal(-1.1);
1406+
expect(slider.roundStep(-1.11)).to.equal(-1.1);
1407+
expect(slider.roundStep(-1.16)).to.equal(-1.2);
1408+
expect(slider.roundStep(-1.15)).to.equal(-1.1);
1409+
expect(slider.roundStep(-1.14)).to.equal(-1.1);
1410+
});
1411+
1412+
it('should have a valid hideEl', function() {
1413+
var el = angular.element('<div></div>');
1414+
slider.hideEl(el);
1415+
expect(el.css('opacity')).to.equal('0');
1416+
});
1417+
1418+
it('should have a valid showEl when not rzAlwaysHide', function() {
1419+
var el = angular.element('<div></div>');
1420+
slider.showEl(el);
1421+
expect(el.css('opacity')).to.equal('1');
1422+
});
1423+
1424+
it('should have a valid showEl when rzAlwaysHide', function() {
1425+
var el = angular.element('<div></div>');
1426+
el.css('opacity', 0);
1427+
el.rzAlwaysHide = true;
1428+
1429+
slider.showEl(el);
1430+
expect(el.css('opacity')).to.equal('0');
1431+
});
1432+
1433+
it('should have a valid setPosition for horizontal sliders', function() {
1434+
var el = angular.element('<div></div>');
1435+
slider.setPosition(el, 12);
1436+
expect(el.css('left')).to.equal('12px');
1437+
});
1438+
1439+
it('should have a valid setPosition for vertical sliders', function() {
1440+
scope.slider.options.vertical = true;
1441+
scope.$digest();
1442+
var el = angular.element('<div></div>');
1443+
slider.setPosition(el, 12);
1444+
expect(el.css('bottom')).to.equal('12px');
1445+
});
1446+
1447+
it('should have a valid getDimension for horizontal sliders', function() {
1448+
expect(slider.getDimension(slider.sliderElem)).to.equal(1000);
1449+
});
1450+
1451+
it('should have a valid getDimension for horizontal sliders with custom scale', function() {
1452+
scope.slider.options.scale = 2;
1453+
scope.$digest();
1454+
expect(slider.getDimension(slider.sliderElem)).to.equal(2000);
1455+
});
1456+
1457+
it('should have a valid getDimension for vertical sliders', function() {
1458+
scope.slider.options.vertical = true;
1459+
scope.$digest();
1460+
expect(slider.getDimension(slider.sliderElem)).to.equal(1000);
1461+
});
1462+
1463+
it('should have a valid getDimension for vertical sliders with custom scale', function() {
1464+
scope.slider.options.scale = 2;
1465+
scope.slider.options.vertical = true;
1466+
scope.$digest();
1467+
expect(slider.getDimension(slider.sliderElem)).to.equal(2000);
1468+
});
1469+
1470+
it('should have a valid setDimension for horizontal sliders', function() {
1471+
var el = angular.element('<div></div>');
1472+
slider.setDimension(el, 12);
1473+
expect(el.css('width')).to.equal('12px');
1474+
});
1475+
1476+
it('should have a valid setDimension for vertical sliders', function() {
1477+
scope.slider.options.vertical = true;
1478+
scope.$digest();
1479+
var el = angular.element('<div></div>');
1480+
slider.setDimension(el, 12);
1481+
expect(el.css('height')).to.equal('12px');
1482+
});
1483+
1484+
it('should have a valid valueToOffset for positive sliders', function() {
1485+
slider.maxPos = 1000;
1486+
expect(slider.valueToOffset(0)).to.equal(0);
1487+
expect(slider.valueToOffset(50)).to.equal(500);
1488+
expect(slider.valueToOffset(100)).to.equal(1000);
1489+
});
1490+
1491+
it('should have a valid valueToOffset for negative sliders', function() {
1492+
scope.slider.options.floor = -100;
1493+
scope.slider.options.ceil = 0;
1494+
scope.slider.value = -50;
1495+
scope.$digest();
1496+
1497+
slider.maxPos = 1000;
1498+
expect(slider.valueToOffset(0)).to.equal(1000);
1499+
expect(slider.valueToOffset(-50)).to.equal(500);
1500+
expect(slider.valueToOffset(-100)).to.equal(0);
1501+
});
1502+
1503+
it('should have a valid sanitizeValue', function() {
1504+
expect(slider.sanitizeValue(0)).to.equal(0);
1505+
expect(slider.sanitizeValue(50)).to.equal(50);
1506+
expect(slider.sanitizeValue(100)).to.equal(100);
1507+
expect(slider.sanitizeValue(-1)).to.equal(0);
1508+
expect(slider.sanitizeValue(-10)).to.equal(0);
1509+
expect(slider.sanitizeValue(101)).to.equal(100);
1510+
expect(slider.sanitizeValue(110)).to.equal(100);
1511+
});
1512+
});
13141513
});

0 commit comments

Comments
 (0)