Skip to content

Commit 5e8bc06

Browse files
author
Mark Lagendijk
committed
Lets do this the professional way!
- Added Gulp for minifying + running tests - Added Karma + tests - Added bower.json
1 parent 6bb268f commit 5e8bc06

File tree

9 files changed

+250
-1
lines changed

9 files changed

+250
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
bower_components
3+
node_modules

recursionhelper.js renamed to angular-recursion.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
module.factory('RecursionHelper', ['$compile', function($compile){
1+
/*
2+
* An Angular service which helps with creating recursive directives.
3+
* @author Mark Lagendijk
4+
* @license MIT
5+
*/
6+
angular.module('RecursionHelper', []).factory('RecursionHelper', ['$compile', function($compile){
27
return {
38
/**
49
* Manually compiles the element, fixing the recursion loop.

angular-recursion.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bower.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "angular-recursionhelper",
3+
"main": "angular-recursionhelper.min.js",
4+
"version": "1.0.0",
5+
"homepage": "https://github.com/marklagendijk/angular-recursion",
6+
"authors": [
7+
"Mark Lagendijk <[email protected]>"
8+
],
9+
"description": "An Angular service which helps with creating recursive directives.",
10+
"keywords": [
11+
"angular",
12+
"angular-service",
13+
"angular-recursion",
14+
"angular-recursion-helper",
15+
"recursion",
16+
"recursive"
17+
],
18+
"license": "MIT",
19+
"ignore": [
20+
"**/.*",
21+
"node_modules",
22+
"package.json",
23+
"gulpfile.js",
24+
"bower_components",
25+
"test",
26+
"tests",
27+
"config"
28+
],
29+
"dependencies": {
30+
"angular": "~1.2.8"
31+
},
32+
"devDependencies": {
33+
"angular-mocks": "~1.2.8",
34+
"jQuery": "2.1.0"
35+
}
36+
}

config/config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
testFiles: [
3+
'bower_components/jQuery/dist/jquery.min.js',
4+
'bower_components/angular/angular.js',
5+
'bower_components/angular-mocks/angular-mocks.js',
6+
'angular-recursion.min.js',
7+
'test/**/*.js'
8+
]
9+
};

config/karma.conf.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var config = require('./config.js');
2+
module.exports = function(karmaConfig){
3+
karmaConfig.set({
4+
5+
// base path, that will be used to resolve files and exclude
6+
basePath: '../',
7+
8+
9+
// frameworks to use
10+
frameworks: ['jasmine'],
11+
12+
13+
// list of files / patterns to load in the browser
14+
files: config.testFiles,
15+
16+
17+
// test results reporter to use
18+
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
19+
reporters: ['progress'],
20+
21+
22+
// web server port
23+
port: 9876,
24+
25+
26+
// enable / disable colors in the output (reporters and logs)
27+
colors: true,
28+
29+
30+
// level of logging
31+
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
32+
logLevel: karmaConfig.LOG_WARN,
33+
34+
35+
// enable / disable watching file and executing tests whenever any file changes
36+
autoWatch: true,
37+
38+
39+
// Start these browsers, currently available:
40+
// - Chrome
41+
// - ChromeCanary
42+
// - Firefox
43+
// - Opera (has to be installed with `npm install karma-opera-launcher`)
44+
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
45+
// - PhantomJS
46+
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
47+
browsers: ['Chrome'],
48+
49+
50+
// If browser does not capture in given timeout [ms], kill it
51+
captureTimeout: 60000,
52+
53+
54+
// Continuous Integration mode
55+
// if true, it capture browsers, run tests and exit
56+
singleRun: false
57+
});
58+
};

gulpfile.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var gulp = require("gulp");
2+
var uglify = require("gulp-uglify");
3+
var rename = require("gulp-rename");
4+
var karma = require("gulp-karma");
5+
6+
var config = require('./config/config.js');
7+
8+
gulp.task("test", ["minify"], function(){
9+
gulp.src(config.testFiles)
10+
.pipe(karma({
11+
configFile: 'config/karma.conf.js',
12+
action: 'run'
13+
}));
14+
});
15+
16+
gulp.task("minify", function(){
17+
gulp.src("angular-recursion.js")
18+
.pipe(uglify())
19+
.pipe(rename("angular-recursion.min.js"))
20+
.pipe(gulp.dest("./"));
21+
});
22+
23+
gulp.task("default", ["minify", "test"]);

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"directories": {
3+
"test": "test"
4+
},
5+
"dependencies": {},
6+
"devDependencies": {
7+
"karma": "~0.11.9",
8+
"karma-chrome-launcher": "~0.1.2",
9+
"karma-jasmine": "~0.1.5",
10+
"karma-firefox-launcher": "~0.1.3",
11+
"karma-phantomjs-launcher": "~0.1.1",
12+
"karma-requirejs": "~0.2.1",
13+
"karma-script-launcher": "~0.1.0",
14+
"gulp": "~3.3.0",
15+
"gulp-uglify": "~0.1.0",
16+
"gulp-karma": "0.0.1",
17+
"gulp-rename": "~0.2.1"
18+
},
19+
"scripts": {
20+
"test": "gulp test",
21+
"minify": "gulp minify"
22+
}
23+
}

test/angularRecursionHelperSpec.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* globals: beforeEach, describe, it, module, inject, expect */
2+
describe('RecursionHelper', function(){
3+
var $compile, parent, scope, link;
4+
5+
angular.module('Tree', []).directive("tree", function(RecursionHelper){
6+
return {
7+
restrict: "E",
8+
scope: {
9+
family: '='
10+
},
11+
replace: true,
12+
template: '' +
13+
'<div class="tree">' +
14+
' <p>{{ family.name }}</p>' +
15+
' <ul>' +
16+
' <li ng-repeat="child in family.children">' +
17+
' <tree family="child"></tree>' +
18+
' </li>' +
19+
' </ul>' +
20+
'</div>',
21+
compile: function(element){
22+
return RecursionHelper.compile(element, link);
23+
}
24+
};
25+
});
26+
27+
beforeEach(module('RecursionHelper', 'Tree'));
28+
beforeEach(inject(function(_$compile_, $rootScope){
29+
$compile = _$compile_;
30+
scope = $rootScope.$new();
31+
scope.treeFamily = {
32+
name: "Parent",
33+
children: [
34+
{
35+
name: "Child1",
36+
children: [
37+
{
38+
name: "Grandchild1",
39+
children: []
40+
},
41+
{
42+
name: "Grandchild2",
43+
children: []
44+
},
45+
{
46+
name: "Grandchild3",
47+
children: []
48+
}
49+
]
50+
},
51+
{
52+
name: "Child2",
53+
children: []
54+
}
55+
]
56+
};
57+
}));
58+
59+
function compileTree(){
60+
parent = $compile('<tree family="treeFamily"></tree>')(scope);
61+
scope.$apply();
62+
}
63+
64+
65+
it('should render the whole tree', function(){
66+
compileTree();
67+
var children = parent.children('ul').children();
68+
var grandChildren = children.find(':first-child').children('ul').children();
69+
expect(children.length).toBe(2);
70+
expect(grandChildren.length).toBe(3);
71+
});
72+
73+
it('should call the pre and post linking functions, when passed as object in the link parameter', function(){
74+
link = {
75+
pre: jasmine.createSpy('pre'),
76+
post: jasmine.createSpy('post')
77+
};
78+
79+
compileTree();
80+
expect(link.pre).toHaveBeenCalled();
81+
expect(link.post).toHaveBeenCalled();
82+
83+
});
84+
85+
it('should call the post linking function, when passed as function in the link parameter', function(){
86+
link = jasmine.createSpy('post');
87+
88+
compileTree();
89+
expect(link).toHaveBeenCalled();
90+
});
91+
});

0 commit comments

Comments
 (0)