test(javascript): share tRPC procedure expectations (#17402)

This commit is contained in:
Vincent Koc
2026-12-06 13:39:25 +08:00
committed by GitHub
parent a6987c820d
commit e5d13c1eb0
@@ -14,6 +14,12 @@ const rpcEndpoints = (source: string) =>
mechanism.startsWith("trpc:") || mechanism.startsWith("graphql:"),
);
const expectedProcedure = (value: string, method: string) => ({
kind: "network",
value,
mechanism: `trpc:procedure:${method}`,
});
describe("RPC object property ownership", () => {
it.each([
['url: "/trpc/old", url: "/trpc/live"', "/trpc/live"],
@@ -93,17 +99,9 @@ describe("tRPC procedure recognition", () => {
value: "/api/trpc",
mechanism: "trpc:link:httpBatchLink",
},
{ kind: "network", value: "archive", mechanism: "trpc:procedure:query" },
{
kind: "network",
value: "history.list",
mechanism: "trpc:procedure:query",
},
{
kind: "network",
value: "endpoints.list",
mechanism: "trpc:procedure:query",
},
expectedProcedure("archive", "query"),
expectedProcedure("history.list", "query"),
expectedProcedure("endpoints.list", "query"),
]);
});
@@ -117,31 +115,11 @@ describe("tRPC procedure recognition", () => {
n.invoice.list.useSuspenseQuery();
`),
).toEqual([
{
kind: "network",
value: "post.byId",
mechanism: "trpc:procedure:useQuery",
},
{
kind: "network",
value: "post.add",
mechanism: "trpc:procedure:useMutation",
},
{
kind: "network",
value: "feed",
mechanism: "trpc:procedure:useInfiniteQuery",
},
{
kind: "network",
value: "onEvent",
mechanism: "trpc:procedure:useSubscription",
},
{
kind: "network",
value: "invoice.list",
mechanism: "trpc:procedure:useSuspenseQuery",
},
expectedProcedure("post.byId", "useQuery"),
expectedProcedure("post.add", "useMutation"),
expectedProcedure("feed", "useInfiniteQuery"),
expectedProcedure("onEvent", "useSubscription"),
expectedProcedure("invoice.list", "useSuspenseQuery"),
]);
});
@@ -153,16 +131,8 @@ describe("tRPC procedure recognition", () => {
client.invalidateQueries({ queryKey: e.invoice.list.queryKey() });
`),
).toEqual([
{
kind: "network",
value: "invoice.list",
mechanism: "trpc:procedure:queryOptions",
},
{
kind: "network",
value: "invoice.create",
mechanism: "trpc:procedure:mutationOptions",
},
expectedProcedure("invoice.list", "queryOptions"),
expectedProcedure("invoice.create", "mutationOptions"),
]);
});
});
@@ -184,17 +154,9 @@ describe("tRPC procedure recognition", () => {
value: "/api/trpc",
mechanism: "trpc:link:httpBatchLink",
},
{ kind: "network", value: "getUser", mechanism: "trpc:procedure:query" },
{
kind: "network",
value: "post.add",
mechanism: "trpc:procedure:mutate",
},
{
kind: "network",
value: "onPost",
mechanism: "trpc:procedure:subscribe",
},
expectedProcedure("getUser", "query"),
expectedProcedure("post.add", "mutate"),
expectedProcedure("onPost", "subscribe"),
]);
expect(
rpcEndpoints(`
@@ -229,16 +191,8 @@ describe("tRPC procedure recognition", () => {
e.invoice.onChange.subscribe(undefined, { onData() {} });
`),
).toEqual([
{
kind: "network",
value: "invoice.list",
mechanism: "trpc:procedure:query",
},
{
kind: "network",
value: "invoice.onChange",
mechanism: "trpc:procedure:subscribe",
},
expectedProcedure("invoice.list", "query"),
expectedProcedure("invoice.onChange", "subscribe"),
]);
});
@@ -269,8 +223,8 @@ describe("tRPC binding identity and bundled syntax", () => {
try {} catch (c) { c.users.query(); }
`),
).toEqual([
{ kind: "network", value: "users", mechanism: "trpc:procedure:query" },
{ kind: "network", value: "posts", mechanism: "trpc:procedure:query" },
expectedProcedure("users", "query"),
expectedProcedure("posts", "query"),
]);
});
@@ -285,9 +239,7 @@ describe("tRPC binding identity and bundled syntax", () => {
rpcEndpoints(`
var c = createTRPCClient({}); c.users.query();
`),
).toEqual([
{ kind: "network", value: "users", mechanism: "trpc:procedure:query" },
]);
).toEqual([expectedProcedure("users", "query")]);
});
it("retains one client initializer across variable redeclarations", () => {
@@ -311,9 +263,7 @@ describe("tRPC binding identity and bundled syntax", () => {
if (flag) { var conditional = createTRPCClient({}); }
conditional.groups.query();
`),
).toEqual([
{ kind: "network", value: "users", mechanism: "trpc:procedure:query" },
]);
).toEqual([expectedProcedure("users", "query")]);
});
it("recognizes surviving factory names inside bundled call wrappers", () => {
@@ -334,8 +284,8 @@ describe("tRPC binding identity and bundled syntax", () => {
value: "/api/rpc",
mechanism: "trpc:link:httpBatchLink",
},
{ kind: "network", value: "users", mechanism: "trpc:procedure:query" },
{ kind: "network", value: "posts", mechanism: "trpc:procedure:query" },
expectedProcedure("users", "query"),
expectedProcedure("posts", "query"),
{ kind: "network", value: "/api/other", mechanism: "trpc:link:httpLink" },
]);
});
@@ -343,7 +293,7 @@ describe("tRPC binding identity and bundled syntax", () => {
it("preserves a procedure path of 64 segments", () => {
const path = Array.from({ length: 64 }, (_, i) => `router${i}`).join(".");
expect(rpcEndpoints(`root.${path}.useQuery();`)).toEqual([
{ kind: "network", value: path, mechanism: "trpc:procedure:useQuery" },
expectedProcedure(path, "useQuery"),
]);
});
});