Skip to content

Commit

Permalink
Merge 3ba264d into b2131e9
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimoulis committed Sep 28, 2017
2 parents b2131e9 + 3ba264d commit fc18347
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 17 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
.nvmrc
*.log

# We do not commit node modules, builds and temp
/node_modules
# We do not commit node modules, builds, coverage and temp
/build
/coverage
/node_modules
/tmp

# MACs come on
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Simple route matcher using minimatch.
[![node][node]][node-url]
[![deps][deps]][deps-url]
[![tests][tests]][tests-url]
[![coverage][cover]][cover-url]

## Installation

Expand Down Expand Up @@ -118,3 +119,6 @@ router.clearAll();

[tests]: https://img.shields.io/travis/cdimoulis/simple-routes/master.svg
[tests-url]: https://travis-ci.org/cdimoulis/simple-routes

[cover]: https://coveralls.io/repos/github/cdimoulis/simple-routes/badge.svg?branch=master
[cover-url]: https://coveralls.io/github/cdimoulis/simple-routes?branch=master
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"url": "https://github.com/cdimoulis/simple-routes.git"
},
"scripts": {
"test": "jest"
"test": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
"test-dev": "jest --coverage"
},
"engines": {
"node": ">= 6.11"
Expand All @@ -29,6 +30,7 @@
"minimatch": "^3.0.4"
},
"devDependencies": {
"coveralls": "^3.0.0",
"jest": "^21.2.0"
},
"jest": {
Expand Down
17 changes: 8 additions & 9 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@ const Router = function(opts) {
if (action && (typeof action) === 'function') {
let route_len = _routes.push(route);
let action_len = _actions.push(action);
if (route_len != action_len) {
throw `Router Error: Mismatch routes and actions\nroute: ${route}\naction: ${action}`;
}
}
else {
throw `Router Error: Action must be function for\nroute: ${route}\naction: ${action}`;
throw new Error(`Router Error: Action must be a function for\nroute: ${route}\naction: ${action}`);
}
}
else {
throw `Router Error: Route must be a string for\nroute: ${route}\naction: ${action}`;
throw new Error(`Router Error: Route must be a string for\nroute: ${route}\naction: ${action}`);
}
// Allow chaining of addRoutes
return this;
Expand All @@ -35,11 +32,13 @@ const Router = function(opts) {
// Adds multiple routes and actions.
// list: array of arrays. Each array is a pair of [route, action].
this.addRoutes = (list) => {
if (list) {
for (let i = 0; i < list.length; i++) {
let obj = list[i];
if (list && Array.isArray(list)) {
list.forEach((obj) => {
this.addRoute(obj[0], obj[1]);
}
});
}
else {
throw new Error(`Router Error: List must be an array\nList: ${list}`)
}
// Allow chaining of addRoutes
return this;
Expand Down
47 changes: 42 additions & 5 deletions test/router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,73 @@ test('Clear all', () => {

test('Add routes', () => {
router.clearAll();
// Create a function without name
let f = {};
f.x = () => {return 3;};
router.addRoutes([
['/test', action1],
['/love', action2],
['/temp', f.x],
]);
let routes = router.routes();
let actions = router.actions();
expect(routes.length).toBe(2);
expect(actions.length).toBe(2);
expect(routes.length).toBe(3);
expect(actions.length).toBe(3);
expect(routes[1]).toBe('/love');
expect(actions[1]).toBe(action2);
});

test('Get Action', () => {
expect(router.getAction('/test')).toBe(action1);
expect(router.getAction('/love')).toBe(action2);
expect(router.getAction('/not_found')).toBe(-1);
});

test('Remove route', () => {
router.removeRoute('test');
let routes = router.routes();
expect(routes.length).toBe(2);
expect(routes.length).toBe(3);
router.removeRoute('/test');
routes = router.routes();
let actions = router.actions();
expect(routes.length).toBe(1);
expect(actions.length).toBe(1);
expect(routes.length).toBe(2);
expect(actions.length).toBe(2);
expect(routes).not.toContain('/test');
expect(actions).not.toContain(action1);
});

test('Has Route', () => {
expect(router.hasRoute('/love')).toBeTruthy();
expect(router.hasRoute('/not_found')).toBeFalsy();
});

test('To String', () => {
expect(typeof router.toString()).toBe('string');
});


// EXCEPTIONS
test('Bad Route', () => {
let f = () => {router.addRoute(3, action1)};
expect(f).toThrow(/Router Error: Route must be a string/);
});

test('Bad Action', () => {
let f = () => {router.addRoute('/edit',3)};
expect(f).toThrow(/Router Error: Action must be a function/);
});

test('Missmatch routes-actions', () => {
let f = () => {
router.addRoutes([
['/test',action1],
['/peace']
]);
};
expect(f).toThrow();
});

test('Invalid list', () => {
let f = () => {router.addRoutes('/edit',action1)};
expect(f).toThrow(/Router Error: List must be an array/);
})

0 comments on commit fc18347

Please sign in to comment.