Skip to content

Commit

Permalink
Fix error calling 'current()' on an unknown route (#362)
Browse files Browse the repository at this point in the history
* Add failing tests

* Default current and route to undefined if no matching route found

* Return type
  • Loading branch information
bakerkretzmar authored Nov 13, 2020
1 parent 865333a commit e2a9914
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/js/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class Router extends String {
*
* @param {String} name - Route name to check.
* @param {(String|Number|Array|Object)} params - Route parameters.
* @return {(Boolean|String)}
* @return {(Boolean|String|undefined)}
*/
current(name, params) {
const url = this._config.absolute
Expand All @@ -76,7 +76,7 @@ export default class Router extends String {
// Find the first route that matches the current URL
const [current, route] = Object.entries(this._config.routes).find(
([_, route]) => new Route(name, route, this._config).matchesUrl(url)
);
) || [undefined, undefined];

// If a name wasn't passed, return the name of the current route
if (!name) return current;
Expand Down
12 changes: 12 additions & 0 deletions tests/js/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,18 @@ describe('current()', () => {
same(route(undefined, undefined, undefined, config).current(), 'events.index');
});

test('can return undefined when getting the current route name on an unknown route', () => {
global.window.location.pathname = '/unknown/';

same(route().current(), undefined);
});

test('can return false when checking the current route name on an unknown route', () => {
global.window.location.pathname = '/unknown/';

same(route().current('posts.delete'), false);
});

test('can check the current route name against a pattern', () => {
global.window.location.pathname = '/events/1/venues/2';

Expand Down

0 comments on commit e2a9914

Please sign in to comment.