Skip to content

Commit 9b28d79

Browse files
committed
tests
1 parent 73e0637 commit 9b28d79

File tree

40 files changed

+234
-0
lines changed

40 files changed

+234
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// beep
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Ciao
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import path from 'path';
2+
import { describe, expect, test } from 'vitest';
3+
import { createRouteManifest } from '../../../../../src/config/manifest/build-manifest';
4+
5+
describe('catchall', () => {
6+
const manifest = createRouteManifest({ appDirPath: path.join(__dirname, 'app') });
7+
8+
test('should generate a manifest with catchall route', () => {
9+
expect(manifest).toEqual({
10+
dynamic: [
11+
{
12+
path: '/catchall/:path*?',
13+
dynamic: true,
14+
pattern: '^/catchall/(.*)$',
15+
paramNames: ['path'],
16+
},
17+
],
18+
static: [{ path: '/', dynamic: false }],
19+
});
20+
});
21+
22+
test('should generate correct pattern for catchall route', () => {
23+
const regex = new RegExp(manifest.dynamic[0]?.pattern ?? '');
24+
expect(regex.test('/catchall/123')).toBe(true);
25+
expect(regex.test('/catchall/abc')).toBe(true);
26+
expect(regex.test('/catchall/123/456')).toBe(true);
27+
expect(regex.test('/catchall/123/abc/789')).toBe(true);
28+
expect(regex.test('/catchall/')).toBe(true);
29+
expect(regex.test('/123/catchall/123')).toBe(false);
30+
expect(regex.test('/')).toBe(false);
31+
});
32+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// beep
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Ciao
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Hola
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// User profile page
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Post detail page
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// User settings page
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import path from 'path';
2+
import { describe, expect, test } from 'vitest';
3+
import { createRouteManifest } from '../../../../../src/config/manifest/build-manifest';
4+
5+
describe('dynamic', () => {
6+
const manifest = createRouteManifest({ appDirPath: path.join(__dirname, 'app') });
7+
8+
test('should generate a comprehensive dynamic manifest', () => {
9+
expect(manifest).toEqual({
10+
dynamic: [
11+
{
12+
path: '/dynamic/:id',
13+
dynamic: true,
14+
pattern: '^/dynamic/([^/]+)$',
15+
paramNames: ['id'],
16+
},
17+
{
18+
path: '/users/:id',
19+
dynamic: true,
20+
pattern: '^/users/([^/]+)$',
21+
paramNames: ['id'],
22+
},
23+
{
24+
path: '/users/:id/posts/:postId',
25+
dynamic: true,
26+
pattern: '^/users/([^/]+)/posts/([^/]+)$',
27+
paramNames: ['id', 'postId'],
28+
},
29+
{
30+
path: '/users/:id/settings',
31+
dynamic: true,
32+
pattern: '^/users/([^/]+)/settings$',
33+
paramNames: ['id'],
34+
},
35+
],
36+
static: [
37+
{ path: '/', dynamic: false },
38+
{ path: '/static/nested', dynamic: false },
39+
],
40+
});
41+
});
42+
43+
test('should generate correct pattern for single dynamic route', () => {
44+
const singleDynamic = manifest.dynamic.find(route => route.path === '/dynamic/:id');
45+
const regex = new RegExp(singleDynamic?.pattern ?? '');
46+
expect(regex.test('/dynamic/123')).toBe(true);
47+
expect(regex.test('/dynamic/abc')).toBe(true);
48+
expect(regex.test('/dynamic/123/456')).toBe(false);
49+
expect(regex.test('/dynamic123/123')).toBe(false);
50+
expect(regex.test('/')).toBe(false);
51+
});
52+
53+
test('should generate correct pattern for mixed static-dynamic route', () => {
54+
const mixedRoute = manifest.dynamic.find(route => route.path === '/users/:id/settings');
55+
const regex = new RegExp(mixedRoute?.pattern ?? '');
56+
57+
expect(regex.test('/users/123/settings')).toBe(true);
58+
expect(regex.test('/users/john-doe/settings')).toBe(true);
59+
expect(regex.test('/users/123/settings/extra')).toBe(false);
60+
expect(regex.test('/users/123')).toBe(false);
61+
expect(regex.test('/settings')).toBe(false);
62+
});
63+
64+
test('should generate correct pattern for multiple dynamic segments', () => {
65+
const multiDynamic = manifest.dynamic.find(route => route.path === '/users/:id/posts/:postId');
66+
const regex = new RegExp(multiDynamic?.pattern ?? '');
67+
68+
expect(regex.test('/users/123/posts/456')).toBe(true);
69+
expect(regex.test('/users/john/posts/my-post')).toBe(true);
70+
expect(regex.test('/users/123/posts/456/comments')).toBe(false);
71+
expect(regex.test('/users/123/posts')).toBe(false);
72+
expect(regex.test('/users/123')).toBe(false);
73+
74+
const match = '/users/123/posts/456'.match(regex);
75+
expect(match).toBeTruthy();
76+
expect(match?.[1]).toBe('123');
77+
expect(match?.[2]).toBe('456');
78+
});
79+
80+
test('should handle special characters in dynamic segments', () => {
81+
// Test that dynamic segments with special characters work properly
82+
const userSettingsRoute = manifest.dynamic.find(route => route.path === '/users/:id/settings');
83+
expect(userSettingsRoute).toBeDefined();
84+
expect(userSettingsRoute?.pattern).toBeDefined();
85+
86+
const regex = new RegExp(userSettingsRoute!.pattern!);
87+
expect(regex.test('/users/user-with-dashes/settings')).toBe(true);
88+
expect(regex.test('/users/user_with_underscores/settings')).toBe(true);
89+
expect(regex.test('/users/123/settings')).toBe(true);
90+
expect(regex.test('/users/123/settings/extra')).toBe(false);
91+
});
92+
});

0 commit comments

Comments
 (0)