Skip to content

Commit

Permalink
refactor(route): consider index as / (#656)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranet authored Nov 5, 2024
1 parent 0782cfd commit 3e51fea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/api/src/lib/structures/router/RouterRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export class RouterRoot extends RouterBranch {
parts.push(trimmed);
}

parts.push(name.trim());
if (name !== 'index') {
parts.push(name.trim());
}

return parts.join('/');
}

Expand Down
37 changes: 37 additions & 0 deletions packages/api/tests/lib/structures/router/RouterRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,41 @@ describe('RouterBranch', () => {
expect(value).toBe('GET');
});
});

describe('makeRoutePathForPiece', () => {
test('GIVEN empty directories and index file THEN it should return empty path', () => {
const value = RouterRoot.makeRoutePathForPiece([], 'index');
expect(value).toBe('');
});

test('GIVEN empty directories and non-index file THEN it should return the name', () => {
const value = RouterRoot.makeRoutePathForPiece([], 'users');
expect(value).toBe('users');
});

test('GIVEN non-empty directories and index file THEN it should return the validated directories', () => {
const value = RouterRoot.makeRoutePathForPiece(['testing'], 'index');
expect(value).toBe('testing');
});

test('GIVEN non-empty directories and non-index file THEN it should return the validated directories joined with the name', () => {
const value = RouterRoot.makeRoutePathForPiece(['testing'], 'users');
expect(value).toBe('testing/users');
});

test('GIVEN a group directory THEN it is ignored', () => {
const value = RouterRoot.makeRoutePathForPiece(['(users)'], 'index');
expect(value).toBe('');
});

test('GIVEN a name with group directory syntax THEN it is used-as-is', () => {
const value = RouterRoot.makeRoutePathForPiece([], '(users)');
expect(value).toBe('(users)');
});

test('GIVEN an empty or whitespace-only directory name THEN it is ignored', () => {
const value = RouterRoot.makeRoutePathForPiece([' '], 'index');
expect(value).toBe('');
});
});
});

0 comments on commit 3e51fea

Please sign in to comment.