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

useLocation.test-d.tsx

38 lines1.1 KB

1import { expectTypeOf, test } from 'vitest'
2import { createRootRoute, createRoute, createRouter, useLocation } from '../src'
3import type { ParsedLocation } from '../src'
4​
5const rootRoute = createRootRoute()
6​
7const indexRoute = createRoute({
8 getParentRoute: () => rootRoute,
9 path: '/',
10})
11​
12const invoicesRoute = createRoute({
13 getParentRoute: () => rootRoute,
14 path: 'invoices',
15})
16​
17const routeTree = rootRoute.addChildren([invoicesRoute, indexRoute])
18​
19const defaultRouter = createRouter({ routeTree })
20​
21type DefaultRouter = typeof defaultRouter
22​
23test('should have the types for a ParsedLocation in useLocation', () => {
24 const location = useLocation<DefaultRouter>()
25​
26 expectTypeOf(location()).toEqualTypeOf<ParsedLocation>()
27 expectTypeOf(location())
28 .toHaveProperty('pathname')
29 .toEqualTypeOf<ParsedLocation['pathname']>()
30})
31​
32test('should have the type of string for selecting the pathname in useLocation', () => {
33 const pathname = useLocation<DefaultRouter, string>({
34 select: (state) => state.pathname,
35 })()
36​
37 expectTypeOf(pathname).toMatchTypeOf<ParsedLocation['pathname']>()
38})