import { afterEach, expect, test, vi } from 'vitest' import { cleanup, render, screen } from '@testing-library/react' 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('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')) })