Skip to content

Commit 66357d4

Browse files
committed
Test updates
1 parent 85bf665 commit 66357d4

File tree

2 files changed

+20
-35
lines changed

2 files changed

+20
-35
lines changed

packages/react-router/__tests__/router/instrumentation-test.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1899,7 +1899,7 @@ describe("instrumentation", () => {
18991899
},
19001900
);
19011901
let handler = createRequestHandler(build);
1902-
let response = await handler(new Request("http://localhost/"));
1902+
let response = await handler(new Request("http://localhost/"), {});
19031903

19041904
expect(await response.text()).toBe("GET http://localhost/ COMPONENT");
19051905
expect(spy.mock.calls).toEqual([
@@ -1913,9 +1913,7 @@ describe("instrumentation", () => {
19131913
get: expect.any(Function),
19141914
},
19151915
},
1916-
context: {
1917-
get: expect.any(Function),
1918-
},
1916+
context: {},
19191917
},
19201918
],
19211919
["loader"],
@@ -1929,9 +1927,7 @@ describe("instrumentation", () => {
19291927
get: expect.any(Function),
19301928
},
19311929
},
1932-
context: {
1933-
get: expect.any(Function),
1934-
},
1930+
context: {},
19351931
},
19361932
],
19371933
]);
@@ -2071,9 +2067,7 @@ describe("instrumentation", () => {
20712067
},
20722068
params: {},
20732069
unstable_pattern: "/",
2074-
context: {
2075-
get: expect.any(Function),
2076-
},
2070+
context: {},
20772071
},
20782072
],
20792073
["loader"],
@@ -2089,9 +2083,7 @@ describe("instrumentation", () => {
20892083
},
20902084
params: {},
20912085
unstable_pattern: "/",
2092-
context: {
2093-
get: expect.any(Function),
2094-
},
2086+
context: {},
20952087
},
20962088
],
20972089
]);
@@ -2148,9 +2140,7 @@ describe("instrumentation", () => {
21482140
},
21492141
params: {},
21502142
unstable_pattern: "/",
2151-
context: {
2152-
get: expect.any(Function),
2153-
},
2143+
context: {},
21542144
},
21552145
],
21562146
["action"],
@@ -2166,9 +2156,7 @@ describe("instrumentation", () => {
21662156
},
21672157
params: {},
21682158
unstable_pattern: "/",
2169-
context: {
2170-
get: expect.any(Function),
2171-
},
2159+
context: {},
21722160
},
21732161
],
21742162
]);

packages/react-router/lib/router/instrumentation.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ type RequestHandlerInstrumentations = {
133133

134134
type RequestHandlerInstrumentationInfo = Readonly<{
135135
request: ReadonlyRequest;
136-
context: ReadonlyContext;
136+
context: ReadonlyContext | undefined;
137137
}>;
138138

139139
const UninstrumentedSymbol = Symbol("Uninstrumented");
@@ -351,7 +351,7 @@ export function instrumentHandler(
351351
let [request, context] = args as Parameters<RequestHandler>;
352352
return {
353353
request: getReadonlyRequest(request),
354-
context: getReadonlyContext(context),
354+
context: context != null ? getReadonlyContext(context) : context,
355355
} satisfies RequestHandlerInstrumentationInfo;
356356
}) as RequestHandler;
357357
}
@@ -483,27 +483,21 @@ function getReadonlyRequest(request: Request): {
483483
}
484484

485485
function getReadonlyContext(
486-
context:
487-
| (MiddlewareEnabled extends true ? RouterContextProvider : AppLoadContext)
488-
// Context can be undefined in the request handler instrumentation case
489-
| null
490-
| undefined,
486+
context: MiddlewareEnabled extends true
487+
? RouterContextProvider
488+
: AppLoadContext,
491489
): MiddlewareEnabled extends true
492490
? Pick<RouterContextProvider, "get">
493491
: Readonly<AppLoadContext> {
494492
if (isPlainObject(context)) {
495-
let frozen = context ? { ...context } : {};
493+
let frozen = { ...context };
496494
Object.freeze(frozen);
497495
return frozen;
498496
} else {
499-
return context
500-
? {
501-
get: <T>(ctx: RouterContext<T>) =>
502-
(context as unknown as RouterContextProvider).get(ctx),
503-
}
504-
: {
505-
get: () => undefined,
506-
};
497+
return {
498+
get: <T>(ctx: RouterContext<T>) =>
499+
(context as unknown as RouterContextProvider).get(ctx),
500+
};
507501
}
508502
}
509503

@@ -515,6 +509,9 @@ const objectProtoNames = Object.getOwnPropertyNames(Object.prototype)
515509
function isPlainObject(
516510
thing: unknown,
517511
): thing is Record<string | number | symbol, unknown> {
512+
if (thing === null || typeof thing !== "object") {
513+
return false;
514+
}
518515
const proto = Object.getPrototypeOf(thing);
519516
return (
520517
proto === Object.prototype ||

0 commit comments

Comments
 (0)