|
1 | 1 | import type { AgnosticRouteObject } from '@remix-run/router'; |
2 | 2 | import { describe, expect, it } from 'vitest'; |
3 | | -import { getTransactionName } from '../../src/utils/utils'; |
| 3 | +import { convertRemixRouteIdToPath, getTransactionName } from '../../src/utils/utils'; |
4 | 4 |
|
5 | 5 | describe('getTransactionName', () => { |
6 | 6 | const mockRoutes: AgnosticRouteObject[] = [ |
@@ -74,5 +74,110 @@ describe('getTransactionName', () => { |
74 | 74 | expect(name).toBe('/docs/:*'); |
75 | 75 | expect(source).toBe('route'); |
76 | 76 | }); |
| 77 | + |
| 78 | + it('should handle nested _index routes', () => { |
| 79 | + const routesWithNestedIndex: AgnosticRouteObject[] = [ |
| 80 | + { |
| 81 | + id: 'routes/users._index', |
| 82 | + path: '/users', |
| 83 | + }, |
| 84 | + ]; |
| 85 | + |
| 86 | + const url = new URL('http://localhost/users'); |
| 87 | + const [name, source] = getTransactionName(routesWithNestedIndex, url); |
| 88 | + |
| 89 | + // Should return /users, not /users/_index |
| 90 | + expect(name).toBe('/users'); |
| 91 | + expect(source).toBe('route'); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('convertRemixRouteIdToPath', () => { |
| 97 | + describe('basic routes', () => { |
| 98 | + it('should convert root _index route', () => { |
| 99 | + expect(convertRemixRouteIdToPath('routes/_index')).toBe('/'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should convert root index route', () => { |
| 103 | + expect(convertRemixRouteIdToPath('routes/index')).toBe('/'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should convert static routes', () => { |
| 107 | + expect(convertRemixRouteIdToPath('routes/about')).toBe('/about'); |
| 108 | + expect(convertRemixRouteIdToPath('routes/contact')).toBe('/contact'); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should convert dynamic routes', () => { |
| 112 | + expect(convertRemixRouteIdToPath('routes/user.$id')).toBe('/user/:id'); |
| 113 | + expect(convertRemixRouteIdToPath('routes/blog.$slug')).toBe('/blog/:slug'); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + describe('_index route handling', () => { |
| 118 | + it('should handle nested _index routes (flat file convention)', () => { |
| 119 | + // users._index should map to /users, not /users/_index |
| 120 | + expect(convertRemixRouteIdToPath('routes/users._index')).toBe('/users'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should handle deeply nested _index routes', () => { |
| 124 | + expect(convertRemixRouteIdToPath('routes/admin.settings._index')).toBe('/admin/settings'); |
| 125 | + expect(convertRemixRouteIdToPath('routes/api.v1.users._index')).toBe('/api/v1/users'); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should handle _index with dynamic segments', () => { |
| 129 | + // This represents an index route under a dynamic segment |
| 130 | + expect(convertRemixRouteIdToPath('routes/users.$id._index')).toBe('/users/:id'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should handle _index under pathless layouts', () => { |
| 134 | + // _auth is a pathless layout, _auth._index is its index (maps to /) |
| 135 | + expect(convertRemixRouteIdToPath('routes/_auth._index')).toBe('/'); |
| 136 | + // _dashboard.settings._index maps to /settings (skipping _dashboard) |
| 137 | + expect(convertRemixRouteIdToPath('routes/_dashboard.settings._index')).toBe('/settings'); |
| 138 | + }); |
| 139 | + |
| 140 | + it('should NOT include _index as a path segment', () => { |
| 141 | + const result = convertRemixRouteIdToPath('routes/users._index'); |
| 142 | + expect(result).not.toContain('_index'); |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + describe('index route handling (non-underscore)', () => { |
| 147 | + it('should handle nested index routes', () => { |
| 148 | + expect(convertRemixRouteIdToPath('routes/users.index')).toBe('/users'); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should handle deeply nested index routes', () => { |
| 152 | + expect(convertRemixRouteIdToPath('routes/admin.settings.index')).toBe('/admin/settings'); |
| 153 | + }); |
| 154 | + }); |
| 155 | + |
| 156 | + describe('layout routes', () => { |
| 157 | + it('should skip pathless layout segments', () => { |
| 158 | + expect(convertRemixRouteIdToPath('routes/_auth.login')).toBe('/login'); |
| 159 | + expect(convertRemixRouteIdToPath('routes/_dashboard.settings')).toBe('/settings'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should handle multiple pathless layouts', () => { |
| 163 | + expect(convertRemixRouteIdToPath('routes/_auth._layout.login')).toBe('/login'); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('splat routes', () => { |
| 168 | + it('should convert splat routes', () => { |
| 169 | + expect(convertRemixRouteIdToPath('routes/docs.$')).toBe('/docs/:*'); |
| 170 | + expect(convertRemixRouteIdToPath('routes/$')).toBe('/:*'); |
| 171 | + }); |
| 172 | + }); |
| 173 | + |
| 174 | + describe('complex nested routes', () => { |
| 175 | + it('should handle multiple dynamic segments', () => { |
| 176 | + expect(convertRemixRouteIdToPath('routes/users.$userId.posts.$postId')).toBe('/users/:userId/posts/:postId'); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should handle mixed static and dynamic segments', () => { |
| 180 | + expect(convertRemixRouteIdToPath('routes/api.v1.users.$id.comments')).toBe('/api/v1/users/:id/comments'); |
| 181 | + }); |
77 | 182 | }); |
78 | 183 | }); |
0 commit comments