Skip to content

Commit ed51909

Browse files
committed
improve testing
1 parent 7229151 commit ed51909

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

packages/hydrogen/src/vite/get-virtual-routes.test.ts

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import {describe, it, expect} from 'vitest';
2-
import {getVirtualRoutesV3} from './get-virtual-routes.js';
2+
import {
3+
getVirtualRoutesV3,
4+
createVirtualRoutesPath,
5+
} from './get-virtual-routes.js';
36

47
describe('virtual routes', () => {
58
it('gets virtual routes V3', async () => {
@@ -30,15 +33,36 @@ describe('virtual routes', () => {
3033
});
3134
});
3235

33-
it('handles file paths with spaces correctly', async () => {
34-
const result = await getVirtualRoutesV3();
36+
it('decodes URL-encoded file paths with spaces', () => {
37+
// Mock import.meta.url with a path containing a space
38+
const mockImportMetaUrl =
39+
'file:///path/with%20space/vite/get-virtual-routes.ts';
3540

36-
result.routes.forEach((route) => {
37-
expect(route.file).not.toContain('%20');
38-
expect(route.file).not.toContain('%');
39-
});
41+
const result = createVirtualRoutesPath(
42+
mockImportMetaUrl,
43+
['vite', 'virtual-routes'],
44+
'layout.jsx',
45+
);
46+
47+
// Should decode %20 to actual space
48+
expect(result).toContain('with space');
49+
expect(result).not.toContain('%20');
50+
});
51+
52+
it('removes Windows drive path prefix (e.g. /C:/)', () => {
53+
// Mock a Windows file URL with C: drive letter
54+
const mockWindowsUrl =
55+
'file:///C:/Users/developer/project/vite/get-virtual-routes.ts';
56+
57+
const result = createVirtualRoutesPath(
58+
mockWindowsUrl,
59+
['vite', 'virtual-routes'],
60+
'layout.jsx',
61+
);
4062

41-
expect(result.layout.file).not.toContain('%20');
42-
expect(result.layout.file).not.toContain('%');
63+
// Should remove /C:/ prefix and normalize to Unix-style path
64+
expect(result).not.toContain('/C:/');
65+
expect(result).toContain('/Users/developer/project');
66+
expect(result).toContain('layout.jsx');
4367
});
4468
});

packages/hydrogen/src/vite/get-virtual-routes.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ export const VIRTUAL_ROUTES_ROUTES_DIR_PARTS = [
77
];
88
export const VIRTUAL_ROUTES_DIR_PARTS = ['vite', 'virtual-routes'];
99

10-
function getVirtualRoutesPath(
10+
/**
11+
* @internal
12+
* Exported for testing only.
13+
*/
14+
export function createVirtualRoutesPath(
15+
baseUrl: string,
1116
pathParts: Array<string>,
1217
forFile: string,
1318
): string {
14-
const basePath = new URL('../', import.meta.url);
19+
const basePath = new URL('../', baseUrl);
1520
const virtualRoutesPath = pathParts.reduce((working, dirPart) => {
1621
return new URL(`${dirPart}/`, working);
1722
}, basePath);
@@ -26,6 +31,13 @@ function getVirtualRoutesPath(
2631
return decodeURIComponent(pathname);
2732
}
2833

34+
function getVirtualRoutesPath(
35+
pathParts: Array<string>,
36+
forFile: string,
37+
): string {
38+
return createVirtualRoutesPath(import.meta.url, pathParts, forFile);
39+
}
40+
2941
export async function getVirtualRoutesV3() {
3042
return {
3143
routes: [

0 commit comments

Comments
 (0)