import { afterEach, expect, test, vi } from 'vitest'
import { cleanup, render, screen } from '@testing-library/vue'
import { createMemoryHistory } from '@tanstack/history'
import { createControlledPromise, notFound } from '@tanstack/router-core'
import {
Outlet,
RouterProvider,
createRootRoute,
createRoute,
createRouter,
} from '../src'
const outletWarning = (
component: 'pendingComponent' | 'errorComponent' | 'notFoundComponent',
) =>
`Warning: An was rendered inside a ${component}. should only be rendered inside a route component.`
afterEach(() => {
cleanup()
vi.restoreAllMocks()
})
test('does not warn when Outlet is rendered inside a route component', async () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const rootRoute = createRootRoute({
component: () => (
<>
Root route
>
),
})
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => Index route,
})
const router = createRouter({
routeTree: rootRoute.addChildren([indexRoute]),
history: createMemoryHistory({ initialEntries: ['/'] }),
})
render()
expect(await screen.findByText('Index route')).toBeInTheDocument()
expect(warn).not.toHaveBeenCalled()
})
test('warns when Outlet is rendered inside a pendingComponent', async () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const pending = createControlledPromise()
const rootRoute = createRootRoute({ component: Outlet })
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => Index route,
})
const pendingRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/pending',
loader: () => pending,
pendingMs: 0,
pendingComponent: () => (
<>
Pending route
>
),
component: () => Resolved route,
})
const router = createRouter({
routeTree: rootRoute.addChildren([indexRoute, pendingRoute]),
history: createMemoryHistory({ initialEntries: ['/'] }),
})
render()
await screen.findByText('Index route')
const navigation = router.navigate({ to: '/pending' })
expect(await screen.findByText('Pending route')).toBeInTheDocument()
pending.resolve()
await navigation
expect(warn).toHaveBeenCalledWith(outletWarning('pendingComponent'))
})
test('warns when Outlet is rendered inside an errorComponent', async () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const rootRoute = createRootRoute({ component: Outlet })
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
loader: () => {
throw new Error('Loader failed')
},
errorComponent: () => (
<>
Error route
>
),
})
const router = createRouter({
routeTree: rootRoute.addChildren([indexRoute]),
history: createMemoryHistory({ initialEntries: ['/'] }),
})
render()
expect(await screen.findByText('Error route')).toBeInTheDocument()
expect(warn).toHaveBeenCalledWith(outletWarning('errorComponent'))
})
test.each([new Error('Loader failed'), false, 0, '', null, undefined])(
'warns with the current component after a fallback transition with %s',
async (error) => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const pending = createControlledPromise()
const FallbackComponent = (props: { error?: unknown }) => (
<>
{'error' in props ? 'Error route' : 'Pending route'}
>
)
const rootRoute = createRootRoute({ component: Outlet })
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => Index route,
})
const transitionRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/transition',
loader: () => pending,
pendingMs: 0,
pendingComponent: FallbackComponent,
errorComponent: FallbackComponent,
})
const router = createRouter({
routeTree: rootRoute.addChildren([indexRoute, transitionRoute]),
history: createMemoryHistory({ initialEntries: ['/'] }),
})
render()
await screen.findByText('Index route')
const navigation = router.navigate({ to: '/transition' })
expect(await screen.findByText('Pending route')).toBeInTheDocument()
pending.reject(error)
await navigation
expect(await screen.findByText('Error route')).toBeInTheDocument()
expect(warn).toHaveBeenCalledWith(outletWarning('pendingComponent'))
expect(warn).toHaveBeenCalledWith(outletWarning('errorComponent'))
},
)
test('warns when Outlet is rendered inside a notFoundComponent', async () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
const rootRoute = createRootRoute({ component: Outlet })
const indexRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/',
component: () => Index route,
})
const notFoundRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/not-found',
component: () => {
throw notFound()
},
notFoundComponent: () => (
<>
Not found route
>
),
})
const router = createRouter({
routeTree: rootRoute.addChildren([indexRoute, notFoundRoute]),
history: createMemoryHistory({ initialEntries: ['/'] }),
})
render()
await screen.findByText('Index route')
await router.navigate({ to: '/not-found' })
expect(await screen.findByText('Not found route')).toBeInTheDocument()
expect(warn).toHaveBeenCalledWith(outletWarning('notFoundComponent'))
})