Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

utils.test.ts

405 lines11.9 KB

1import { describe, expect, it, vi } from 'vitest'
2import {
3 cleanPath,
4 determineInitialRoutePath,
5 isValidNonNestedRoute,
6 mergeImportDeclarations,
7 multiSortBy,
8 removeExt,
9 removeLeadingUnderscores,
10 removeTrailingUnderscores,
11 removeUnderscores,
12 routePathToVariable,
13} from '../src/utils'
14import type { ImportDeclaration } from '../src/types'
15​
16describe('cleanPath', () => {
17 it('keeps path with leading slash and trailing slash', () => {
18 expect(cleanPath('/test/')).toBe('/test/')
19 })
20})
21​
22describe.each([
23 { nonNested: true, mode: 'experimental nonNestedPaths' },
24 { nonNested: false, mode: 'default' },
25])('determineInitialRoutePath - $mode', ({ nonNested }) => {
26 const config = {
27 experimental: {
28 nonNestedRoutes: nonNested,
29 },
30 routeToken: 'route',
31 indexToken: 'index',
32 }
33​
34 it('removes dots and adds slashes', () => {
35 expect(determineInitialRoutePath('test.test', config)).toStrictEqual({
36 routePath: '/test/test',
37 isExperimentalNonNestedRoute: false,
38 originalRoutePath: '/test/test',
39 })
40 })
41​
42 it('keeps leading slash', () => {
43 expect(determineInitialRoutePath('/test.test', config)).toStrictEqual({
44 routePath: '/test/test',
45 isExperimentalNonNestedRoute: false,
46 originalRoutePath: '/test/test',
47 })
48 })
49​
50 it('keeps trailing slash', () => {
51 expect(determineInitialRoutePath('test.test/', config)).toStrictEqual({
52 routePath: '/test/test/',
53 isExperimentalNonNestedRoute: false,
54 originalRoutePath: '/test/test/',
55 })
56 })
57​
58 it('removes dots and adds slashes with leading and trailing slashes', () => {
59 expect(determineInitialRoutePath('/test.test/', config)).toStrictEqual({
60 routePath: '/test/test/',
61 isExperimentalNonNestedRoute: false,
62 originalRoutePath: '/test/test/',
63 })
64 })
65​
66 it("returns '/' if path is empty", () => {
67 expect(determineInitialRoutePath('', config)).toStrictEqual({
68 routePath: '/',
69 isExperimentalNonNestedRoute: false,
70 originalRoutePath: '/',
71 })
72 })
73​
74 it("returns '/' if path is '.'", () => {
75 expect(determineInitialRoutePath('.', config)).toStrictEqual({
76 routePath: '/',
77 isExperimentalNonNestedRoute: false,
78 originalRoutePath: '/',
79 })
80 })
81​
82 it("returns '/' if path is './'", () => {
83 expect(determineInitialRoutePath('./', config)).toStrictEqual({
84 routePath: '/',
85 isExperimentalNonNestedRoute: false,
86 originalRoutePath: '/',
87 })
88 })
89​
90 it('errors on disallowed escaped character', () => {
91 const consoleSpy = vi.spyOn(console, 'error')
92​
93 expect(() => determineInitialRoutePath('/a[/]', config)).toThrowError()
94​
95 expect(consoleSpy).toBeCalledWith(
96 'Error: Disallowed character "/" found in square brackets in route path "/a[/]".\n' +
97 'You cannot use any of the following characters in square brackets: /, \\, ?, #, :, *, <, >, |, !, $, %\n' +
98 'Please remove and/or replace them.',
99 )
100​
101 consoleSpy.mockRestore()
102 })
103​
104 it('escapes characters correctly', () => {
105 expect(determineInitialRoutePath('/a[.]', config)).toStrictEqual({
106 routePath: '/a.',
107 isExperimentalNonNestedRoute: false,
108 originalRoutePath: '/a[.]',
109 })
110​
111 expect(determineInitialRoutePath('/a[_]', config)).toStrictEqual({
112 routePath: '/a_',
113 isExperimentalNonNestedRoute: false,
114 originalRoutePath: '/a[_]',
115 })
116 })
117​
118 // this is changed with experimental non-nested paths.
119 // currently trailing underscores are not removed
120 // with experimental non-nested paths this is removed to allow escaped '_' to be processed correctly later
121 it('should handle trailing underscores correctly', () => {
122 expect(determineInitialRoutePath('a_', config)).toStrictEqual({
123 routePath: `/a${nonNested ? '' : '_'}`,
124 isExperimentalNonNestedRoute: nonNested,
125 originalRoutePath: '/a_',
126 })
127​
128 expect(determineInitialRoutePath('a_.route', config)).toStrictEqual({
129 routePath: `/a${nonNested ? '' : '_'}/route`,
130 isExperimentalNonNestedRoute: nonNested,
131 originalRoutePath: '/a_/route',
132 })
133​
134 expect(determineInitialRoutePath('a_.b.c', config)).toStrictEqual({
135 routePath: `/a${nonNested ? '' : '_'}/b/c`,
136 isExperimentalNonNestedRoute: nonNested,
137 originalRoutePath: '/a_/b/c',
138 })
139​
140 expect(determineInitialRoutePath('a.b_.c.d', config)).toStrictEqual({
141 routePath: `/a/b${nonNested ? '' : '_'}/c/d`,
142 isExperimentalNonNestedRoute: nonNested,
143 originalRoutePath: '/a/b_/c/d',
144 })
145​
146 expect(determineInitialRoutePath('a_.route.b', config)).toStrictEqual({
147 routePath: `/a${nonNested ? '' : '_'}/route/b`,
148 isExperimentalNonNestedRoute: false,
149 originalRoutePath: '/a_/route/b',
150 })
151​
152 expect(
153 determineInitialRoutePath('/a_/_route_/b_/c/d[_]', {
154 ...config,
155 routeToken: 'route',
156 }),
157 ).toStrictEqual({
158 routePath: `/a${nonNested ? '' : '_'}/_route${nonNested ? '' : '_'}/b${nonNested ? '' : '_'}/c/d_`,
159 isExperimentalNonNestedRoute: nonNested,
160 originalRoutePath: '/a_/_route_/b_/c/d[_]',
161 })
162​
163 expect(
164 determineInitialRoutePath('/a_/_route_/b_/c/d[_]', {
165 ...config,
166 routeToken: '_route_',
167 }),
168 ).toStrictEqual({
169 routePath: `/a${nonNested ? '' : '_'}/_route_/b${nonNested ? '' : '_'}/c/d_`,
170 isExperimentalNonNestedRoute: nonNested,
171 originalRoutePath: '/a_/_route_/b_/c/d[_]',
172 })
173 })
174})
175​
176describe('multiSortBy', () => {
177 it('sorts by multiple criteria', () => {
178 const data = [
179 { routePath: '/test/1/2/index', f: 'b' },
180 { routePath: '/test/1', f: 'b' },
181 { routePath: '/test/1/2/3/4/index', f: 'b' },
182 { routePath: '/test/1/2/3', f: 'b' },
183 { routePath: '/test/1/2/3/index', f: 'b' },
184 { routePath: '/test/1/2', f: 'b' },
185 { routePath: '/test/1/2/3/4', f: 'b' },
186 ]
187​
188 const sorted = multiSortBy(data, [
189 (d) => (d.routePath.includes('1') ? -1 : 1),
190 (d) => d.routePath.split('/').length,
191 (d) => (d.routePath.endsWith('index') ? -1 : 1),
192 (d) => d,
193 ])
194​
195 expect(sorted).toEqual([
196 { routePath: '/test/1', f: 'b' },
197 { routePath: '/test/1/2', f: 'b' },
198 { routePath: '/test/1/2/index', f: 'b' },
199 { routePath: '/test/1/2/3', f: 'b' },
200 { routePath: '/test/1/2/3/index', f: 'b' },
201 { routePath: '/test/1/2/3/4', f: 'b' },
202 { routePath: '/test/1/2/3/4/index', f: 'b' },
203 ])
204 })
205})
206​
207describe('removeExt', () => {
208 it('removes extension', () => {
209 expect(removeExt('test.ts')).toBe('test')
210 })
211​
212 it('does not remove extension if no extension', () => {
213 expect(removeExt('test')).toBe('test')
214 })
215​
216 it('removes extension with multiple dots', () => {
217 expect(removeExt('test.test.ts')).toBe('test.test')
218 })
219​
220 it('removes extension with leading dot', () => {
221 expect(removeExt('.test.ts')).toBe('.test')
222 })
223​
224 it('removes extension when in a route path', () => {
225 expect(removeExt('/test/test.ts')).toBe('/test/test')
226 })
227})
228​
229describe('removeUnderscores', () => {
230 it('removes leading underscore', () => {
231 expect(removeUnderscores('_test')).toBe('test')
232 })
233​
234 it('removes trailing underscore', () => {
235 expect(removeUnderscores('test_')).toBe('test')
236 })
237​
238 it('removes leading and trailing underscores', () => {
239 expect(removeUnderscores('_test_')).toBe('test')
240 })
241})
242​
243describe('removeLeadingUnderscores', () => {
244 it('removes leading underscore when not routeToken', () => {
245 expect(removeLeadingUnderscores('_test', 'route')).toBe('test')
246​
247 expect(removeLeadingUnderscores('/_test/abc/route/_d', 'route')).toBe(
248 '/test/abc/route/d',
249 )
250​
251 expect(removeLeadingUnderscores('/_test_/abc/_route_/d_/_e', 'route')).toBe(
252 '/test_/abc/route_/d_/e',
253 )
254​
255 expect(
256 removeLeadingUnderscores('/_test_/abc/_route_/d_/_e', '_route_'),
257 ).toBe('/test_/abc/_route_/d_/e')
258​
259 expect(
260 removeLeadingUnderscores('/_test_/abc/_route_/d_/_e', 'route_'),
261 ).toBe('/test_/abc/route_/d_/e')
262 })
263})
264​
265describe('removeTrailingUnderscores', () => {
266 it('removes trailing underscore when not routeToken', () => {
267 expect(removeTrailingUnderscores('test_', 'route')).toBe('test')
268​
269 expect(removeTrailingUnderscores('/_test_/abc_/route/_d', 'route')).toBe(
270 '/_test/abc/route/_d',
271 )
272​
273 expect(
274 removeTrailingUnderscores('/_test_/abc/_route_/d_/_e', 'route'),
275 ).toBe('/_test/abc/_route/d/_e')
276​
277 expect(
278 removeTrailingUnderscores('/_test_/abc/_route_/d_/_e', '_route_'),
279 ).toBe('/_test/abc/_route_/d/_e')
280​
281 expect(
282 removeTrailingUnderscores('/_test_/abc/_route_/d_/_e', '_route'),
283 ).toBe('/_test/abc/_route/d/_e')
284 })
285})
286​
287describe('routePathToVariable', () => {
288 it.each([
289 ['/test/$/index', 'TestSplatIndex'],
290 ['/test/$', 'TestSplat'],
291 ['/test/$/', 'TestSplat'],
292 ['/test/index', 'TestIndex'],
293 ['/test', 'Test'],
294 ['/test/', 'Test'],
295 ['/r0ut3', 'R0ut3'],
296 ])(`converts "%s" to "%s"`, (routePath, expected) => {
297 expect(routePathToVariable(routePath)).toBe(expected)
298 })
299})
300​
301describe('mergeImportDeclarations', () => {
302 it('merges imports with the same source but different specifiers', () => {
303 const imports: Array<ImportDeclaration> = [
304 { source: 'moduleA', specifiers: [{ imported: 'A' }] },
305 { source: 'moduleA', specifiers: [{ imported: 'B' }] },
306 ]
307​
308 const result = mergeImportDeclarations(imports)
309​
310 expect(result).toEqual([
311 {
312 source: 'moduleA',
313 specifiers: [{ imported: 'A' }, { imported: 'B' }],
314 },
315 ])
316 })
317​
318 it('merges imports with overlapping specifiers', () => {
319 const imports: Array<ImportDeclaration> = [
320 { source: 'moduleA', specifiers: [{ imported: 'A' }] },
321 { source: 'moduleA', specifiers: [{ imported: 'A' }, { imported: 'B' }] },
322 ]
323​
324 const result = mergeImportDeclarations(imports)
325​
326 expect(result).toEqual([
327 {
328 source: 'moduleA',
329 specifiers: [{ imported: 'A' }, { imported: 'B' }],
330 },
331 ])
332 })
333​
334 it('does not merge imports with mixed import kinds for the same source', () => {
335 const imports: Array<ImportDeclaration> = [
336 {
337 source: 'moduleA',
338 importKind: 'type',
339 specifiers: [{ imported: 'A' }],
340 },
341 { source: 'moduleA', specifiers: [{ imported: 'B' }] },
342 ]
343​
344 const result = mergeImportDeclarations(imports)
345​
346 expect(result).toEqual([
347 {
348 source: 'moduleA',
349 importKind: 'type',
350 specifiers: [{ imported: 'A' }],
351 },
352 { source: 'moduleA', specifiers: [{ imported: 'B' }] },
353 ])
354 })
355​
356 it('removes duplicate specifiers', () => {
357 const imports: Array<ImportDeclaration> = [
358 { source: 'moduleA', specifiers: [{ imported: 'A' }] },
359 { source: 'moduleA', specifiers: [{ imported: 'A' }] },
360 ]
361​
362 const result = mergeImportDeclarations(imports)
363​
364 expect(result).toEqual([
365 {
366 source: 'moduleA',
367 specifiers: [{ imported: 'A' }],
368 },
369 ])
370 })
371})
372​
373describe('isValidNonNestedRoute', () => {
374 const config = {
375 experimental: {
376 nonNestedRoutes: true,
377 },
378 routeToken: 'route',
379 indexToken: 'index',
380 }
381​
382 it('should identify valid non-nested routes', () => {
383 expect(isValidNonNestedRoute('/a_', config)).toBe(true)
384 expect(isValidNonNestedRoute('/a/b_', config)).toBe(true)
385 expect(isValidNonNestedRoute('/a_/route', config)).toBe(true)
386 expect(isValidNonNestedRoute('/a/route/b_', config)).toBe(true)
387 expect(isValidNonNestedRoute('/a_/b', config)).toBe(true)
388 })
389​
390 it('should identify invalid non-nested routes', () => {
391 expect(isValidNonNestedRoute('/a', config)).toBe(false)
392 expect(isValidNonNestedRoute('/a/b', config)).toBe(false)
393 expect(isValidNonNestedRoute('/a/route/false', config)).toBe(false)
394 expect(isValidNonNestedRoute('/a_/route/b', config)).toBe(false)
395 })
396​
397 it('should return false if not enabled', () => {
398 expect(
399 isValidNonNestedRoute('/a_', {
400 ...config,
401 experimental: { nonNestedRoutes: false },
402 }),
403 ).toBe(false)
404 })
405})