Skip to content

Commit 28b5de3

Browse files
committed
test(react-router): add test verify notFoundRouteProps properly passes data
Signed-off-by: leesb971204 <[email protected]>
1 parent a3bdffd commit 28b5de3

File tree

1 file changed

+121
-1
lines changed

1 file changed

+121
-1
lines changed

packages/react-router/tests/not-found.test.tsx

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*/
14
import { afterEach, beforeEach, expect, test } from 'vitest'
25
import { cleanup, render, screen } from '@testing-library/react'
6+
import '@testing-library/jest-dom/vitest'
37

48
import {
59
Link,
@@ -9,8 +13,9 @@ import {
913
createRootRoute,
1014
createRoute,
1115
createRouter,
16+
notFound,
1217
} from '../src'
13-
import type { RouterHistory } from '../src'
18+
import type { NotFoundRouteProps, RouterHistory } from '../src'
1419

1520
let history: RouterHistory
1621

@@ -123,3 +128,118 @@ test.each([
123128
expect(notFoundComponent).toBeInTheDocument()
124129
},
125130
)
131+
132+
test('defaultNotFoundComponent and notFoundComponent receives data props via spread operator', async () => {
133+
const customData = {
134+
message: 'Custom not found message',
135+
}
136+
137+
const DefaultNotFoundComponentWithProps = (props: NotFoundRouteProps) => (
138+
<div data-testid="default-not-found-with-props">
139+
<span data-testid="message">{props.data.message}</span>
140+
</div>
141+
)
142+
143+
const NotFoundComponentWithProps = (props: NotFoundRouteProps) => (
144+
<div data-testid="not-found-with-props">
145+
<span data-testid="message">{props.data.message}</span>
146+
</div>
147+
)
148+
149+
const rootRoute = createRootRoute({
150+
component: () => (
151+
<div data-testid="root-component">
152+
<h1>Root Component</h1>
153+
<div>
154+
<Link
155+
data-testid="default-not-found-route-link"
156+
to="/default-not-found-route"
157+
>
158+
link to default not found route
159+
</Link>
160+
<Link data-testid="not-found-route-link" to="/not-found-route">
161+
link to not found route
162+
</Link>
163+
</div>
164+
<Outlet />
165+
</div>
166+
),
167+
})
168+
169+
const indexRoute = createRoute({
170+
getParentRoute: () => rootRoute,
171+
path: '/',
172+
component: () => (
173+
<div data-testid="index-component">
174+
<h2>Index Page</h2>
175+
</div>
176+
),
177+
})
178+
179+
const defaultNotFoundRoute = createRoute({
180+
getParentRoute: () => rootRoute,
181+
path: '/default-not-found-route',
182+
loader: () => {
183+
throw notFound({ data: customData })
184+
},
185+
component: () => (
186+
<div data-testid="default-not-found-route-component">
187+
Should not render
188+
</div>
189+
),
190+
})
191+
192+
const notFoundRoute = createRoute({
193+
getParentRoute: () => rootRoute,
194+
path: '/not-found-route',
195+
loader: () => {
196+
throw notFound({ data: customData })
197+
},
198+
component: () => (
199+
<div data-testid="not-found-route-component">Should not render</div>
200+
),
201+
notFoundComponent: NotFoundComponentWithProps,
202+
})
203+
204+
const router = createRouter({
205+
routeTree: rootRoute.addChildren([
206+
indexRoute,
207+
defaultNotFoundRoute,
208+
notFoundRoute,
209+
]),
210+
history,
211+
defaultNotFoundComponent: DefaultNotFoundComponentWithProps,
212+
})
213+
214+
render(<RouterProvider router={router} />)
215+
await router.load()
216+
await screen.findByTestId('root-component')
217+
218+
const defaultNotFoundRouteLink = screen.getByTestId(
219+
'default-not-found-route-link',
220+
)
221+
defaultNotFoundRouteLink.click()
222+
223+
const defaultNotFoundComponent = await screen.findByTestId(
224+
'default-not-found-with-props',
225+
{},
226+
{ timeout: 1000 },
227+
)
228+
expect(defaultNotFoundComponent).toBeInTheDocument()
229+
230+
const defaultNotFoundComponentMessage = await screen.findByTestId('message')
231+
expect(defaultNotFoundComponentMessage).toHaveTextContent(customData.message)
232+
233+
const notFoundRouteLink = screen.getByTestId('not-found-route-link')
234+
notFoundRouteLink.click()
235+
236+
const notFoundComponent = await screen.findByTestId(
237+
'not-found-with-props',
238+
{},
239+
{ timeout: 1000 },
240+
)
241+
expect(notFoundComponent).toBeInTheDocument()
242+
243+
const errorMessageComponent = await screen.findByTestId('message')
244+
expect(errorMessageComponent).toHaveTextContent(customData.message)
245+
})

0 commit comments

Comments
 (0)