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

fileRoute.test.ts

75 lines2.1 KB

1/* eslint-disable */
2import { afterEach, describe, it, expect, vi } from 'vitest'
3import {
4 getRouteApi,
5 createFileRoute,
6 createLazyRoute,
7 createLazyFileRoute,
8 LazyRoute,
9 FileRoute,
10} from '../src'
11​
12afterEach(() => {
13 vi.restoreAllMocks()
14})
15​
16describe('createFileRoute', () => {
17 it('creates a non-root route without a deprecation warning', () => {
18 const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
19 // @ts-expect-error
20 const route = createFileRoute('')({})
21​
22 expect(route.isRoot).toBe(false)
23 expect(warn).not.toHaveBeenCalled()
24 })
25​
26 it('keeps the deprecation warning for direct FileRoute usage', () => {
27 const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
28 // @ts-expect-error
29 const route = new FileRoute('').createRoute({})
30​
31 expect(route.isRoot).toBe(false)
32 expect(warn).toHaveBeenCalledOnce()
33 })
34})
35​
36describe('createFileRoute has the same hooks as getRouteApi', () => {
37 const routeApi = getRouteApi('foo')
38 const hookNames = Object.keys(routeApi).filter((key) => key.startsWith('use'))
39 // @ts-expect-error
40 const route = createFileRoute('')({})
41​
42 it.each(hookNames.map((name) => [name]))(
43 'should have the "%s" hook defined',
44 (hookName) => {
45 expect(route[hookName as keyof LazyRoute<any>]).toBeDefined()
46 },
47 )
48})
49​
50describe('createLazyFileRoute has the same hooks as getRouteApi', () => {
51 const routeApi = getRouteApi('foo')
52 const hookNames = Object.keys(routeApi).filter((key) => key.startsWith('use'))
53 // @ts-expect-error
54 const route = createLazyFileRoute('')({})
55​
56 it.each(hookNames.map((name) => [name]))(
57 'should have the "%s" hook defined',
58 (hookName) => {
59 expect(route[hookName as keyof LazyRoute<any>]).toBeDefined()
60 },
61 )
62})
63​
64describe('createLazyRoute has the same hooks as getRouteApi', () => {
65 const routeApi = getRouteApi('foo')
66 const route = createLazyRoute({})({})
67 const hookNames = Object.keys(routeApi).filter((key) => key.startsWith('use'))
68​
69 it.each(hookNames.map((name) => [name]))(
70 'should have the "%s" hook defined',
71 (hookName) => {
72 expect(route[hookName as keyof LazyRoute<any>]).toBeDefined()
73 },
74 )
75})