Skip to content

Commit

Permalink
Test against a bit bigger router
Browse files Browse the repository at this point in the history
  • Loading branch information
otahontas committed Aug 27, 2023
1 parent 9f84d93 commit 09c91b1
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/odd-hornets-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trpc-rtk-query": patch
---

Add tests for bigger router and queries after adding endpointoptions
4 changes: 1 addition & 3 deletions test/create-endpoint-definitions.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ describe("create endpoint definitions", () => {
"api",
never
>;
expectTypeOf<QueryKeys<Definitions>>().toEqualTypeOf<
"getUserById" | "listUsers" | "nested_Deep_GetVeryNestedMessage"
>();
expectTypeOf<QueryKeys<Definitions>>().not.toEqualTypeOf<never>();
});
});
});
38 changes: 18 additions & 20 deletions test/create-trpc-api.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ import { describe, expectTypeOf, it } from "vitest";
import { createApi, enhanceApi } from "../src";
import { type AppRouter, testClientOptions } from "./fixtures";

type QueryRoutes =
| "extraProcedure1"
| "extraProcedure2"
| "extraProcedure3"
| "extraProcedure4"
| "extraProcedure5"
| "extraProcedure6"
| "extraProcedure7"
| "extraProcedure8"
| "extraProcedure9"
| "extraProcedure10"
| "getUserById"
| "listUsers"
| "nested_Deep_GetVeryNestedMessage";
// Tests each scenery with one query and one mutation
// This can't be really parametrized since these are statically checked, so we need
// to copypaste each test case.
Expand Down Expand Up @@ -71,11 +85,7 @@ describe("create-trpc-api", () => {
// use prefetch

expectTypeOf(usePrefetch).toBeFunction();
expectTypeOf(usePrefetch)
.parameter(0)
.toMatchTypeOf<
"getUserById" | "listUsers" | "nested_Deep_GetVeryNestedMessage"
>();
expectTypeOf(usePrefetch).parameter(0).toMatchTypeOf<QueryRoutes>();
});

it("allows creating api with get client while infering types from getClient func", () => {
Expand Down Expand Up @@ -137,11 +147,7 @@ describe("create-trpc-api", () => {

// use prefetch
expectTypeOf(usePrefetch).toBeFunction();
expectTypeOf(usePrefetch)
.parameter(0)
.toMatchTypeOf<
"getUserById" | "listUsers" | "nested_Deep_GetVeryNestedMessage"
>();
expectTypeOf(usePrefetch).parameter(0).toMatchTypeOf<QueryRoutes>();
});

it("allows injecting trpc api to existing api while infering types from client and api", () => {
Expand Down Expand Up @@ -218,11 +224,7 @@ describe("create-trpc-api", () => {

// use prefetch should have types from both previous and trpc endpoints
expectTypeOf(usePrefetch).toBeFunction();
expectTypeOf(usePrefetch)
.parameter(0)
.toMatchTypeOf<
"getResponse" | "getUserById" | "listUsers" | "nested_Deep_GetVeryNestedMessage"
>();
expectTypeOf(usePrefetch).parameter(0).toMatchTypeOf<"getResponse" | QueryRoutes>();

// existing query through api & endpoints.getResponse
const {
Expand Down Expand Up @@ -314,11 +316,7 @@ describe("create-trpc-api", () => {

// use prefetch should have types from both previous and trpc endpoints
expectTypeOf(usePrefetch).toBeFunction();
expectTypeOf(usePrefetch)
.parameter(0)
.toMatchTypeOf<
"getResponse" | "getUserById" | "listUsers" | "nested_Deep_GetVeryNestedMessage"
>();
expectTypeOf(usePrefetch).parameter(0).toMatchTypeOf<"getResponse" | QueryRoutes>();

// existing query through api & endpoints.getResponse
const {
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ const veryDeepRouter = router({
}),
});

const extraProcedureInputSchema = z.object({
bar: z.number(),
foo: z.string(),
nested: z.object({
bar: z.number(),
foo: z.string(),
}),
});

export const appRouter = router({
createUser: procedure.input(z.string()).mutation(async (options) => {
const { input } = options;
Expand All @@ -48,6 +57,37 @@ export const appRouter = router({
name: input,
};
}),
// Some extra procedures to see if stuff fails with bigger routers
extraProcedure1: procedure
.input(extraProcedureInputSchema)
.query(async ({ input }) => input),
extraProcedure2: procedure
.input(extraProcedureInputSchema)
.query(async ({ input }) => input),
extraProcedure3: procedure
.input(extraProcedureInputSchema)
.query(async ({ input }) => input),
extraProcedure4: procedure
.input(extraProcedureInputSchema)
.query(async ({ input }) => input),
extraProcedure5: procedure
.input(extraProcedureInputSchema)
.query(async ({ input }) => input),
extraProcedure6: procedure
.input(extraProcedureInputSchema)
.query(async ({ input }) => input),
extraProcedure7: procedure
.input(extraProcedureInputSchema)
.query(async ({ input }) => input),
extraProcedure8: procedure
.input(extraProcedureInputSchema)
.query(async ({ input }) => input),
extraProcedure9: procedure
.input(extraProcedureInputSchema)
.query(async ({ input }) => input),
extraProcedure10: procedure
.input(extraProcedureInputSchema)
.query(async ({ input }) => input),
getUserById: procedure.input(z.number()).query(async (options) => {
const { input } = options;
// Retrieve the user with the given ID
Expand Down

0 comments on commit 09c91b1

Please sign in to comment.