Permalink
Browse files

Added some more unit tests

  • Loading branch information...
1 parent 9139412 commit 72011f4170295f3ccf60ebb9db2caeb960bc8601 @wieringen committed Jan 23, 2015
Showing with 94 additions and 29 deletions.
  1. +2 −0 Gruntfile.coffee
  2. +48 −26 lib/jquery.tinycircleslider.js
  3. +2 −2 lib/jquery.tinycircleslider.min.js
  4. +42 −1 test/unit/jquery.tinycircleslider.spec.js
View
@@ -40,6 +40,8 @@ module.exports = ( grunt ) ->
configFile: "test/conf/karma-dev.js"
ci:
configFile: "test/conf/karma-ci.js"
+ debug:
+ configFile: "test/conf/karma-debug.js"
# Remove old build.
@@ -1,4 +1,4 @@
-;(function (factory) {
+;(function(factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
}
@@ -9,15 +9,15 @@
factory(jQuery);
}
}
-(function ($) {
+(function($) {
var pluginName = "tinycircleslider"
, defaults = {
- interval : false
- , intervalTime : 3500
- , dots : true
- , dotsSnap : false
- , dotsHide : true
- , radius : 140
+ interval : false
+ , intervalTime : 3500
+ , dots : true
+ , dotsSnap : false
+ , dotsHide : true
+ , radius : 140
}
;
@@ -56,8 +56,6 @@
, $dots = $container.find(".dot")
, $links = $slides.find("a")
- , dots = []
-
, containerSize = {
width: $container.outerWidth(true)
, height: $container.outerHeight(true)
@@ -83,6 +81,15 @@
;
/**
+ * When dotsSnap is enabled every slide has a corresponding dot.
+ *
+ * @property dots
+ * @type Array
+ * @default []
+ */
+ this.dots = [];
+
+ /**
* The index of the current slide.
*
* @property slideCurrent
@@ -101,6 +108,15 @@
this.angleCurrent = 10;
/**
+ * The number of slides the slider is currently aware of.
+ *
+ * @property slidesTotal
+ * @type Number
+ * @default 0
+ */
+ this.slidesTotal = $slides.length;
+
+ /**
* If the interval is running the value will be true.
*
* @property intervalActive
@@ -213,7 +229,7 @@
.addClass($(slide).attr("data-classname"))
.css(position);
- dots.push({
+ self.dots.push({
"angle": angle
, "slide": slide
, "dot": $dotClone
@@ -222,11 +238,11 @@
docFragment.appendChild($dotClone[0]);
});
- dots.sort(function(dotA, dotB) {
+ self.dots.sort(function(dotA, dotB) {
return dotA.angle - dotB.angle;
});
- $.each(dots, function(index, dot) {
+ $.each(self.dots, function(index, dot) {
$(dot.dot)
.addClass("dot-" + (index + 1))
.html("<span>" + (index + 1) + "</span>");
@@ -298,15 +314,15 @@
*/
function _findClosestSlide(angle) {
var closestDotAngleToAngleCCW = 9999
- , closestDotAngleToAngleCW = 9999
- , closestDotAngleToAngle = 9999
- , closestSlideCCW = 0
- , closestSlideCW = 0
- , closestSlide = 0
+ , closestDotAngleToAngleCW = 9999
+ , closestDotAngleToAngle = 9999
+ , closestSlideCCW = 0
+ , closestSlideCW = 0
+ , closestSlide = 0
;
- $.each(dots, function(index, dot) {
- var delta = findShortestPath(dot.angle, angle);
+ $.each(self.dots, function(index, dot) {
+ var delta = _findShortestPath(dot.angle, angle);
if(Math.abs(delta[0]) < Math.abs(closestDotAngleToAngle)) {
closestDotAngleToAngle = delta[0];
@@ -335,14 +351,20 @@
*
* @method move
* @chainable
- * @param {Number} [slideIndex]
+ * @param {Number} [index]
* @param {Boolean} [interval]
*/
- this.move = function(slideIndex, interval) {
- var angleDestination = dots[slideIndex] && dots[slideIndex].angle || 0
- , angleDelta = _findShortestPath(angleDestination, self.angleCurrent)[0]
- , framerate = Math.ceil(Math.abs(angleDelta) / 10)
- , angleStep = (angleDelta / framerate) || 0
+ this.move = function(index, interval) {
+ var slideIndex = Math.max(0, index || self.slideCurrent);
+
+ if(slideIndex >= self.slidesTotal) {
+ slideIndex = 0;
+ }
+
+ var angleDestination = self.dots[slideIndex] && self.dots[slideIndex].angle || 0
+ , angleDelta = _findShortestPath(angleDestination, self.angleCurrent)[0]
+ , framerate = Math.ceil(Math.abs(angleDelta) / 10)
+ , angleStep = (angleDelta / framerate)
;
self.slideCurrent = slideIndex;
@@ -1,7 +1,7 @@
-/*! Tiny Circleslider - v2.0.6 - 2015-01-20
+/*! Tiny Circleslider - v2.0.6 - 2015-01-23
* http://www.baijs.com/tinycircleslider
*
* Copyright (c) 2015 Maarten Baijs <wieringen@gmail.com>;
* Licensed under the MIT license */
-!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof exports?module.exports=a(require("jquery")):a(jQuery)}(function(a){function b(b,e){function f(){return u.options.dotsSnap&&k(),v.append(w.first().clone()).css("width",B.width*(w.length+1)),g(),u.move(0,u.options.interval),u}function g(){var c=H?"touchstart":"mousedown";H?(b[0].ontouchstart=t,b[0].ontouchmove=q,b[0].ontouchend=s):x.bind(c,t),u.options.dotsSnap&&b.delegate(".dot",c,function(b){return b.preventDefault(),b.stopImmediatePropagation(),clearTimeout(E),0===G&&u.move(a(this).text()-1),u.start(),!1})}function h(a){E=setTimeout(function(){u.move(void 0!==w[u.slideCurrent+1]?u.slideCurrent+1:0,!0)},a?50:u.options.intervalTime)}function i(a){return a*(Math.PI/180)}function j(a){return 180*a/Math.PI}function k(){var c=document.createDocumentFragment();y.remove(),w=w.remove(),w.each(function(b,d){var e=y.clone(),f=u.options.dots?360*b/w.length:parseInt(a(d).attr("data-degrees"),10),g={top:-Math.cos(i(f))*u.options.radius+A.height/2-D.height/2,left:Math.sin(i(f))*u.options.radius+A.width/2-D.width/2};e.addClass(a(d).attr("data-classname")).css(g),z.push({angle:f,slide:d,dot:e}),c.appendChild(e[0])}),z.sort(function(a,b){return a.angle-b.angle}),a.each(z,function(b,c){a(c.dot).addClass("dot-"+(b+1)).html("<span>"+(b+1)+"</span>"),v.append(c.slide)}),b.append(c),y=b.find(".dot")}function l(a,b){var c,d,e;return a>b?(c=a-b,d=-(b+360-a)):(c=a+360-b,d=-(b-a)),e=c<Math.abs(d)?c:d,[e,d,c]}function m(b){var c=9999,d=9999,e=9999,f=0,g=0,h=0;return a.each(z,function(a,i){var j=findShortestPath(i.angle,b);Math.abs(j[0])<Math.abs(e)&&(e=j[0],h=a),Math.abs(j[1])<Math.abs(c)&&(c=j[1],f=a),Math.abs(j[2])<Math.abs(d)&&(d=j[2],g=a)}),[[h,f,g],[e,c,d]]}function n(a){return 0>a?360+a%-360:a%360}function o(a,b,c,d){G+=1;var e=n(Math.round(G*a+u.angleCurrent));G===c&&d&&u.start(),r(e,G===c),c>G?F=setTimeout(function(){o(a,b,c,d)},50):(G=0,u.angleCurrent=b)}function p(a){return{x:I?a.targetTouches[0].pageX:a.pageX||a.clientX,y:I?a.targetTouches[0].pageY:a.pageY||a.clientY}}function q(a){var c=b.offset(),d={left:p(a).x-c.left-A.width/2,top:p(a).y-c.top-A.height/2};return u.angleCurrent=n(j(Math.atan2(d.left,-d.top))),r(u.angleCurrent),!1}function r(a,c){u.options.dots?v.css("left",-(a/360*B.width*w.length)):(closestSlidesAndAngles=m(a),closestSlides=_closestSlidesAndAngles[0],closestAngles=_closestSlidesAndAngles[1],v.css("left",-(closestSlides[1]*B.width+Math.abs(closestAngles[1])*B.width/(Math.abs(closestAngles[1])+Math.abs(closestAngles[2]))))),x.css({top:-Math.cos(i(a))*u.options.radius+(A.height/2-C.height/2),left:Math.sin(i(a))*u.options.radius+(A.width/2-C.width/2)}),c&&b.trigger("move",[w[u.slideCurrent],u.slideCurrent])}function s(b){return a(b.target).hasClass("dot")?!1:(b.preventDefault(),clearTimeout(F),H||(a(document).unbind("mousemove mouseup"),x.unbind("mouseup")),u.options.dotsSnap&&(u.options.dotsHide&&y.stop(!0,!0).fadeOut("slow"),u.move(m(u.angleCurrent)[0][0])),u.start(),void 0)}function t(b){return b.preventDefault(),I="touchstart"==b.type,a(b.target).hasClass("dot")?!1:(clearTimeout(E),H||(a(document).mousemove(q),a(document).mouseup(s),x.mouseup(s)),u.options.dotsSnap&&u.options.dotsHide&&y.stop(!0,!0).fadeIn("slow"),void 0)}this.options=a.extend({},d,e),this._defaults=d,this._name=c;var u=this,v=(b.find(".viewport"),b.find(".overview")),w=v.children(),x=b.find(".thumb"),y=b.find(".dot"),z=(w.find("a"),[]),A={width:b.outerWidth(!0),height:b.outerHeight(!0)},B={width:w.first().outerWidth(!0),height:w.first().outerHeight(!0)},C={width:x.outerWidth(!0),height:x.outerHeight(!0)},D={width:y.outerWidth(),height:y.outerHeight()},E=null,F=null,G=0,H="ontouchstart"in document.documentElement,I=!1;return this.slideCurrent=0,this.angleCurrent=10,this.intervalActive=!1,this.start=function(a){return u.options.interval&&(u.intervalActive=!0,h(a)),u},this.stop=function(){return u.intervalActive=!1,clearTimeout(E),u},this.move=function(a,b){var c=z[a]&&z[a].angle||0,d=l(c,u.angleCurrent)[0],e=Math.ceil(Math.abs(d)/10),f=d/e||0;return u.slideCurrent=a,o(f,c,e,b),u},f()}var c="tinycircleslider",d={interval:!1,intervalTime:3500,dots:!0,dotsSnap:!1,dotsHide:!0,radius:140};a.fn[c]=function(d){return this.each(function(){a.data(this,"plugin_"+c)||a.data(this,"plugin_"+c,new b(a(this),d))})}});
+!function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof exports?module.exports=a(require("jquery")):a(jQuery)}(function(a){function b(b,e){function f(){return u.options.dotsSnap&&k(),v.append(w.first().clone()).css("width",A.width*(w.length+1)),g(),u.move(0,u.options.interval),u}function g(){var c=G?"touchstart":"mousedown";G?(b[0].ontouchstart=t,b[0].ontouchmove=q,b[0].ontouchend=s):x.bind(c,t),u.options.dotsSnap&&b.delegate(".dot",c,function(b){return b.preventDefault(),b.stopImmediatePropagation(),clearTimeout(D),0===F&&u.move(a(this).text()-1),u.start(),!1})}function h(a){D=setTimeout(function(){u.move(void 0!==w[u.slideCurrent+1]?u.slideCurrent+1:0,!0)},a?50:u.options.intervalTime)}function i(a){return a*(Math.PI/180)}function j(a){return 180*a/Math.PI}function k(){var c=document.createDocumentFragment();y.remove(),w=w.remove(),w.each(function(b,d){var e=y.clone(),f=u.options.dots?360*b/w.length:parseInt(a(d).attr("data-degrees"),10),g={top:-Math.cos(i(f))*u.options.radius+z.height/2-C.height/2,left:Math.sin(i(f))*u.options.radius+z.width/2-C.width/2};e.addClass(a(d).attr("data-classname")).css(g),u.dots.push({angle:f,slide:d,dot:e}),c.appendChild(e[0])}),u.dots.sort(function(a,b){return a.angle-b.angle}),a.each(u.dots,function(b,c){a(c.dot).addClass("dot-"+(b+1)).html("<span>"+(b+1)+"</span>"),v.append(c.slide)}),b.append(c),y=b.find(".dot")}function l(a,b){var c,d,e;return a>b?(c=a-b,d=-(b+360-a)):(c=a+360-b,d=-(b-a)),e=c<Math.abs(d)?c:d,[e,d,c]}function m(b){var c=9999,d=9999,e=9999,f=0,g=0,h=0;return a.each(u.dots,function(a,i){var j=l(i.angle,b);Math.abs(j[0])<Math.abs(e)&&(e=j[0],h=a),Math.abs(j[1])<Math.abs(c)&&(c=j[1],f=a),Math.abs(j[2])<Math.abs(d)&&(d=j[2],g=a)}),[[h,f,g],[e,c,d]]}function n(a){return 0>a?360+a%-360:a%360}function o(a,b,c,d){F+=1;var e=n(Math.round(F*a+u.angleCurrent));F===c&&d&&u.start(),r(e,F===c),c>F?E=setTimeout(function(){o(a,b,c,d)},50):(F=0,u.angleCurrent=b)}function p(a){return{x:H?a.targetTouches[0].pageX:a.pageX||a.clientX,y:H?a.targetTouches[0].pageY:a.pageY||a.clientY}}function q(a){var c=b.offset(),d={left:p(a).x-c.left-z.width/2,top:p(a).y-c.top-z.height/2};return u.angleCurrent=n(j(Math.atan2(d.left,-d.top))),r(u.angleCurrent),!1}function r(a,c){u.options.dots?v.css("left",-(a/360*A.width*w.length)):(closestSlidesAndAngles=m(a),closestSlides=_closestSlidesAndAngles[0],closestAngles=_closestSlidesAndAngles[1],v.css("left",-(closestSlides[1]*A.width+Math.abs(closestAngles[1])*A.width/(Math.abs(closestAngles[1])+Math.abs(closestAngles[2]))))),x.css({top:-Math.cos(i(a))*u.options.radius+(z.height/2-B.height/2),left:Math.sin(i(a))*u.options.radius+(z.width/2-B.width/2)}),c&&b.trigger("move",[w[u.slideCurrent],u.slideCurrent])}function s(b){return a(b.target).hasClass("dot")?!1:(b.preventDefault(),clearTimeout(E),G||(a(document).unbind("mousemove mouseup"),x.unbind("mouseup")),u.options.dotsSnap&&(u.options.dotsHide&&y.stop(!0,!0).fadeOut("slow"),u.move(m(u.angleCurrent)[0][0])),u.start(),void 0)}function t(b){return b.preventDefault(),H="touchstart"==b.type,a(b.target).hasClass("dot")?!1:(clearTimeout(D),G||(a(document).mousemove(q),a(document).mouseup(s),x.mouseup(s)),u.options.dotsSnap&&u.options.dotsHide&&y.stop(!0,!0).fadeIn("slow"),void 0)}this.options=a.extend({},d,e),this._defaults=d,this._name=c;var u=this,v=(b.find(".viewport"),b.find(".overview")),w=v.children(),x=b.find(".thumb"),y=b.find(".dot"),z=(w.find("a"),{width:b.outerWidth(!0),height:b.outerHeight(!0)}),A={width:w.first().outerWidth(!0),height:w.first().outerHeight(!0)},B={width:x.outerWidth(!0),height:x.outerHeight(!0)},C={width:y.outerWidth(),height:y.outerHeight()},D=null,E=null,F=0,G="ontouchstart"in document.documentElement,H=!1;return this.dots=[],this.slideCurrent=0,this.angleCurrent=10,this.slidesTotal=w.length,this.intervalActive=!1,this.start=function(a){return u.options.interval&&(u.intervalActive=!0,h(a)),u},this.stop=function(){return u.intervalActive=!1,clearTimeout(D),u},this.move=function(a,b){var c=Math.max(0,a||u.slideCurrent);c>=u.slidesTotal&&(c=0);var d=u.dots[c]&&u.dots[c].angle||0,e=l(d,u.angleCurrent)[0],f=Math.ceil(Math.abs(e)/10),g=e/f;return u.slideCurrent=c,o(g,d,f,b),u},f()}var c="tinycircleslider",d={interval:!1,intervalTime:3500,dots:!0,dotsSnap:!1,dotsHide:!0,radius:140};a.fn[c]=function(d){return this.each(function(){a.data(this,"plugin_"+c)||a.data(this,"plugin_"+c,new b(a(this),d))})}});
@@ -8,7 +8,7 @@ describe('A single Tinycircleslider', function() {
});
afterEach(function () {
- document.body.innerHTML = '';
+ //document.body.innerHTML = '';
});
it('should have a chainable constructor', function() {
@@ -55,4 +55,45 @@ describe('A single Tinycircleslider', function() {
expect(instance.start().intervalActive).to.equal(true);
});
+
+ it('should have a chainable move method', function() {
+ var instance = $('#rotatescroll').tinycircleslider().data('plugin_tinycircleslider');
+
+ expect(instance.move()._name).to.equal('tinycircleslider');
+ });
+
+ it('should move to first slide if given index is less then 1', function() {
+ var instance = $('#rotatescroll').tinycircleslider().data('plugin_tinycircleslider');
+
+ instance.move(-1);
+
+ expect(instance.slideCurrent).to.equal(0);
+ });
+
+ it('should move to the first slide if given index is more then slidesTotal', function() {
+ var instance = $('#rotatescroll').tinycircleslider().data('plugin_tinycircleslider');
+
+ instance.move(100);
+
+ expect(instance.slideCurrent).to.equal(0);
+ });
+
+ it('should stay in place if move method is called without arguments', function() {
+ var instance = $('#rotatescroll').tinycircleslider().data('plugin_tinycircleslider');
+
+ instance.move();
+
+ expect(instance.slideCurrent).to.equal(0);
+ });
+
+ it('should set the angle of every dot when dotSnap is enabled', function() {
+ var instance = $('#rotatescroll').tinycircleslider({ dotsSnap: true }).data('plugin_tinycircleslider')
+ , angle = 360 / instance.slidesTotal
+ ;
+
+ $.each(instance.dots, function(index, dot){
+ expect(dot.angle).to.equal(angle * index);
+ });
+ });
+
});

0 comments on commit 72011f4

Please sign in to comment.