Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wsScriptLoader unit tests #1

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
9 changes: 5 additions & 4 deletions dist/ws-script-loader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function() {


angular.module('wsScriptLoader', [])
angular.module('wsScriptLoader', ['wsScriptLoader'])
.service('wsScriptLoader', ["$q", function($q) {
var service = this;

Expand All @@ -21,6 +21,7 @@ angular.module('wsScriptLoader', [])
document.body.appendChild(tag);

loadedScripts.push(tagUrl);
return tag;
}
else
{
Expand All @@ -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;
};
};
}]);
}());
}());
10 changes: 6 additions & 4 deletions karma-src.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
],


Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
},
"engines": {
"node": ">=0.8.0"
},
"scripts": {
"test": "gulp test-debug"
}
}
11 changes: 11 additions & 0 deletions src/app/controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
angular.module('wsScriptLoader')
.controller('wsScriptLoaderController', ['wsScriptLoader', function(wsScriptLoader) {

var wsScriptLoaderCtrl = this;

wsScriptLoaderCtrl.scriptName = "";

wsScriptLoaderCtrl.addScript = function(scriptName) {
wsScriptLoader.loadScriptTag(scriptName);
};
}]);
20 changes: 20 additions & 0 deletions src/app/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>wsScriptLoader unit test</title>
<script src="../../bower/angular/angular.js" type="text/javascript"></script>
<script src="../../bower/angular-mocks/angular-mocks.js" type="text/javascript"></script>
<script src="../../dist/ws-script-loader.js" type="text/javascript"></script>
<script src="controller.js" type="text/javascript"></script>
</head>
<body ng-app="wsScriptLoader">

<div class="container" ng-controller="wsScriptLoaderController as ws">
<input type="text" name="scriptName" ng-model="ws.scriptName" />
<br/>
<br/>
<button ng-click="ws.addScript(ws.scriptName)">Add Script</button>
</div>
</body>
</html>
1 change: 1 addition & 0 deletions src/app/myScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var test = 3;
7 changes: 4 additions & 3 deletions src/wsScriptLoader.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
angular.module('wsScriptLoader', [])
angular.module('wsScriptLoader', ['wsScriptLoader'])
.service('wsScriptLoader', function($q) {
var service = this;

Expand All @@ -18,6 +18,7 @@ angular.module('wsScriptLoader', [])
document.body.appendChild(tag);

loadedScripts.push(tagUrl);
return tag;
}
else
{
Expand All @@ -38,5 +39,5 @@ angular.module('wsScriptLoader', [])
});

return deferred.promise;
};
});
};
});
94 changes: 79 additions & 15 deletions test/wsScriptLoaderSpec.js
Original file line number Diff line number Diff line change
@@ -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

});
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
});
});