From 28566243469fad7a1b8a01f2867dbc0235e2d6d4 Mon Sep 17 00:00:00 2001 From: epicleptico Date: Tue, 3 Mar 2015 11:52:07 +0100 Subject: [PATCH 1/2] Set up test environment and added tests --- .gitignore | 1 + Gruntfile.js | 30 +- bower.json | 6 +- karma.conf.js | 22 ++ package.json | 11 +- src/js/plupload-angular-directive.js | 3 +- tests/js/plupload-angular-directive.spec.js | 406 ++++++++++++++++++++ tests/mocks/plUploader.mock.js | 65 ++++ 8 files changed, 538 insertions(+), 6 deletions(-) create mode 100644 karma.conf.js create mode 100644 tests/js/plupload-angular-directive.spec.js create mode 100644 tests/mocks/plUploader.mock.js diff --git a/.gitignore b/.gitignore index 3c3629e..a088b6f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +bower_components diff --git a/Gruntfile.js b/Gruntfile.js index 8ba670e..efa3870 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -22,7 +22,7 @@ module.exports = function(grunt) { } }, jshint: { - files: ['Gruntfile.js', 'src/**/*.js', 'src/**/**/*.js'], + files: ['Gruntfile.js', 'src/js/**/*.js'], options: { // options here to override JSHint defaults globals: { @@ -40,6 +40,28 @@ module.exports = function(grunt) { {expand: false, src: ['src/lib/plupload/plupload.silverlight.xap'], dest: 'dist/plupload.silverlight.xap'}, ] } + }, + karma: { + once: { + configFile: 'karma.conf.js', + singleRun: true, + browsers: [ + //'Chrome', + //'Firefox', + 'PhantomJS' + ] + }, + watch: { + configFile: 'karma.conf.js', + background: false, + autoWatch: true, + singleRun: false, + browsers: [ + //'Chrome', + //'Firefox', + 'PhantomJS' + ] + } } }); @@ -47,9 +69,11 @@ module.exports = function(grunt) { grunt.loadNpmTasks('grunt-contrib-jshint'); grunt.loadNpmTasks('grunt-contrib-concat'); grunt.loadNpmTasks('grunt-contrib-copy'); + grunt.loadNpmTasks('grunt-karma'); + - grunt.registerTask('test', ['jshint']); + grunt.registerTask('test', ['jshint', 'karma:once']); - grunt.registerTask('build', ['jshint', 'concat', 'uglify','copy']); + grunt.registerTask('build', ['jshint', 'karma:once', 'concat', 'uglify','copy']); }; \ No newline at end of file diff --git a/bower.json b/bower.json index 88a5af1..029c015 100644 --- a/bower.json +++ b/bower.json @@ -24,5 +24,9 @@ "bower_components", "test", "tests" - ] + ], + "dependencies": { + "angular": "~1.3.12", + "angular-mocks" : "~1.3.12" + } } diff --git a/karma.conf.js b/karma.conf.js new file mode 100644 index 0000000..3e57d9a --- /dev/null +++ b/karma.conf.js @@ -0,0 +1,22 @@ +'use strict'; + +module.exports = function(config) { + config.set({ + basePath: '', + frameworks: ['jasmine'], + logLevel: config.LOG_INFO, + colors: true, + runnerPort: 9100, + port: 9018, + reporters: ['progress'], + preprocessors: {}, + files: [ + 'bower_components/angular/angular.js', + 'bower_components/angular-mocks/angular-mocks.js', + 'tests/mocks/plUploader.mock.js', + 'src/lib/plupload/plupload.full.js', + 'src/js/plupload-angular-directive.js', + 'tests/js/plupload-angular-directive.spec.js' + ] + }); +} \ No newline at end of file diff --git a/package.json b/package.json index a3ba193..05ba832 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,20 @@ ], "dependencies": {}, "devDependencies": { + "coveralls": "^2.11.2", "grunt": "^0.4.5", "grunt-contrib-concat": "^0.4.0", "grunt-contrib-copy": "^0.5.0", "grunt-contrib-jshint": "^0.10.0", - "grunt-contrib-uglify": "^0.4.0" + "grunt-contrib-uglify": "^0.4.0", + "grunt-karma": "^0.9.0", + "jasmine-core": "^2.1.2", + "karma": "^0.12.26", + "karma-coverage": "^0.2.6", + "karma-chrome-launcher": "^0.1.5", + "karma-firefox-launcher": "^0.1.3", + "karma-jasmine": "^0.3.1", + "karma-phantomjs-launcher": "^0.1.4" }, "engine": "node >= 0.4.1" } diff --git a/src/js/plupload-angular-directive.js b/src/js/plupload-angular-directive.js index 5d2e80b..b88eedb 100755 --- a/src/js/plupload-angular-directive.js +++ b/src/js/plupload-angular-directive.js @@ -1,6 +1,7 @@ 'use strict'; angular.module('plupload.directive', []) + .constant('plupload', plupload) //Needed for use as dependency for the directive plUpload .provider('plUploadService', function() { var config = { @@ -29,7 +30,7 @@ angular.module('plupload.directive', []) }]; }) - .directive('plUpload', ['$parse', '$log', 'plUploadService', function ($parse, $log, plUploadService) { + .directive('plUpload', ['$parse', '$log', 'plupload', 'plUploadService', function ($parse, $log, plupload, plUploadService) { return { restrict: 'A', scope: { diff --git a/tests/js/plupload-angular-directive.spec.js b/tests/js/plupload-angular-directive.spec.js new file mode 100644 index 0000000..1fca632 --- /dev/null +++ b/tests/js/plupload-angular-directive.spec.js @@ -0,0 +1,406 @@ +describe('plupload-angular-directive', function(){ + + /* + * plUploadService tests + */ + describe('service', function(){ + + beforeEach(module('plupload.directive', function(plUploadServiceProvider){ + plUploadServiceProvider.setConfig('flashPath', 'plupload/flash.swf'); + plUploadServiceProvider.setConfig('silverLightPath', 'plupload/silverlight.xap'); + plUploadServiceProvider.setConfig('uploadPath', 'upload.php'); + })); + + var plUploadService; + beforeEach(inject(function(_plUploadService_){ + plUploadService = _plUploadService_; + })); + + describe('setConfig', function(){ + + it('can set values', function(){ + plUploadService.setConfig('flashPath', 'anotherValue'); + expect(plUploadService.getConfig('flashPath')).not.toBe('plupload/flash.swf'); + }); + }); + + describe('getConfig', function(){ + + it('can retrieve the config', function(){ + expect(plUploadService.getConfig('flashPath')).toBe('plupload/flash.swf'); + expect(plUploadService.getConfig('silverLightPath')).toBe('plupload/silverlight.xap'); + expect(plUploadService.getConfig('uploadPath')).toBe('upload.php'); + }); + + }); + + }); + + describe('directive', function(){ + + var $scope, $compile, $rootScope; + + beforeEach(module('plupload.directive', function(plUploadServiceProvider, $provide){ + plUploadServiceProvider.setConfig('flashPath', 'plupload/flash.swf'); + plUploadServiceProvider.setConfig('silverLightPath', 'plupload/silverlight.xap'); + plUploadServiceProvider.setConfig('uploadPath', 'upload.php'); + + //Mock the plupload + $provide.constant('plupload', PlUploadMock); + })); + + beforeEach(inject(function(_$rootScope_, _$compile_){ + $scope = _$rootScope_.$new(); + $compile = _$compile_; + $rootScope = _$rootScope_; + })); + + /** + * Fn that will help to create the element and compile into the scope + * @param attributes + */ + var createAndCompile = function(attributes) { + + var defaultAttributes = { + 'pl-upload' : '' + }; + + attributes = angular.extend(defaultAttributes, attributes); + + var element = angular.element('