import { defineComponent } from 'vue' import { afterEach, beforeEach, expect, test } from 'vitest' import { cleanup, render, screen } from '@testing-library/vue' import { Link, Outlet, RouterProvider, createBrowserHistory, createRootRoute, createRoute, createRouter, notFound, rootRouteId, } from '../src' import type { NotFoundRouteProps, RouterHistory } from '../src' let history: RouterHistory beforeEach(() => { history = createBrowserHistory() expect(window.location.pathname).toBe('/') }) afterEach(() => { history.destroy() window.history.replaceState(null, 'root', '/') cleanup() }) test.each([ { notFoundMode: 'fuzzy' as const, expectedNotFoundComponent: 'settings-not-found', }, { notFoundMode: 'root' as const, expectedNotFoundComponent: 'root-not-found', }, ])( 'correct notFoundComponent is rendered for mode=%s', async ({ notFoundMode, expectedNotFoundComponent }) => { const rootRoute = createRootRoute({ component: () => (

Root Component

link to settings {' '} link to non-existing route
), notFoundComponent: () => ( Root Not Found Component ), }) const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: () => (

Index Page

), }) const settingsRoute = createRoute({ getParentRoute: () => rootRoute, path: '/settings', notFoundComponent: () => ( Settings Not Found Component ), component: () => (

Settings Page Layout

), }) const settingsIndexRoute = createRoute({ getParentRoute: () => settingsRoute, path: '/', component: () => (
Settings Page
), }) const router = createRouter({ routeTree: rootRoute.addChildren([ indexRoute, settingsRoute.addChildren([settingsIndexRoute]), ]), history, notFoundMode, }) render() await router.load() await screen.findByTestId('root-component') const settingsLink = screen.getByTestId('settings-link') settingsLink.click() const settingsIndexComponent = await screen.findByTestId( 'settings-index-component', ) expect(settingsIndexComponent).toBeInTheDocument() const nonExistingLink = screen.getByTestId('non-existing-link') nonExistingLink.click() const notFoundComponent = await screen.findByTestId( expectedNotFoundComponent, {}, { timeout: 1000 }, ) expect(notFoundComponent).toBeInTheDocument() }, ) test('defaultNotFoundComponent and notFoundComponent receives data props via spread operator', async () => { const isCustomData = (data: unknown): data is typeof customData => { return 'message' in (data as typeof customData) } const customData = { message: 'Custom not found message', } const DefaultNotFoundComponentWithProps = (props: NotFoundRouteProps) => (
{isCustomData(props.data) && {props.data.message}}
) const rootRoute = createRootRoute({ component: () => (

Root Component

link to default not found route link to not found route
), }) const indexRoute = createRoute({ getParentRoute: () => rootRoute, path: '/', component: () => (

Index Page

), }) const defaultNotFoundRoute = createRoute({ getParentRoute: () => rootRoute, path: '/default-not-found-route', loader: () => { throw notFound({ data: customData }) }, component: () => (
Should not render
), }) const notFoundRoute = createRoute({ getParentRoute: () => rootRoute, path: '/not-found-route', loader: () => { throw notFound({ data: customData }) }, component: () => (
Should not render
), notFoundComponent: (props) => (
{isCustomData(props.data) && {props.data.message}}
), }) const router = createRouter({ routeTree: rootRoute.addChildren([ indexRoute, defaultNotFoundRoute, notFoundRoute, ]), history, defaultNotFoundComponent: DefaultNotFoundComponentWithProps, }) render() await router.load() await screen.findByTestId('root-component') const defaultNotFoundRouteLink = screen.getByTestId( 'default-not-found-route-link', ) defaultNotFoundRouteLink.click() const defaultNotFoundComponent = await screen.findByTestId( 'default-not-found-with-props', {}, { timeout: 1000 }, ) expect(defaultNotFoundComponent).toBeInTheDocument() const defaultNotFoundComponentMessage = await screen.findByTestId('message') expect(defaultNotFoundComponentMessage).toHaveTextContent(customData.message) const notFoundRouteLink = screen.getByTestId('not-found-route-link') notFoundRouteLink.click() const notFoundComponent = await screen.findByTestId( 'not-found-with-props', {}, { timeout: 1000 }, ) expect(notFoundComponent).toBeInTheDocument() const errorMessageComponent = await screen.findByTestId('message') expect(errorMessageComponent).toHaveTextContent(customData.message) }) test('component-thrown bare notFound renders current route notFoundComponent', async () => { const rootRoute = createRootRoute({ component: () => , notFoundComponent: () => Root, }) const parentRoute = createRoute({ getParentRoute: () => rootRoute, path: '/parent', component: () => , notFoundComponent: () => ( Parent not found ), }) const childRoute = createRoute({ getParentRoute: () => parentRoute, path: '/child', notFoundComponent: () => ( Child not found ), component: () => { throw notFound() }, }) const router = createRouter({ routeTree: rootRoute.addChildren([parentRoute.addChildren([childRoute])]), history, notFoundMode: 'fuzzy', }) render() await router.navigate({ to: '/parent/child' }) expect(await screen.findByTestId('child-not-found')).toBeInTheDocument() expect(screen.queryByTestId('parent-not-found')).not.toBeInTheDocument() expect(screen.queryByTestId('root-not-found')).not.toBeInTheDocument() }) test('component-thrown bare notFound falls back to nearest ancestor notFoundComponent', async () => { const rootRoute = createRootRoute({ component: () => , notFoundComponent: () => Root, }) const parentRoute = createRoute({ getParentRoute: () => rootRoute, path: '/parent', component: () => , notFoundComponent: () => ( Parent not found ), }) const childRoute = createRoute({ getParentRoute: () => parentRoute, path: '/child', component: () => { throw notFound() }, }) const router = createRouter({ routeTree: rootRoute.addChildren([parentRoute.addChildren([childRoute])]), history, notFoundMode: 'fuzzy', }) render() await router.navigate({ to: '/parent/child' }) expect(await screen.findByTestId('parent-not-found')).toBeInTheDocument() expect(screen.queryByTestId('root-not-found')).not.toBeInTheDocument() }) test('beforeLoad notFound with routeId targets root notFoundComponent', async () => { const rootRoute = createRootRoute({ component: () => , notFoundComponent: () => ( Root not found ), }) const parentRoute = createRoute({ getParentRoute: () => rootRoute, path: '/parent', component: () => , notFoundComponent: () => ( Parent not found ), }) const childRoute = createRoute({ getParentRoute: () => parentRoute, path: '/child', beforeLoad: () => { throw notFound({ routeId: rootRouteId }) }, component: () => Child, }) const router = createRouter({ routeTree: rootRoute.addChildren([parentRoute.addChildren([childRoute])]), history, notFoundMode: 'fuzzy', }) render() await router.navigate({ to: '/parent/child' }) expect(await screen.findByTestId('root-not-found')).toBeInTheDocument() expect(screen.queryByTestId('child-component')).not.toBeInTheDocument() }) test('beforeLoad notFound with routeId targets parent boundary and preserves parent loader data', async () => { const rootRoute = createRootRoute({ component: () => , notFoundComponent: () => ( Root not found ), }) const ParentNotFoundComponent = defineComponent({ setup() { const loaderData = parentRoute.useLoaderData() return () => ( {loaderData.value.message} ) }, }) const parentRoute = createRoute({ getParentRoute: () => rootRoute, path: '/parent', loader: () => ({ message: 'ready' }), component: () => , notFoundComponent: ParentNotFoundComponent, }) const childRoute = createRoute({ getParentRoute: () => parentRoute, path: '/child', beforeLoad: () => { throw notFound({ routeId: parentRoute.id }) }, component: () => Child, }) const router = createRouter({ routeTree: rootRoute.addChildren([parentRoute.addChildren([childRoute])]), history, notFoundMode: 'fuzzy', }) render() await router.navigate({ to: '/parent/child' }) expect( await screen.findByTestId('parent-not-found-with-loader-data'), ).toHaveTextContent('ready') expect(screen.queryByTestId('child-component')).not.toBeInTheDocument() }) test('beforeLoad notFound with non-exact routeId falls back to root notFoundComponent', async () => { const rootRoute = createRootRoute({ component: () => , notFoundComponent: () => ( Root not found ), }) const parentRoute = createRoute({ getParentRoute: () => rootRoute, path: '/parent', component: () => , notFoundComponent: () => ( Parent not found ), }) const childRoute = createRoute({ getParentRoute: () => parentRoute, path: '/child', beforeLoad: () => { throw notFound({ routeId: `${parentRoute.id}/` as never }) }, component: () => Child, }) const router = createRouter({ routeTree: rootRoute.addChildren([parentRoute.addChildren([childRoute])]), history, notFoundMode: 'fuzzy', }) render() await router.navigate({ to: '/parent/child' }) expect(await screen.findByTestId('root-not-found')).toBeInTheDocument() expect(screen.queryByTestId('parent-not-found')).not.toBeInTheDocument() expect(screen.queryByTestId('child-component')).not.toBeInTheDocument() })