Skip to content

Commit debc432

Browse files
committed
test: add base path test case about create route manifest
1 parent dbbf241 commit debc432

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// about page
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// API test page
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// root page
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// users id dynamic page
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import path from 'path';
2+
import { describe, expect, test } from 'vitest';
3+
import { createRouteManifest } from '../../../../../src/config/manifest/createRouteManifest';
4+
5+
describe('basePath', () => {
6+
test('should generate routes with base path prefix', () => {
7+
const manifest = createRouteManifest({
8+
basePath: '/my-app',
9+
appDirPath: path.join(__dirname, 'app')
10+
});
11+
12+
expect(manifest).toEqual({
13+
staticRoutes: [
14+
{ path: '/my-app' },
15+
{ path: '/my-app/about' },
16+
{ path: '/my-app/api/test' }
17+
],
18+
dynamicRoutes: [
19+
{
20+
path: '/my-app/users/:id',
21+
regex: '^/my-app/users/([^/]+)$',
22+
paramNames: ['id'],
23+
}
24+
],
25+
});
26+
});
27+
28+
test('should validate dynamic route regex with base path', () => {
29+
const manifest = createRouteManifest({
30+
basePath: '/my-app',
31+
appDirPath: path.join(__dirname, 'app')
32+
});
33+
34+
const dynamicRoute = manifest.dynamicRoutes.find(route => route.path === '/my-app/users/:id');
35+
const regex = new RegExp(dynamicRoute?.regex ?? '');
36+
37+
// Should match valid paths with base path
38+
expect(regex.test('/my-app/users/123')).toBe(true);
39+
expect(regex.test('/my-app/users/john-doe')).toBe(true);
40+
41+
// Should not match paths without base path
42+
expect(regex.test('/users/123')).toBe(false);
43+
44+
// Should not match invalid paths
45+
expect(regex.test('/my-app/users/')).toBe(false);
46+
expect(regex.test('/my-app/users/123/extra')).toBe(false);
47+
expect(regex.test('/my-app/user/123')).toBe(false);
48+
});
49+
});

0 commit comments

Comments
 (0)