Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Add ability to handle child route with parameter #286

Merged
merged 2 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/sky-pages-route-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,12 @@ function parseRoute(route) {

const routeTokens = route.routePath.split('/');
const lastToken = routeTokens[routeTokens.length - 1];
const secondLastToken = routeTokens[routeTokens.length - 2];

// if it begins with #, that indicates we should create a child route
if (lastToken.startsWith('#')) {
if (lastToken.startsWith('#') ||
(secondLastToken && secondLastToken.startsWith('#') && lastToken.startsWith(':'))
) {
const reversedTokens = routeTokens.slice().reverse();
const childTokens = [lastToken];
for (let i = 1; i < reversedTokens.length; i++) {
Expand All @@ -187,7 +190,7 @@ function parseRoute(route) {
let currentRoute = result;
childTokens.reverse().forEach(token => {
let childToken = {
routePath: token.substring(1),
routePath: token.startsWith('#') ? token.substring(1) : token,
children: []
};

Expand Down
18 changes: 18 additions & 0 deletions test/sky-pages-route-generator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,24 @@ describe('SKY UX Builder route generator', () => {
);
});

it('should handle child routes with parameter', () => {
spyOn(glob, 'sync').and.callFake(() => ['my-custom-src/#my-custom-route/_custom/index.html']);
spyOn(path, 'join').and.returnValue('');
spyOn(fs, 'readFileSync').and.returnValue('');
spyOn(fs, 'existsSync').and.returnValue(true);

const routes = generator.getRoutes({
runtime: {
srcPath: ''
}
});

expect(routes.declarations).toContain(`path: 'my-custom-src'`);
expect(routes.declarations).toContain(`path: 'my-custom-route'`);
expect(routes.declarations).toContain(`path: ':custom'`);
expect(routes.definitions).toContain(`this.custom = params['custom'];`);
});

it('should handle top-level routes within a child route', () => {
spyOn(glob, 'sync').and.callFake(() => ['my-custom-src/#my-custom-route/top-level/index.html']);
spyOn(path, 'join').and.returnValue('');
Expand Down