diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1c871c2 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +language: node_js +node_js: + - 5.6.0 +before_script: + - npm install + - npm install bower + - bower install + - npm update + - bower update +script: gulp diff --git a/README.md b/README.md index 6b8e997..1437c4e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![Build Status](https://travis-ci.org/Kangele/ws-script-loader.svg?branch=travis_setup)](https://travis-ci.org/Kangele/ws-script-loader) + # wsScriptLoader Dynamically load scripts in your Angular code. diff --git a/dist/ws-script-loader.js b/dist/ws-script-loader.js index 1caffc6..bc683e5 100755 --- a/dist/ws-script-loader.js +++ b/dist/ws-script-loader.js @@ -1,7 +1,7 @@ (function() { -angular.module('wsScriptLoader', []) +angular.module('wsScriptLoader', ['wsScriptLoader']) .service('wsScriptLoader', ["$q", function($q) { var service = this; @@ -21,6 +21,7 @@ angular.module('wsScriptLoader', []) document.body.appendChild(tag); loadedScripts.push(tagUrl); + return tag; } else { @@ -36,11 +37,11 @@ angular.module('wsScriptLoader', []) service.loadScriptTag = function (tagUrl) { var deferred = $q.defer(); - service.insertScriptTag(tagUrl, function () { + return service.insertScriptTag(tagUrl, function () { deferred.resolve(); }); return deferred.promise; - }; + }; }]); -}()); \ No newline at end of file +}()); diff --git a/karma-src.conf.js b/karma-src.conf.js index ec87981..66e0da1 100644 --- a/karma-src.conf.js +++ b/karma-src.conf.js @@ -10,7 +10,7 @@ module.exports = function(config) { // frameworks to use // available frameworks: https://npmjs.org/browse/keyword/karma-adapter - frameworks: ['mocha', 'chai-jquery', 'jquery-1.8.3', 'sinon-chai'], + frameworks: ['jasmine', 'chai-jquery', 'jquery-1.8.3', 'sinon-chai', 'mocha'], plugins: [ 'karma-mocha', @@ -20,15 +20,17 @@ module.exports = function(config) { 'karma-phantomjs-launcher', 'karma-jquery', 'karma-chai-jquery', - 'karma-mocha-reporter' + 'karma-mocha-reporter', + 'karma-jasmine' ], // list of files / patterns to load in the browser files: [ 'bower/angular/angular.js', 'bower/angular-mocks/angular-mocks.js', - 'src/**/*.js', - 'test/unit/**/*.js' + 'dist/ws-script-loader.js', + 'src/app/controller.js', + 'test/*.js', ], diff --git a/package.json b/package.json index 1db12a5..f87977d 100644 --- a/package.json +++ b/package.json @@ -30,5 +30,8 @@ }, "engines": { "node": ">=0.8.0" + }, + "scripts": { + "test": "gulp test-debug" } } diff --git a/src/app/controller.js b/src/app/controller.js new file mode 100644 index 0000000..d97bd21 --- /dev/null +++ b/src/app/controller.js @@ -0,0 +1,11 @@ +angular.module('wsScriptLoader') + .controller('wsScriptLoaderController', ['wsScriptLoader', function(wsScriptLoader) { + + var wsScriptLoaderCtrl = this; + + wsScriptLoaderCtrl.scriptName = ""; + + wsScriptLoaderCtrl.addScript = function(scriptName) { + wsScriptLoader.loadScriptTag(scriptName); + }; + }]); diff --git a/src/app/index.html b/src/app/index.html new file mode 100644 index 0000000..50f8006 --- /dev/null +++ b/src/app/index.html @@ -0,0 +1,20 @@ + + + + + wsScriptLoader unit test + + + + + + + +
+ +
+
+ +
+ + diff --git a/src/app/myScript.js b/src/app/myScript.js new file mode 100644 index 0000000..375dc12 --- /dev/null +++ b/src/app/myScript.js @@ -0,0 +1 @@ +var test = 3; diff --git a/src/wsScriptLoader.js b/src/wsScriptLoader.js index 67fb3bb..eef064b 100755 --- a/src/wsScriptLoader.js +++ b/src/wsScriptLoader.js @@ -1,4 +1,4 @@ -angular.module('wsScriptLoader', []) +angular.module('wsScriptLoader', ['wsScriptLoader']) .service('wsScriptLoader', function($q) { var service = this; @@ -18,6 +18,7 @@ angular.module('wsScriptLoader', []) document.body.appendChild(tag); loadedScripts.push(tagUrl); + return tag; } else { @@ -38,5 +39,5 @@ angular.module('wsScriptLoader', []) }); return deferred.promise; - }; - }); \ No newline at end of file + }; + }); diff --git a/test/wsScriptLoaderSpec.js b/test/wsScriptLoaderSpec.js index c513661..808d391 100755 --- a/test/wsScriptLoaderSpec.js +++ b/test/wsScriptLoaderSpec.js @@ -1,29 +1,93 @@ 'use strict'; -describe('', function() { +describe('wsScriptLoaderService test', function() { - var module; - var dependencies; - dependencies = []; + var wsScriptLoader; + var dependencies = []; + var wsScriptLoaderService; var hasModule = function(module) { - return dependencies.indexOf(module) >= 0; + return dependencies.indexOf(module) >= 0; }; - beforeEach(function() { - // Get module - module = angular.module('wsScriptLoader'); - dependencies = module.requires; - }); + beforeEach(module('wsScriptLoader')); + + beforeEach(inject(function($injector) { + wsScriptLoaderService = $injector.get('wsScriptLoader'); + })); it('should load module', function() { - expect(hasModule('wsScriptLoader')).to.be.ok; + dependencies = angular.module('wsScriptLoader').requires; + expect(hasModule('wsScriptLoader')).toBeTruthy(); + }); + + /*** CHECK IF METHODS ARE DEFINED ***/ + + it('insertScriptTag should be defined', function() { + expect(wsScriptLoaderService.insertScriptTag).toBeDefined(); }); - + it('loadScriptTag should be defined', function() { + expect(wsScriptLoaderService.loadScriptTag).toBeDefined(); + }); - + /*** CHECK IF METHODS WORK WELL ***/ - + // For insertScriptTag -}); \ No newline at end of file + it('method insertScriptTag should add script into the DOM', function() { + + var data = { + scriptName: 'myScript', + callback: function() { + console.error('Hello !'); + } + }; + + var element = wsScriptLoaderService.insertScriptTag(data.scriptName, data.callback); + var src = angular.element(element).attr('src'); + var onload = angular.element(element).attr('onload'); + + expect(src).toBeDefined(); + expect(src).toEqual('myScript'); + + expect(onload).toBeDefined(); + expect(onload).toEqual(data.callback); + }); + + // For loadScriptTag + it('method loadScriptTag should add script into the DOM', function() { + + var data = { + scriptName: 'myScript' + }; + + var element = wsScriptLoaderService.loadScriptTag(data.scriptName); + var src = angular.element(element).attr('src'); + var onload = angular.element(element).attr('onload'); + + expect(src).toBeDefined(); + expect(src).toEqual('myScript'); + + expect(onload).toBeDefined(); + expect(onload).toEqual(data.callback); + }); + + it('method insertScriptTag should execute the callback when script have already been added !', function() { + + // object created to use spies functionality + var data = { + scriptName: 'myScript', + callback: function() { + console.error('Script added !'); + } + }; + spyOn(data, 'callback'); // crate a spy to notice if the callback have been called + // First call + wsScriptLoaderService.insertScriptTag(data.scriptName, data.callback); + // Second call + wsScriptLoaderService.insertScriptTag(data.scriptName, data.callback); + + expect(data.callback).toHaveBeenCalled(); // suppose to be true + }); +});