Skip to content

Commit f621455

Browse files
committed
Fix type and test
1 parent 4bb64f6 commit f621455

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

packages/react-router/__tests__/dom/stub-test.tsx

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import {
1212
type LoaderFunctionArgs,
1313
useRouteError,
1414
} from "../../index";
15-
import { unstable_createContext } from "../../lib/router/utils";
15+
import {
16+
unstable_RouterContextProvider,
17+
unstable_createContext,
18+
} from "../../lib/router/utils";
1619

1720
test("renders a route", () => {
1821
let RoutesStub = createRoutesStub([
@@ -236,7 +239,50 @@ test("can pass a predefined loader", () => {
236239
]);
237240
});
238241

239-
test("can pass context values", async () => {
242+
test("can pass context values (w/o middleware)", async () => {
243+
let RoutesStub = createRoutesStub(
244+
[
245+
{
246+
path: "/",
247+
HydrateFallback: () => null,
248+
Component() {
249+
let data = useLoaderData() as string;
250+
return (
251+
<div>
252+
<pre data-testid="root">Context: {data}</pre>
253+
<Outlet />
254+
</div>
255+
);
256+
},
257+
loader({ context }) {
258+
return context.message;
259+
},
260+
children: [
261+
{
262+
path: "hello",
263+
Component() {
264+
let data = useLoaderData() as string;
265+
return <pre data-testid="hello">Context: {data}</pre>;
266+
},
267+
loader({ context }) {
268+
return context.message;
269+
},
270+
},
271+
],
272+
},
273+
],
274+
{ message: "hello" }
275+
);
276+
277+
render(<RoutesStub initialEntries={["/hello"]} />);
278+
279+
expect(await screen.findByTestId("root")).toHaveTextContent(/Context: hello/);
280+
expect(await screen.findByTestId("hello")).toHaveTextContent(
281+
/Context: hello/
282+
);
283+
});
284+
285+
test("can pass context values (w/middleware)", async () => {
240286
let helloContext = unstable_createContext();
241287
let RoutesStub = createRoutesStub(
242288
[
@@ -269,10 +315,15 @@ test("can pass context values", async () => {
269315
],
270316
},
271317
],
272-
() => new Map([[helloContext, "hello"]])
318+
new unstable_RouterContextProvider(new Map([[helloContext, "hello"]]))
273319
);
274320

275-
render(<RoutesStub initialEntries={["/hello"]} />);
321+
render(
322+
<RoutesStub
323+
future={{ unstable_middleware: true }}
324+
initialEntries={["/hello"]}
325+
/>
326+
);
276327

277328
expect(await screen.findByTestId("root")).toHaveTextContent(/Context: hello/);
278329
expect(await screen.findByTestId("hello")).toHaveTextContent(

packages/react-router/lib/dom/ssr/routes-test-stub.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ export interface RoutesTestStubProps {
117117
*/
118118
export function createRoutesStub(
119119
routes: StubRouteObject[],
120-
_context: MiddlewareEnabled extends true
121-
? unstable_RouterContextProvider
122-
: AppLoadContext
120+
_context?: AppLoadContext | unstable_RouterContextProvider
123121
) {
124122
return function RoutesTestStub({
125123
initialEntries,

0 commit comments

Comments
 (0)