Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Jul 28, 2023
1 parent 9bd746a commit d73c871
Showing 1 changed file with 36 additions and 42 deletions.
78 changes: 36 additions & 42 deletions packages/router/__tests__/createRoutes.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import { describe, it } from 'vitest'
import { describe, it, expect } from 'vitest'
import { z } from 'zod'
import {
createMemoryHistory,
RoutesInfo,
RootRoute,
Route,
ParseRoute,
ParseRouteChildren,
AnyRoute,
Values,
Router,
lazy
} from '../src'
import { createMemoryHistory, RootRoute, Route, Router, lazy } from '../src'

// Write a test
describe('everything', () => {
Expand All @@ -28,6 +17,7 @@ describe('everything', () => {
})
.parse(search),
})

const testRoute = new Route({
getParentRoute: () => rootRoute,
component: lazy(() => import('./TestComponent'), 'NamedComponent'),
Expand All @@ -40,6 +30,7 @@ describe('everything', () => {
})
.parse(search),
})

const dashboardRoute = new Route({
getParentRoute: () => rootRoute,
path: 'dashboard',
Expand All @@ -50,18 +41,22 @@ describe('everything', () => {
}
},
})

const dashboardIndexRoute = new Route({
getParentRoute: () => dashboardRoute,
path: '/',
})

const invoicesRoute = new Route({
getParentRoute: () => dashboardRoute,
path: 'invoices',
})

const invoicesIndexRoute = new Route({
getParentRoute: () => invoicesRoute,
path: '/',
})

const invoiceRoute = new Route({
getParentRoute: () => invoicesRoute,
path: '$invoiceId',
Expand All @@ -76,6 +71,7 @@ describe('everything', () => {
}
},
})

const usersRoute = new Route({
getParentRoute: () => dashboardRoute,
path: 'users',
Expand Down Expand Up @@ -106,6 +102,7 @@ describe('everything', () => {
}),
],
})

const userRoute = new Route({
getParentRoute: () => usersRoute,
path: '$userId',
Expand All @@ -115,14 +112,17 @@ describe('everything', () => {
}
},
})

const authenticatedRoute = new Route({
getParentRoute: () => rootRoute,
path: 'authenticated/', // Trailing slash doesn't mean anything
})

const authenticatedIndexRoute = new Route({
getParentRoute: () => authenticatedRoute,
path: '/',
})

const layoutRoute = new Route({
getParentRoute: () => rootRoute,
id: 'layout',
Expand All @@ -134,11 +134,13 @@ describe('everything', () => {
})
.parse(search),
})

const layoutARoute = new Route({
getParentRoute: () => layoutRoute,
path: 'layout-a',
component: () => 'layout-a',
})

const layoutBRoute = new Route({
getParentRoute: () => layoutRoute,
path: 'layout-b',
Expand All @@ -154,22 +156,9 @@ describe('everything', () => {
usersRoute.addChildren([userRoute]),
]),
authenticatedRoute.addChildren([authenticatedIndexRoute]),
layoutRoute.addChildren([layoutARoute, layoutBRoute])
layoutRoute.addChildren([layoutARoute, layoutBRoute]),
])

type MyRoutesInfo = RoutesInfo<typeof routeTree>

type Test = ParseRouteChildren<typeof routeTree>
// ^?

type MyRootRoute = MyRoutesInfo['routeTree']
type RoutesById = MyRoutesInfo['routesById']
// ^?
type RoutePaths = MyRoutesInfo['routesByFullPath']
// ^?
type InvoiceRoute = RoutesById['/dashboard/invoices/$invoiceId']
// ^?

const router = new Router({
routeTree,
history: createMemoryHistory({
Expand All @@ -178,7 +167,6 @@ describe('everything', () => {
})

const route = router.getRoute('/dashboard/users/$userId')
// ^?

router.buildLink({
to: '/dashboard/users/$userId',
Expand All @@ -192,11 +180,13 @@ describe('everything', () => {
}),
})

// @ts-expect-error
router.buildLink({
from: '/',
to: '/test',
})
expect(() =>
// @ts-expect-error
router.buildLink({
from: '/',
to: '/test',
}),
).toThrowError()

router.buildLink({
from: '/',
Expand All @@ -209,15 +199,19 @@ describe('everything', () => {
},
})

router.buildLink({
from: '/test',
to: '/',
})
expect(() => {
router.buildLink({
from: '/test',
to: '/',
})
}).toThrowError()

router.buildLink({
from: route.id,
to: '',
})
expect(() => {
router.buildLink({
from: route.id,
to: '',
})
}).toThrowError()

router.buildLink({
from: '/dashboard',
Expand Down Expand Up @@ -315,9 +309,9 @@ describe('everything', () => {
to: '/dashboard',
})

// @ts-expect-error
router.buildLink({
from: '/',
// @ts-expect-error
to: '/does-not-exist',
})

Expand Down

0 comments on commit d73c871

Please sign in to comment.