Skip to content

Commit c4f892b

Browse files
committed
chore: added similar test to solid-router in solid-router package
1 parent abe3b86 commit c4f892b

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

packages/solid-router/tests/router.test.tsx

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,88 @@ describe('invalidate', () => {
10171017
expect(match.invalid).toBe(false)
10181018
})
10191019
})
1020+
1021+
it('re-runs loaders that throw notFound() when invalidated via HMR filter', async () => {
1022+
const history = createMemoryHistory({
1023+
initialEntries: ['/hmr-not-found'],
1024+
})
1025+
const loader = vi.fn(() => {
1026+
throw notFound()
1027+
})
1028+
1029+
const rootRoute = createRootRoute({
1030+
component: () => <Outlet />,
1031+
})
1032+
1033+
const hmrRoute = createRoute({
1034+
getParentRoute: () => rootRoute,
1035+
path: '/hmr-not-found',
1036+
loader,
1037+
component: () => <div data-testid="hmr-route">Route</div>,
1038+
notFoundComponent: () => (
1039+
<div data-testid="hmr-route-not-found">Route Not Found</div>
1040+
),
1041+
})
1042+
1043+
const router = createRouter({
1044+
routeTree: rootRoute.addChildren([hmrRoute]),
1045+
history,
1046+
})
1047+
1048+
render(() => <RouterProvider router={router} />)
1049+
await router.load()
1050+
1051+
await screen.findByTestId('hmr-route-not-found')
1052+
const initialCalls = loader.mock.calls.length
1053+
expect(initialCalls).toBeGreaterThan(0)
1054+
1055+
await router.invalidate({
1056+
filter: (match) => match.routeId === hmrRoute.id,
1057+
})
1058+
1059+
await waitFor(() => expect(loader).toHaveBeenCalledTimes(initialCalls + 1))
1060+
await screen.findByTestId('hmr-route-not-found')
1061+
expect(screen.queryByTestId('hmr-route')).not.toBeInTheDocument()
1062+
})
1063+
1064+
it('keeps rendering a route notFoundComponent when loader returns notFound() after invalidate', async () => {
1065+
const history = createMemoryHistory({
1066+
initialEntries: ['/loader-not-found'],
1067+
})
1068+
const loader = vi.fn(() => notFound())
1069+
1070+
const rootRoute = createRootRoute({
1071+
component: () => <Outlet />,
1072+
})
1073+
1074+
const loaderRoute = createRoute({
1075+
getParentRoute: () => rootRoute,
1076+
path: '/loader-not-found',
1077+
loader,
1078+
component: () => <div data-testid="loader-route">Route</div>,
1079+
notFoundComponent: () => (
1080+
<div data-testid="loader-not-found-component">Route Not Found</div>
1081+
),
1082+
})
1083+
1084+
const router = createRouter({
1085+
routeTree: rootRoute.addChildren([loaderRoute]),
1086+
history,
1087+
})
1088+
1089+
render(() => <RouterProvider router={router} />)
1090+
await router.load()
1091+
1092+
await screen.findByTestId('loader-not-found-component')
1093+
const initialCalls = loader.mock.calls.length
1094+
expect(initialCalls).toBeGreaterThan(0)
1095+
1096+
await router.invalidate()
1097+
1098+
await waitFor(() => expect(loader).toHaveBeenCalledTimes(initialCalls + 1))
1099+
await screen.findByTestId('loader-not-found-component')
1100+
expect(screen.queryByTestId('loader-route')).not.toBeInTheDocument()
1101+
})
10201102
})
10211103

10221104
describe('search params in URL', () => {

0 commit comments

Comments
 (0)