Skip to content

Commit

Permalink
feat: cover / as a route with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleh Momot committed Dec 28, 2020
1 parent 6dbbb96 commit e6eb5f6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
8 changes: 5 additions & 3 deletions ilc/common/router/Router.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ module.exports = class Router {

routeExp = new RegExp(`^(${basePath})/?.*`);
} else {
const basePath = v.route[v.route.length - 1] === '/'
? route.substring(0, route.length - 1)
: route;
let basePath = route;

if (v.route.length > 1 && v.route[v.route.length - 1] === '/') {
basePath = route.substring(0, route.length - 1);
};

routeExp = new RegExp(`^(${basePath})/?$`);
}
Expand Down
38 changes: 38 additions & 0 deletions ilc/common/router/Router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,44 @@ describe('router', () => {
});
});

it('should return a matched route when a requested url is `/`', () => {
const routeThatEqualsTrailingSlash = Object.freeze({
routeId: 'homeRoute',
route: '/',
next: false,
template: 'homeTemplate',
slots: {
home: {
appName: 'home',
props: {
firstHomeSlotProp: 'firstHomeSlotProp',
secondHomeSlotProp: 'secondHomeSlotProp',
},
kind: 'primary',
},
},
});
const registryConfigThatHasRouteThatEqualsTrailingSlash = Object.freeze({
...registryConfig,
routes: [
routeThatEqualsTrailingSlash,
...registryConfig.routes,
],
});
const router = new Router(registryConfigThatHasRouteThatEqualsTrailingSlash);
const reqUrl = '/';

chai.expect(router.match(reqUrl)).to.be.eql({
routeId: 'homeRoute',
route: '/',
basePath: '/',
reqUrl,
template: 'homeTemplate',
specialRole: null,
slots: routeThatEqualsTrailingSlash.slots,
});
});

it('should return 404 route when a router does not match a route by a requested url that has something after `/`', () => {
const router = new Router(registryConfigWithRouteThatHasTrailingSlashAtTheEnd);
const reqUrl = '/launchpad/something';
Expand Down

0 comments on commit e6eb5f6

Please sign in to comment.