From c95927b11ea0b2c13ce4dab95118bb292a068f33 Mon Sep 17 00:00:00 2001 From: Johann-S Date: Thu, 14 May 2015 00:26:04 +0200 Subject: [PATCH] Add unit tests --- .gitignore | 1 + Gruntfile.js | 28 ++++++++++++- js/tests/.jshintrc | 5 +++ js/tests/SpecRunner.html | 69 +++++++++++++++++++++++++++++++ js/tests/commonSpec.js | 20 +++++++++ js/tests/modalsSpec.js | 52 +++++++++++++++++++++++ js/tests/sliderSpec.js | 32 ++++++++++++++ js/tests/vendor/touchfaker.min.js | 6 +++ package.json | 1 + 9 files changed, 213 insertions(+), 1 deletion(-) create mode 100755 js/tests/.jshintrc create mode 100755 js/tests/SpecRunner.html create mode 100755 js/tests/commonSpec.js create mode 100755 js/tests/modalsSpec.js create mode 100755 js/tests/sliderSpec.js create mode 100755 js/tests/vendor/touchfaker.min.js diff --git a/.gitignore b/.gitignore index 915c39a..b0ab5cc 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ docs/.jekyll-metadata # Folders to ignore node_modules bower_components +.grunt diff --git a/Gruntfile.js b/Gruntfile.js index 9e19d91..d48c017 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -29,6 +29,7 @@ module.exports = function (grunt) { docsDistPath: 'docs/dist/', docsPath: 'docs/', jsPath: 'js/', + jsTestPath: 'js/tests/', srcPath: 'sass/' }, @@ -292,6 +293,12 @@ module.exports = function (grunt) { }, docs: { src: ['<%= meta.docsAssetsPath %>/js/docs.js', '<%= meta.docsAssetsPath %>/js/fingerblast.js'] + }, + tests: { + options: { + jshintrc: 'js/tests/.jshintrc' + }, + src: 'js/tests/*.js' } }, @@ -307,6 +314,9 @@ module.exports = function (grunt) { }, docs: { src: '<%= jshint.docs.src %>' + }, + tests: { + src: '<%= jshint.tests.src %>' } }, @@ -383,6 +393,22 @@ module.exports = function (grunt) { } ] } + }, + + jasmine: { + src: '<%= jshint.src.src %>', + options: { + specs: '<%= jshint.tests.src %>', + styles: 'dist/ratchet.min.css', + display: 'short', + vendor: [ + '<%= meta.docsAssetsPath %>js/fingerblast.js', + '<%= jshint.src.src %>', + '<%= meta.jsTestPath %>vendor/touchfaker.min.js' + ], + outfile: 'js/tests/SpecRunner.html', + keepRunner: true + } } }); @@ -397,7 +423,7 @@ module.exports = function (grunt) { grunt.registerTask('validate-html', ['jekyll:docs', 'htmllint']); grunt.registerTask('build', ['dist']); grunt.registerTask('default', ['dist']); - grunt.registerTask('test', ['dist', 'csslint', 'jshint', 'jscs', 'validate-html']); + grunt.registerTask('test', ['dist', 'csslint', 'jshint', 'jscs', 'jasmine', 'validate-html']); grunt.registerTask('server', ['dist', 'jekyll:docs', 'connect', 'watch']); grunt.registerTask('prep-release', ['dist', 'jekyll:github', 'htmlmin', 'compress']); diff --git a/js/tests/.jshintrc b/js/tests/.jshintrc new file mode 100755 index 0000000..245a406 --- /dev/null +++ b/js/tests/.jshintrc @@ -0,0 +1,5 @@ +{ + "devel" : true, + "es3" : false, + "jasmine" : true +} diff --git a/js/tests/SpecRunner.html b/js/tests/SpecRunner.html new file mode 100755 index 0000000..3eea7be --- /dev/null +++ b/js/tests/SpecRunner.html @@ -0,0 +1,69 @@ + + + + + Jasmine Spec Runner + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/js/tests/commonSpec.js b/js/tests/commonSpec.js new file mode 100755 index 0000000..ccc19f1 --- /dev/null +++ b/js/tests/commonSpec.js @@ -0,0 +1,20 @@ +describe('Common', function () { + it('RATCHET namespace is defined', function () { + expect(typeof RATCHET !== 'undefined').toBe(true); + }); + + it('window.CustomEvent exists', function () { + expect(typeof window.CustomEvent !== 'undefined').toBe(true); + }); + + it('RATCHET.getBrowserCapabilities returns an object', function () { + var result = RATCHET.getBrowserCapabilities; + expect(typeof result === 'object').toBe(true); + }); + + it('RATCHET.getTransitionEnd returns string', function () { + var result = RATCHET.getTransitionEnd; + expect(typeof result === 'string').toBe(true); + expect(result.length > 0).toBe(true); + }); +}); diff --git a/js/tests/modalsSpec.js b/js/tests/modalsSpec.js new file mode 100755 index 0000000..c1c2b85 --- /dev/null +++ b/js/tests/modalsSpec.js @@ -0,0 +1,52 @@ +describe('Modals', function () { + beforeEach(function () { + var templateModal = [ + 'Open modal', + '' + ].join(''); + document.body.innerHTML += templateModal; + }); + + afterEach(function () { + var linkModal = document.getElementById('linkOpenModal'); + var modal = document.getElementById('myModal'); + linkModal.parentNode.removeChild(linkModal); + modal.parentNode.removeChild(modal); + }); + + it('Modal should fire modalOpen event', function (done) { + window.addEventListener('modalOpen', function () { + expect(true).toBe(true); + done(); + }); + var link = document.getElementById('linkOpenModal'); + var eventTouchEnd = new CustomEvent('touchend', { + bubbles: true, + cancelable: true + }); + link.dispatchEvent(eventTouchEnd); + }); + + it('Modal should fire modalClose event', function (done) { + var link = document.getElementById('linkOpenModal'); + var eventTouchEnd = new CustomEvent('touchend', { + bubbles: true, + cancelable: true + }); + window.addEventListener('modalClose', function () { + expect(true).toBe(true); + done(); + }); + link.dispatchEvent(eventTouchEnd); + var closeLink = document.getElementById('linkCloseModal'); + closeLink.dispatchEvent(eventTouchEnd); + }); +}); diff --git a/js/tests/sliderSpec.js b/js/tests/sliderSpec.js new file mode 100755 index 0000000..938678f --- /dev/null +++ b/js/tests/sliderSpec.js @@ -0,0 +1,32 @@ +describe('Slider', function () { + var slider = null; + beforeEach(function () { + var templateSlider = [ + '
', + '
', + '
', + '', + '
', + '
', + '', + '
', + '
', + '
' + ].join(''); + document.body.innerHTML += templateSlider; + slider = document.getElementById('mySlider'); + }); + + afterEach(function () { + slider.parentNode.removeChild(slider); + slider = null; + }); + + it('Slider should fire slide event', function (done) { + slider.addEventListener('slide', function () { + expect(true).toBe(true); + done(); + }); + TouchFaker.fakeEvent('touchstart', '#firstSlide'); + }); +}); diff --git a/js/tests/vendor/touchfaker.min.js b/js/tests/vendor/touchfaker.min.js new file mode 100755 index 0000000..e364a54 --- /dev/null +++ b/js/tests/vendor/touchfaker.min.js @@ -0,0 +1,6 @@ +/*! + * TouchFaker - v1.1.0 - 2015-05-21 + * https://github.com/Johann-S/TouchFaker + * Copyright (c) 2015 Johann SERVOIRE; Licensed MIT + */ +!function(){"use strict";function a(a,b,c,d,e){d=d||0,e=e||0,this.identifier=b,this.target=a,this.clientX=c.clientX+d||0,this.clientY=c.clientY+e||0,this.screenX=c.screenX+d||0,this.screenY=c.screenY+e||0,this.pageX=c.pageX+d||0,this.pageY=c.pageY+e||0}function b(){var a=[];return a.item=function(a){return this[a]||null},a.identifiedTouch=function(a){return this[a+1]||null},a}function c(){for(var a=[window,document.documentElement],b=["ontouchstart","ontouchmove","ontouchcancel","ontouchend"],c=0;c2}function e(a,b,c){var d=document.createEvent("Event");d.initEvent(a,!0,!0);var e="undefined"!=typeof c?g(c):f(b);d.touches=i(e,a,b),d.targetTouches=i(e,a,b),d.changedTouches=j(e,a,b),b.dispatchEvent(d)}function f(a){var b=a.getBoundingClientRect();return{pageX:b.left,pageY:b.top}}function g(a){var b={};return b.clientX=a.hasOwnProperty("clientX")?a.clientX:0,b.clientY=a.hasOwnProperty("clientY")?a.clientY:0,b.pageX=a.hasOwnProperty("pageX")?a.pageX:0,b.pageY=a.hasOwnProperty("pageY")?a.pageY:0,b.screenX=a.hasOwnProperty("screenX")?a.screenX:0,b.screenY=a.hasOwnProperty("screenY")?a.screenY:0,b}function h(c,d){var e=new b;return e.push(new a(d,1,c,0,0)),e}function i(a,c,d){var e=new b;return"touchend"!==c&&(e=h(a,d)),e}function j(a,b,c){var d=h(a,c);return("touchstart"===b||"touchend"===b)&&d.splice(0,1),d}document.createTouch||(document.createTouch=function(b,c,d,e,f,g,h,i,j){return(void 0===i||void 0===j)&&(i=e-window.pageXOffset,j=f-window.pageYOffset),new a(c,d,{pageX:e,pageY:f,screenX:g,screenY:h,clientX:i,clientY:j})}),document.createTouchList||(document.createTouchList=function(){for(var a=new b,c=0;c