Skip to content

Commit

Permalink
[added] ability to set params/query in Redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanflorence committed Oct 7, 2014
1 parent acc00f7 commit 6723dc5
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
14 changes: 14 additions & 0 deletions docs/api/components/Redirect.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ to `*` so you can redirect anything not found to somewhere else.

The `name` of the route you want to redirect to.

### `params`

By default, the parameters will just pass through to the new route, but
you can specify them if you need to (usually you shouldn't).

### `query`

By default, the query parameters will just pass through to the new
route, but you can specify them if you need to (usually you shouldn't).

Example
-------

Expand All @@ -40,8 +50,12 @@ Example

<!-- `/get-in-touch` -> `/contact` -->
<Redirect from="get-in-touch" to="contact" />

<!-- `/profile/123` -> `/about/123` -->
<Redirect from="profile/:userId" to="about-user" />

<!-- `/profile/jasmin` -> `/about-user/123` -->
<Redirect from="profile/jasmin" to="about-user" params={{userId: 123}} />
</Routes>
```

Expand Down
6 changes: 3 additions & 3 deletions modules/components/Redirect.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var React = require('react');
var Route = require('./Route');

function createRedirectHandler(to) {
function createRedirectHandler(to, _params, _query) {
return React.createClass({
statics: {
willTransitionTo: function (transition, params, query) {
transition.redirect(to, params, query);
transition.redirect(to, _params || params, _query || query);
}
},

Expand All @@ -23,7 +23,7 @@ function Redirect(props) {
return Route({
name: props.name,
path: props.from || props.path || '*',
handler: createRedirectHandler(props.to)
handler: createRedirectHandler(props.to, props.params, props.query)
});
}

Expand Down
66 changes: 66 additions & 0 deletions modules/components/__tests__/Redirect-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var assert = require('assert');
var expect = require('expect');
var React = require('react/addons');
var Route = require('../Route');
var Redirect = require('../Redirect');
var Routes = require('../Routes');

describe('a Redirect', function () {

it('redirects from old to new', function (done) {
var descriptor = Redirect({ from: 'old', to: 'new' });

expect(descriptor.props.path).toEqual('old');

var fakeTransition = {
redirect: function(to) {
expect(to).toEqual('new');
done();
}
};

descriptor.props.handler.willTransitionTo(fakeTransition);
});

it('uses params and query from current path', function (done) {
var descriptor = Redirect({ from: 'old', to: 'new' });
var expectedParams = { foo: 'bar' };
var expectedQuery = { baz: 'qux' };

var fakeTransition = {
redirect: function(to, params, query) {
expect(params).toEqual(expectedParams);
expect(query).toEqual(expectedQuery);
done();
}
};

descriptor.props.handler.willTransitionTo(fakeTransition, expectedParams, expectedQuery);
});

it('uses params and query from the Redirect definition', function (done) {
var expectedParams = { foo: 'bar' };
var expectedQuery = { baz: 'qux' };
var fakePathParams = { hooba: 'scooba' };
var fakePathQuery = { doobie: 'scoobie' };

var descriptor = Redirect({
from: 'old',
to: 'new',
params: expectedParams,
query: expectedQuery
});

var fakeTransition = {
redirect: function(to, params, query) {
expect(params).toEqual(expectedParams);
expect(query).toEqual(expectedQuery);
done();
}
};

descriptor.props.handler.willTransitionTo(fakeTransition, fakePathParams, fakePathQuery);
});

});

1 change: 1 addition & 0 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require('./modules/components/__tests__/DefaultRoute-test');
require('./modules/components/__tests__/Link-test');
require('./modules/components/__tests__/NotFoundRoute-test');
require('./modules/components/__tests__/Routes-test');
require('./modules/components/__tests__/Redirect-test');

require('./modules/mixins/__tests__/ActiveContext-test');
require('./modules/mixins/__tests__/LocationContext-test');
Expand Down

0 comments on commit 6723dc5

Please sign in to comment.