Skip to content

Commit

Permalink
[added] default handler to routes
Browse files Browse the repository at this point in the history
  • Loading branch information
krawaller authored and ryanflorence committed Feb 10, 2015
1 parent edf684c commit cd2087d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
12 changes: 10 additions & 2 deletions modules/components/Route.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var React = require('react');
var Configuration = require('../Configuration');
var PropTypes = require('../PropTypes');

var RouteHandler = require('./RouteHandler');
/**
* <Route> components specify components that are rendered to the page when the
* URL matches a given pattern.
Expand Down Expand Up @@ -39,6 +39,8 @@ var PropTypes = require('../PropTypes');
* );
* }
* });
*
* If no handler is provided for the route, it will render a matched child route.
*/
var Route = React.createClass({

Expand All @@ -49,8 +51,14 @@ var Route = React.createClass({
propTypes: {
name: PropTypes.string,
path: PropTypes.string,
handler: PropTypes.func.isRequired,
handler: PropTypes.func,
ignoreScrollBehavior: PropTypes.bool
},

getDefaultProps: function(){
return {
handler: RouteHandler
};
}

});
Expand Down
38 changes: 38 additions & 0 deletions modules/components/__tests__/Route-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var expect = require('expect');
var React = require('react');
var Router = require('../../index');
var DefaultRoute = require('../DefaultRoute');
var Route = require('../Route');
var { Foo, Bar } = require('../../utils/TestHandlers');

describe('Route', function () {

it('renders default child route if path match but no handler provided', function () {
var routes = (
<Route path='/'>
<Route path='/bar' handler={Bar} />
<DefaultRoute handler={Foo} />
</Route>
);

Router.run(routes, '/', function (App) {
var html = React.renderToString(<App/>);
expect(html).toMatch(/Foo/);
});
});

it('renders matched child route if no handler provided', function () {
var routes = (
<Route path='/'>
<Route path='/bar' handler={Bar} />
<DefaultRoute handler={Foo} />
</Route>
);

Router.run(routes, '/bar', function (App) {
var html = React.renderToString(<App/>);
expect(html).toMatch(/Bar/);
});
});

});

0 comments on commit cd2087d

Please sign in to comment.