Skip to content

Commit

Permalink
Merge pull request #7 from rudolfsonjunior/travis_ci_coveralls
Browse files Browse the repository at this point in the history
  • Loading branch information
aichbauer committed Mar 13, 2017
2 parents 4e914ba + 213fb58 commit 889e94c
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 90 deletions.
9 changes: 8 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"presets": ["env"]
"presets": ["env"],
"plugins": ["transform-runtime"],
"ignore": "./tests/lib/index.js",
"env": {
"development": {
"sourceMaps": "inline"
}
}
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
dist/
.npmignore
coverage
.nyc_output

# Logs
logs
Expand Down
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
language: node_js
sudo: true
dist: trusty
node_js:
- "4.0"
- "5.0"
- "6.0"
install:
- npm install
script: npm run test
notifications:
email:
on_failure: change
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
6 changes: 2 additions & 4 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import gulp from 'gulp';
import babel from 'gulp-babel';
import uglify from 'gulp-uglify';

gulp.task('default', () => {
return gulp.src('./lib/index.js')
.pipe(babel())
.pipe(uglify())
.pipe(gulp.dest('./dist'));
.pipe(babel())
.pipe(gulp.dest('./dist'));
});
52 changes: 25 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,31 @@ function mapRoutes(routes, pathToController = '../../../app/controllers/') {
const routesArr = entries(routes);

routesArr.forEach((value) => {
if (Object.prototype.hasOwnProperty.call(routes, value[0])) {
requestMethodPath = value[0].replace(/\s\s+/g, ' ');

requestMethod = (requestMethodPath.split(' ')[0]).toLocaleLowerCase();
path = requestMethodPath.split(' ')[1];

controller = value[1].split('.')[0];
controllerMethod = value[1].split('.')[1];

try {
require('babel-register');
Handler = require(pathToController + controller).default;
c = new Handler();
} catch (err) {
handler = require(pathToController + controller);
c = handler;
}

if (requestMethod === 'get') {
router.route(path).get(c[controllerMethod]);
} else if (requestMethod === 'post') {
router.route(path).post(c[controllerMethod]);
} else if (requestMethod === 'put') {
router.route(path).put(c[controllerMethod]);
} else if (requestMethod === 'delete') {
router.route(path).delete(c[controllerMethod]);
}
requestMethodPath = value[0].replace(/\s\s+/g, ' ');

requestMethod = (requestMethodPath.split(' ')[0]).toLocaleLowerCase();
path = requestMethodPath.split(' ')[1];

controller = value[1].split('.')[0];
controllerMethod = value[1].split('.')[1];

try {
require('babel-register');
Handler = require(pathToController + controller).default;
c = new Handler();
} catch (err) {
handler = require(pathToController + controller);
c = handler;
}

if (requestMethod === 'get') {
router.route(path).get(c[controllerMethod]);
} else if (requestMethod === 'post') {
router.route(path).post(c[controllerMethod]);
} else if (requestMethod === 'put') {
router.route(path).put(c[controllerMethod]);
} else if (requestMethod === 'delete') {
router.route(path).delete(c[controllerMethod]);
}
});
return router;
Expand Down
22 changes: 18 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"scripts": {
"start": "gulp",
"lint": "eslint ./lib/index.js",
"test": "ava ./tests/lib/",
"test": "gulp && nyc ava ./tests/lib/*",
"prepublish": "npm start",
"precommit": "npm run lint"
"precommit": "npm run lint",
"build": "BABEL_ENV=production babel --out-dir=dist index.js"
},
"keywords": [
"express",
Expand All @@ -31,13 +32,26 @@
"devDependencies": {
"ava": "^0.18.2",
"babel-core": "^6.23.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.2.1",
"coveralls": "^2.12.0",
"eslint": "^3.17.1",
"eslint-config-airbnb-base": "^11.1.1",
"eslint-plugin-import": "^2.2.0",
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-uglify": "^2.0.1",
"husky": "^0.13.2"
"husky": "^0.13.2",
"nyc": "^10.1.2"
},
"ava": {
"require": [
"babel-core/register"
]
},
"nyc": {
"exclude": [
"tests",
"examples"
]
}
}
107 changes: 53 additions & 54 deletions tests/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import test from 'ava';
import mapRoutes from '../../dist/index';
import mapRoutes2 from '../../dist/index';

const routes = {

'GET /user': 'UserController.get',
'POST /user/:name': 'UserController.create',
'DELETE /user/:name/:id': 'UserController.destroy',
'PUT /user/:name/:id': 'UserController.update',
'ERROR /user': 'UserController.update'


}

const routes2 = {}

// es5 testing
test('GET /user : UserController.get', t => {
let router = mapRoutes({'GET /user': 'UserController.get'}, '../examples/es5_example/app/controllers/');
let router = mapRoutes(routes, '../examples/es5_example/app/controllers/');

// method
t.is('get', router.stack[0].route.stack[0].method);
Expand All @@ -13,10 +27,6 @@ test('GET /user : UserController.get', t => {
t.is('get', router.stack[0].route.stack[0].name);
// function to call
t.is('function', typeof(router.stack[0].route.stack[0].handle));
});

test('POST /user/:name : UserController.create', t => {
let router = mapRoutes({'POST /user/:name': 'UserController.create'}, '../examples/es5_example/app/controllers/');

// method
t.is('post', router.stack[1].route.stack[0].method);
Expand All @@ -28,11 +38,6 @@ test('POST /user/:name : UserController.create', t => {
t.is('create', router.stack[1].route.stack[0].name);
// function to call
t.is('function', typeof(router.stack[1].route.stack[0].handle));
});

test('POST /user/:name/:id : UserController.destroy', t => {
let router = mapRoutes({'DELETE /user/:name/:id': 'UserController.destroy'}, '../examples/es5_example/app/controllers/');


// method
t.is('delete', router.stack[2].route.stack[0].method);
Expand All @@ -45,10 +50,6 @@ test('POST /user/:name/:id : UserController.destroy', t => {
t.is('destroy', router.stack[2].route.stack[0].name);
// function to call
t.is('function', typeof(router.stack[2].route.stack[0].handle));
});

test('PUT /user/:name/:id : UserController.update', t => {
let router = mapRoutes({'PUT /user/:name/:id': 'UserController.update'}, '../examples/es5_example/app/controllers/');

// method
t.is('put', router.stack[3].route.stack[0].method);
Expand All @@ -60,67 +61,65 @@ test('PUT /user/:name/:id : UserController.update', t => {
// function name
t.is('update', router.stack[3].route.stack[0].name);
// function to call
t.is('function', typeof(router.stack[2].route.stack[0].handle));
});
t.is('function', typeof(router.stack[3].route.stack[0].handle));

// es 6 testing
test('GET /user : UserController.get', t => {
let router = mapRoutes({'GET /user': 'UserController.get'}, '../examples/es6_example/app/controllers/');
t.is('function', typeof(router));
});

test('es6 testing', t => {
let router = mapRoutes(routes, '../examples/es6_example/app/controllers/');

// method
t.is('get', router.stack[0].route.stack[0].method);
t.is('get', router.stack[4].route.stack[0].method);
// route
t.is('/user', router.stack[0].route.path);
t.is('/user', router.stack[4].route.path);
// function name
t.is('get', router.stack[0].route.stack[0].name);
t.is('get', router.stack[4].route.stack[0].name);
// function to call
t.is('function', typeof(router.stack[0].route.stack[0].handle));
});

test('POST /user/:name : UserController.create', t => {
let router = mapRoutes({'POST /user/:name': 'UserController.create'}, '../examples/es6_example/app/controllers/');
t.is('function', typeof(router.stack[4].route.stack[0].handle));

// method
t.is('post', router.stack[1].route.stack[0].method);
t.is('post', router.stack[5].route.stack[0].method);
// route
t.is('/user/:name', router.stack[1].route.path);
t.is('/user/:name', router.stack[5].route.path);
// keys for route
t.is('name', router.stack[1].keys[0].name);
t.is('name', router.stack[5].keys[0].name);
// function name
t.is('create', router.stack[1].route.stack[0].name);
t.is('create', router.stack[5].route.stack[0].name);
// function to call
t.is('function', typeof(router.stack[1].route.stack[0].handle));
});

test('POST /user/:name/:id : UserController.destroy', t => {
let router = mapRoutes({'DELETE /user/:name/:id': 'UserController.destroy'}, '../examples/es6_example/app/controllers/');

t.is('function', typeof(router.stack[5].route.stack[0].handle));

// method
t.is('delete', router.stack[2].route.stack[0].method);
t.is('delete', router.stack[6].route.stack[0].method);
// route
t.is('/user/:name/:id', router.stack[2].route.path);
t.is('/user/:name/:id', router.stack[6].route.path);
// keys for route
t.is('name', router.stack[2].keys[0].name);
t.is('id', router.stack[2].keys[1].name);
t.is('name', router.stack[6].keys[0].name);
t.is('id', router.stack[6].keys[1].name);
// function name
t.is('destroy', router.stack[2].route.stack[0].name);
t.is('destroy', router.stack[6].route.stack[0].name);
// function to call
t.is('function', typeof(router.stack[2].route.stack[0].handle));
});

test('PUT /user/:name/:id : UserController.update', t => {
let router = mapRoutes({'PUT /user/:name/:id': 'UserController.update'}, '../examples/es6_example/app/controllers/');
t.is('function', typeof(router.stack[6].route.stack[0].handle));

// method
t.is('put', router.stack[3].route.stack[0].method);
t.is('put', router.stack[7].route.stack[0].method);
// route
t.is('/user/:name/:id', router.stack[3].route.path);
t.is('/user/:name/:id', router.stack[7].route.path);
// keys for route
t.is('name', router.stack[3].keys[0].name);
t.is('id', router.stack[3].keys[1].name);
t.is('name', router.stack[7].keys[0].name);
t.is('id', router.stack[7].keys[1].name);
// function name
t.is('update', router.stack[3].route.stack[0].name);
t.is('update', router.stack[7].route.stack[0].name);
// function to call
t.is('function', typeof(router.stack[2].route.stack[0].handle));
});
t.is('function', typeof(router.stack[7].route.stack[0].handle));

t.is('function', typeof(router));

});

test('Default path to', t => {
let router = mapRoutes(routes2);
t.is(8, router.stack.length);
});


0 comments on commit 889e94c

Please sign in to comment.