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

route-initialization.test.ts

60 lines2.0 KB

1import { afterEach, expect, test, vi } from 'vitest'
2import { BaseRootRoute, BaseRoute } from '../src'
3import { processRouteTree } from '../src/new-process-route-tree'
4import type { AnyRoute } from '../src'
5​
6afterEach(() => vi.restoreAllMocks())
7​
8test('initializes routes parent-first before reading their IDs and paths', () => {
9 const root = new BaseRootRoute({})
10 const layout = new BaseRoute({
11 getParentRoute: () => root,
12 path: '/layout',
13 })
14 const child = new BaseRoute({
15 getParentRoute: () => layout,
16 path: '/$id',
17 })
18 const sibling = new BaseRoute({
19 getParentRoute: () => root,
20 path: '/sibling',
21 })
22 const routes = [root, layout, child, sibling]
23 const calls = routes.map((route) => vi.spyOn(route, 'init'))
24 const { routesById } = processRouteTree<AnyRoute>(
25 root.addChildren([layout.addChildren([child]), sibling]),
26 )
27 for (const [originalIndex, init] of calls.entries()) {
28 expect(init).toHaveBeenCalledExactlyOnceWith(originalIndex)
29 expect(routes[originalIndex]!.originalIndex).toBe(originalIndex)
30 }
31 const order = calls.map((init) => init.mock.invocationCallOrder[0]!)
32 expect(order).toEqual([...order].sort((a, b) => a - b))
33 expect(root.id).toBe('__root__')
34 expect(child.fullPath).toBe('/layout/$id')
35 for (const route of routes) {
36 expect(routesById[route.id]).toBe(route)
37 }
38})
39​
40test('normalizes nested route IDs without changing pathless layout paths', () => {
41 const root = new BaseRootRoute({})
42 const layout = new BaseRoute({
43 getParentRoute: () => root,
44 id: '//_layout//',
45 })
46 const item = new BaseRoute({
47 getParentRoute: () => layout,
48 path: '//items//$id//',
49 })
50 const { routesById } = processRouteTree<AnyRoute>(
51 root.addChildren([layout.addChildren([item])]),
52 )
53 expect(root.id).toBe('__root__')
54 expect(layout.id).toBe('/_layout/')
55 expect(layout.fullPath).toBe('/')
56 expect(item.id).toBe('/_layout/items/$id/')
57 expect(item.fullPath).toBe('/items/$id/')
58 expect(item.to).toBe('/items/$id')
59 expect(routesById[item.id]).toBe(item)
60})