Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisGuillem committed Aug 16, 2017
0 parents commit 7a441ee
Show file tree
Hide file tree
Showing 15 changed files with 1,745 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules
/coverage
/test/results
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- '6'
install:
- npm install -g codecov
- npm install
script:
- npm test
- codecov
56 changes: 56 additions & 0 deletions dist/MyLib.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions dist/MyLib.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
module.exports = function(grunt) {

// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
browserify: {
options: {
browserifyOptions : {
debug: true
}
},
dist: {
src: "src/main.js",
dest: "dist/<%= pkg.name %>.js"
},
test: {
files: {
"test/test.js": ["test/src/*.js"]
},
options: {
browserifyOptions: {
transform: [["browserify-istanbul", {ignore:["**/lib/**"]}]],
debug: true
}
}
}
},
uglify: {
options: {
banner: '/*! <%= pkg.name %> <%= pkg.version %> <%= grunt.template.today("yyyy-mm-dd") %>\n'
},
build: {
src: 'dist/<%= pkg.name %>.js',
dest: 'dist/<%= pkg.name %>.min.js'
}
},
watch: {
source: {
files: ["src/**/*", "test/**/*"],
tasks: ["test"]
}
},
mocha_phantomjs: {
options: {
config: {
"hooks": __dirname + '/test/src/phantom_hooks.js'
}
},
all: 'test/test.html'
},
makeReport: {
src: 'coverage/coverage.json',
options: {
type: 'lcov',
dir: 'coverage',
print: 'detail'
}
}
});

// Load plugins
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-jsdoc');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-mocha-phantomjs');
grunt.loadNpmTasks('grunt-istanbul');
// Default task(s).
grunt.registerTask('build', ['browserify', 'uglify']);
grunt.registerTask("build-test", ['browserify:test']);
grunt.registerTask("test", ["build-test", "mocha_phantomjs", "makeReport"])
grunt.registerTask('default', ['watch:source']);

};
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "MyLib",
"version": "1.0.0",
"description": "A template to write robust client-side javascript libraries",
"main": "src/main.js",
"scripts": {
"test": "mocha-phantomjs ./test/test.html --hooks ./test/src/phantom_hooks.js -R xunit -f test/results/result.xml",
"coverage-report": "istanbul report --root coverage lcov"
},
"repository": {
"type": "git",
"url": "https://github.com/rte-antares-rpackage/js-client-tmpl.git"
},
"keywords": [
"javascript",
"client",
"side",
"template",
"unit testing",
"code coverage"
],
"author": "Francois Guillem",
"license": "MIT",
"dependencies": {},
"devDependencies": {
"browserify-istanbul": "^2.0.0",
"grunt": "^1.0.1",
"grunt-browserify": "^5.0.0",
"grunt-contrib-uglify": "^2.0.0",
"grunt-contrib-watch": "^1.0.0",
"grunt-istanbul": "^0.8.0",
"grunt-mocha-phantomjs": "^4.0.0",
"istanbul": "^0.4.5",
"mocha-phantomjs": "^4.1.0"
},
"bugs": {
"url": "https://github.com/rte-antares-rpackage/js-client-tmpl/issues"
},
"homepage": "https://github.com/rte-antares-rpackage/js-client-tmpl#readme"
}
9 changes: 9 additions & 0 deletions src/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(function() {
'use strict';

module.exports = bar;

function bar() {
return "world!";
}
}());
10 changes: 10 additions & 0 deletions src/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(function() {
'use strict';

module.exports = foo;

function foo() {
return "Hello";
}

}());
29 changes: 29 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
(function() {
'use strict';

module.exports.getMessage = getMessage;
module.exports.sayHello = sayHello;
module.exports.sayHelloAsync = sayHelloAsync;

// If we are in a browser, define a global variable.
if (window) window.MyLib = module.exports;

var foo = require("./foo.js");
var bar = require("./bar.js");

function getMessage() {
return foo() + " " + bar();
}

function sayHello(el) {
$(el).html(getMessage());
}

function sayHelloAsync(el, delay, callback) {
setTimeout(function() {
sayHello(el);
callback();
}, delay);
}

}());
4 changes: 4 additions & 0 deletions test/lib/jquery-3.1.1.min.js

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions test/src/phantom_hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// You should not move or edit this file

module.exports = {
afterEnd: function(runner) {
var fs = require('fs');
var coverage = runner.page.evaluate(function() {
return window.__coverage__;
});

if (coverage) {
console.log('Writing coverage to coverage/coverage.json');
fs.write('coverage/coverage.json', JSON.stringify(coverage), 'w');
} else {
console.log('No coverage data generated');
}
}
};
31 changes: 31 additions & 0 deletions test/src/test-MyLib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(function() {
'use strict';

var assert = require("assert");
require("../../src/main.js");

describe("MyLib", function() {
beforeEach(function() {
$("#message").html("");
});

it("returns an important message", function() {
assert.equal(MyLib.getMessage(), "Hello world!");
});

it("displays the message in the desired element", function() {
MyLib.sayHello("#message");
assert.equal($("#message").html(), MyLib.getMessage());
})

it("asynchronously displays the message in the desired element", function(done) {
MyLib.sayHelloAsync("#message", 10, function() {
assert.equal($("#message").html(), MyLib.getMessage());
done();
});
})


});

}());
19 changes: 19 additions & 0 deletions test/src/test-foobar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
(function() {
'use strict';

var assert = require("assert");
var foo = require("../../src/foo.js");
var bar = require("../../src/bar.js");

describe("foo()", function() {
it("returns 'Hello'", function() {
assert.equal(foo(), "Hello");
});
});

describe("bar()", function() {
it("returns 'world!'", function() {
assert.equal(bar(), "world!");
})
})
}());
26 changes: 26 additions & 0 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test MyLib</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css">
<script src="../node_modules/mocha/mocha.js" charset="utf-8"></script>
<!-- Other external dependencies -->
<script src="lib/jquery-3.1.1.min.js" charset="utf-8"></script>
</head>
<body>
<!-- Custom html elements required by the tests -->
<div id="message"></div>

<!-- Run tests. You should not modify the following lines. -->
<div id="mocha"></div>
<script type="text/javascript">
mocha.ui('bdd');
</script>
<script src="test.js" charset="utf-8"></script>
<script type="text/javascript">
mocha.run();
</script>

</body>
</html>
Loading

0 comments on commit 7a441ee

Please sign in to comment.