Skip to content

Commit

Permalink
Updates tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwickens committed Dec 1, 2020
1 parent f268e80 commit 304a365
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 11 deletions.
10 changes: 10 additions & 0 deletions modules/TestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,13 @@ exports.EchoBarParam = React.createClass({
return <div className="EchoBarParam">{this.context.router.getCurrentParams().bar}</div>;
}
});


exports.EchoBarQuery = React.createClass({
contextTypes: {
router: PropTypes.router.isRequired
},
render: function render() {
return <div className="EchoBarQuery">{this.context.router.getCurrentQuery().bar}</div>;
}
});
41 changes: 30 additions & 11 deletions modules/__tests__/PathUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ describe('PathUtils.extractParams', function () {
it('returns an object with the params', function () {
expect(PathUtils.extractParams(pattern, 'comments/abc.js/edit')).toEqual({ id: 'abc', ext: 'js' });
});

it('handles slashes', function () {
expect(PathUtils.extractParams(pattern, 'comments/10%25%20off%20(until%208%2F23).js/edit')).toEqual({ id: '10% off (until 8/23)', ext: 'js' });
});
});

describe('and the pattern is optional', function () {
Expand All @@ -54,6 +58,10 @@ describe('PathUtils.extractParams', function () {
it('returns an object with the params', function () {
expect(PathUtils.extractParams(pattern, 'comments/123/edit')).toEqual({ id: '123' });
});

it('handles slashes', function () {
expect(PathUtils.extractParams(pattern, 'comments/10%25%20off%20(until%208%2F23)/edit')).toEqual({ id: '10% off (until 8/23)' });
});
});

describe('and the path matches without supplied param', function () {
Expand Down Expand Up @@ -213,6 +221,10 @@ describe('PathUtils.injectParams', function () {
expect(PathUtils.injectParams(pattern, { id:'123' })).toEqual('comments/123/edit');
});

it('returns the correct path when param is supplied with special chars', function () {
expect(PathUtils.injectParams(pattern, { id:'10% off (until 8/23)' })).toEqual('comments/10%25%20off%20(until%208%2F23)/edit');
});

it('returns the correct path when param is not supplied', function () {
expect(PathUtils.injectParams(pattern, {})).toEqual('comments//edit');
});
Expand Down Expand Up @@ -242,15 +254,16 @@ describe('PathUtils.injectParams', function () {

describe('and some params have special URL encoding', function () {
it('returns the correct path', function () {
expect(PathUtils.injectParams(pattern, { id: 'one, two' })).toEqual('comments/one, two/edit');
expect(PathUtils.injectParams(pattern, { id: 'one, two' })).toEqual('comments/one%2C%20two/edit');
});
});

describe('and a param has a forward slash', function () {
it('preserves the forward slash', function () {
expect(PathUtils.injectParams(pattern, { id: 'the/id' })).toEqual('comments/the/id/edit');
});
});
// Meadow: We don't support this kind of preserving slashes in params. Everything should be URI encoded.
// describe('and a param has a forward slash', function () {
// it('preserves the forward slash', function () {
// expect(PathUtils.injectParams(pattern, { id: 'the/id' })).toEqual('comments/the/id/edit');
// });
// });

describe('and some params contain dots', function () {
it('returns the correct path', function () {
Expand All @@ -259,11 +272,13 @@ describe('PathUtils.injectParams', function () {
});
});

describe('when a pattern has one splat', function () {
it('returns the correct path', function () {
expect(PathUtils.injectParams('/a/*/d', { splat: 'b/c' })).toEqual('/a/b/c/d');
});
});
// Meadow: We don't support this kind of preserving slashes in params. Everything should be URI encoded.

// describe('when a pattern has one splat', function () {
// it('returns the correct path', function () {
// expect(PathUtils.injectParams('/a/*/d', { splat: 'b/c' })).toEqual('/a/b/c/d');
// });
// });

describe('when a pattern has multiple splats', function () {
it('returns the correct path', function () {
Expand Down Expand Up @@ -303,6 +318,10 @@ describe('PathUtils.extractQuery', function () {
it('properly handles encoded ampersands', function () {
expect(PathUtils.extractQuery('/?id=a%26b')).toEqual({ id: 'a&b' });
});

it('properly handles encoded slashes', function () {
expect(PathUtils.extractQuery('/?id=a%2Fb')).toEqual({ id: 'a/b' });
});
});

describe('when the path does not contain a query string', function () {
Expand Down
28 changes: 28 additions & 0 deletions modules/__tests__/Router-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var {
Nested,
EchoFooProp,
EchoBarParam,
EchoBarQuery,
RedirectToFoo,
RedirectToFooAsync,
Abort,
Expand Down Expand Up @@ -648,6 +649,14 @@ describe('Router', function () {
});
});

it('handles ui encoded params', function (done) {
var routes = <Route handler={EchoFooProp} path='/'/>;
Router.run(routes, '/?foo=10%25%20off%20(until%208%2F23)', function (Handler, state) {
expect(state.query.foo).toEqual('10% off (until 8/23)');
done();
});
});

it('renders with empty query string', function (done) {
var routes = <Route handler={Foo} path='/'/>;
Router.run(routes, '/?', function (Handler, state) {
Expand Down Expand Up @@ -910,6 +919,25 @@ describe('Router.run', function () {
});
});

it('supports dynamic segments with slashes', function (done) {
var routes = <Route handler={EchoBarParam} path='/:bar'/>;
Router.run(routes, '/10%25%20off%20(until%208%2F23)', function (Handler, state) {
var html = React.renderToString(<Handler/>);
expect(html).toMatch(/10% off \(until 8\/23\)/);
done();
});
});


it('supports query params with slashes', function (done) {
var routes = <Route handler={EchoBarQuery} path='/'/>;
Router.run(routes, '/?bar=10%25%20off%20(until%208%2F23)', function (Handler, state) {
var html = React.renderToString(<Handler/>);
expect(html).toMatch(/10% off \(until 8\/23\)/);
done();
});
});

it('supports nested dynamic segments', function (done) {
var routes = (
<Route handler={Nested} path="/:foo">
Expand Down
23 changes: 23 additions & 0 deletions modules/components/__tests__/Link-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ describe('A Link', function () {
});
});
});

it('knows how to make its href (special chars)', function () {
var LinkHandler = React.createClass({
render: function () {
return <Link to="foo" params={{bar: '10% off (until 8/23)'}} query={{qux: 'quux'}}>Link</Link>;
}
});

var routes = [
<Route name="foo" path="foo/:bar" handler={Foo} />,
<Route name="link" handler={LinkHandler} />
];

var div = document.createElement('div');
var location = new TestLocation([ '/link' ]);

Router.run(routes, location, function (Handler) {
React.render(<Handler/>, div, function () {
var a = div.querySelector('a');
expect(a.getAttribute('href')).toEqual('/foo/10%25%20off%20(until%208%2F23)?qux=quux');
});
});
});
});

describe('when its route is active', function () {
Expand Down

0 comments on commit 304a365

Please sign in to comment.