Skip to content

Commit

Permalink
Merge pull request #1 from olivarc/fix-dispatch
Browse files Browse the repository at this point in the history
Fixed dispatch to undefined route
  • Loading branch information
jmhdez authored Jul 19, 2016
2 parents 0cb2444 + f3f9e9e commit efba3cd
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ export function getQueryParams(query) {
const [key, value] = part.split('=');
acc[decodeURIComponent(key)] = decodeURIComponent(value);
return acc;
}, {});
}, {});
};

export function createRoute(name, path, handler) {
const matcher = new RegExp(path.replace(parametersPattern, '([^\/]+)'));
const matcher = new RegExp(path.replace(parametersPattern, '([^\/]+)') + '$');
const params = (path.match(parametersPattern) || []).map(x => x.substring(1));

return {name, path, handler, matcher, params};
};

const findRouteParams = (routes, path) => {
let params;
const route = routes.find(r => params = getMatchedParams(r, path));
return route && {route, params};
return {route, params};
};

const parseUrl = (url) => {
const [path, queryString] = url.split('?');
return {path, queryString};
return {path, queryString};
};

const stripPrefix = (url ,prefix) => url.replace(new RegExp('^' + prefix), '');
Expand Down Expand Up @@ -71,7 +71,7 @@ export default class Router {
const {path, queryString} = parseUrl(stripPrefix(url, this.prefix));
const query = getQueryParams(queryString || '');
const {route, params} = findRouteParams(this.routes, path);

if (route) {
route.handler({params, query});
return route;
Expand Down
24 changes: 12 additions & 12 deletions test/RouteCreationSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {createRoute, getQueryParams, getMatchedParams} from '../src/Router.js';
describe('Route creation', function() {

var noop = function() {};

var buildMatcher = function(path) {
return createRoute('', path, noop).matcher;
};
Expand All @@ -17,26 +17,26 @@ describe('Route creation', function() {
};

describe('build matcher', function() {

// I'm missing c# verbatim strings. All those \\/ are needed because
// in the rexexp / must be escaped with \, and in the string \ must
// be escaped with another \

it('should build matcher regex for routes without parameters', function() {
const r = buildMatcher('/users');
assert.equal(r.source, '\\/users');
assert.equal(r.source, '\\/users$');
});

it('should build matcher regex for routes with one parameter', function() {
const r = buildMatcher('/users/:id');
assert.equal(r.source, '\\/users\\/([^\\/]+)');
assert.equal(r.source, '\\/users\\/([^\\/]+)$');
});

it('should build matcher regex for routes with more than one parameters', function() {
const r = buildMatcher('/users/:id/contact/:contactId');
assert.equal(r.source, '\\/users\\/([^\\/]+)\\/contact\\/([^\\/]+)');
assert.equal(r.source, '\\/users\\/([^\\/]+)\\/contact\\/([^\\/]+)$');
});

});

describe('extract parameter names', function() {
Expand Down Expand Up @@ -69,22 +69,22 @@ describe('getQueryParams', function() {
describe('getMatchedParams', function() {

var route = path => createRoute('', path, function(){});

it('should return falsy when there is no match', function() {
const m = getMatchedParams(route('/users'), '/company');
assert(!m);
});

it('should return truthy when there is a match with no params', function() {
const m = getMatchedParams(route('/users'), '/users');
assert(m);
assert(m);
});

it('should return object with parameters when there is a match with params', function() {
const m = getMatchedParams(
route('/users/:userId/contacts/:contactId'),
'/users/4/contacts/12');

assert(m);
assert.equal(m.userId, 4);
assert.equal(m.contactId, 12);
Expand Down
31 changes: 27 additions & 4 deletions test/RouterSpecs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Router from '../src/Router.js';
describe('Router', function() {

var router;

beforeEach(function() {
router = new Router();
});
Expand Down Expand Up @@ -35,21 +35,44 @@ describe('Router', function() {
});

describe('dispatching', function() {

it('should not dispatch to undefined route', function() {

var dispatched = false;

router.add('/simple', _ => dispatched = true);

router.dispatch('/another');

assert.equal(dispatched, false);
});

it('should not dispatch to prefix routes', function() {

var dispatched = false;

router.add('/simple', _ => dispatched = true);

router.dispatch('/simple1');

assert.equal(dispatched, false);
});

it('should dispatch to simple routes', function() {

var dispatched = false;

router.add('/simple', _ => dispatched = true);

router.dispatch('/simple');

assert.equal(dispatched, true);
});

it('should dispatch to the right route', function() {

let dispatched;

router.add('/one', _ => dispatched = 'one');
router.add('/two', _ => dispatched = 'two');

Expand Down Expand Up @@ -87,7 +110,7 @@ describe('Router', function() {

it('should dispatch to routes with configured prefix', function() {
let dispatched = false;

router.setPrefix('#')
.add('/users', _ => dispatched = true);

Expand Down

0 comments on commit efba3cd

Please sign in to comment.