From 5abedcd1e932048ea66182b371334ff46ae5dfd2 Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Mon, 11 Dec 2023 14:46:53 -0700 Subject: [PATCH] Update type tests to test for explicit type args --- .../__tests__/createQueryPreloader.test.tsx | 265 ++++++++++++++++-- 1 file changed, 237 insertions(+), 28 deletions(-) diff --git a/src/react/query-preloader/__tests__/createQueryPreloader.test.tsx b/src/react/query-preloader/__tests__/createQueryPreloader.test.tsx index 00a5f5edbcc..a90b2462b7d 100644 --- a/src/react/query-preloader/__tests__/createQueryPreloader.test.tsx +++ b/src/react/query-preloader/__tests__/createQueryPreloader.test.tsx @@ -1001,58 +1001,130 @@ describe.skip("type tests", () => { preloadQuery(query, { variables: { foo: "bar", bar: 2 } }); }); - test("variables are optional and can be anything with unspecified TVariables on a TypedDocumentNode", () => { - const query: TypedDocumentNode<{ greeting: string }> = gql``; + test("variables are optional and can be anything with unspecified TVariables", () => { + type Data = { greeting: string }; + const query: TypedDocumentNode = gql``; preloadQuery(query); + preloadQuery(query); preloadQuery(query, { variables: {} }); + preloadQuery(query, { variables: {} }); preloadQuery(query, { returnPartialData: true, variables: {} }); + preloadQuery(query, { returnPartialData: true, variables: {} }); preloadQuery(query, { variables: { foo: "bar" } }); + preloadQuery(query, { variables: { foo: "bar" } }); preloadQuery(query, { variables: { foo: "bar", bar: 2 } }); + preloadQuery(query, { variables: { foo: "bar", bar: 2 } }); }); test("variables are optional when TVariables are empty", () => { - const query: TypedDocumentNode< - { greeting: string }, - Record - > = gql``; + type Data = { greeting: string }; + type Variables = Record; + const query: TypedDocumentNode = gql``; preloadQuery(query); + preloadQuery(query); preloadQuery(query, { variables: {} }); + preloadQuery(query, { variables: {} }); preloadQuery(query, { returnPartialData: true, variables: {} }); - // @ts-expect-error unknown variables - preloadQuery(query, { variables: { foo: "bar" } }); - // @ts-expect-error unknown variables - preloadQuery(query, { returnPartialData: true, variables: { foo: "bar" } }); + preloadQuery(query, { + returnPartialData: true, + variables: {}, + }); + preloadQuery(query, { + variables: { + // @ts-expect-error unknown variables + foo: "bar", + }, + }); + preloadQuery(query, { + variables: { + // @ts-expect-error unknown variables + foo: "bar", + }, + }); + preloadQuery(query, { + returnPartialData: true, + variables: { + // @ts-expect-error unknown variables + foo: "bar", + }, + }); + preloadQuery(query, { + returnPartialData: true, + variables: { + // @ts-expect-error unknown variables + foo: "bar", + }, + }); }); test("does not allow variables when TVariables is `never`", () => { - const query: TypedDocumentNode<{ greeting: string }, never> = gql``; + type Data = { greeting: string }; + const query: TypedDocumentNode = gql``; preloadQuery(query); + preloadQuery(query); preloadQuery(query, { variables: {} }); + preloadQuery(query, { variables: {} }); preloadQuery(query, { returnPartialData: true, variables: {} }); + preloadQuery(query, { + returnPartialData: true, + variables: {}, + }); // @ts-expect-error no variables allowed preloadQuery(query, { variables: { foo: "bar" } }); // @ts-expect-error no variables allowed - preloadQuery(query, { returnPartialData: true, variables: { foo: "bar" } }); + preloadQuery(query, { variables: { foo: "bar" } }); + preloadQuery(query, { + returnPartialData: true, + variables: { + // @ts-expect-error no variables allowed + foo: "bar", + }, + }); + preloadQuery(query, { + returnPartialData: true, + variables: { + // @ts-expect-error no variables allowed + foo: "bar", + }, + }); }); test("optional variables are optional", () => { - const query: TypedDocumentNode<{ posts: string[] }, { limit?: number }> = - gql``; + type Data = { posts: string[] }; + type Variables = { limit?: number }; + const query: TypedDocumentNode = gql``; preloadQuery(query); + preloadQuery(query); preloadQuery(query, { variables: {} }); + preloadQuery(query, { variables: {} }); preloadQuery(query, { returnPartialData: true, variables: {} }); + preloadQuery(query, { + returnPartialData: true, + variables: {}, + }); preloadQuery(query, { variables: { limit: 10 } }); + preloadQuery(query, { variables: { limit: 10 } }); preloadQuery(query, { returnPartialData: true, variables: { limit: 10 } }); + preloadQuery(query, { + returnPartialData: true, + variables: { limit: 10 }, + }); preloadQuery(query, { variables: { // @ts-expect-error unknown variable foo: "bar", }, }); + preloadQuery(query, { + variables: { + // @ts-expect-error unknown variable + foo: "bar", + }, + }); preloadQuery(query, { returnPartialData: true, variables: { @@ -1060,6 +1132,13 @@ describe.skip("type tests", () => { foo: "bar", }, }); + preloadQuery(query, { + returnPartialData: true, + variables: { + // @ts-expect-error unknown variable + foo: "bar", + }, + }); preloadQuery(query, { variables: { limit: 10, @@ -1067,6 +1146,13 @@ describe.skip("type tests", () => { foo: "bar", }, }); + preloadQuery(query, { + variables: { + limit: 10, + // @ts-expect-error unknown variable + foo: "bar", + }, + }); preloadQuery(query, { returnPartialData: true, variables: { @@ -1075,26 +1161,62 @@ describe.skip("type tests", () => { foo: "bar", }, }); + preloadQuery(query, { + returnPartialData: true, + variables: { + limit: 10, + // @ts-expect-error unknown variable + foo: "bar", + }, + }); }); test("enforces required variables", () => { - const query: TypedDocumentNode<{ character: string }, { id: string }> = - gql``; + type Data = { character: string }; + type Variables = { id: string }; + const query: TypedDocumentNode = gql``; // @ts-expect-error missing variables option preloadQuery(query); - // @ts-expect-error empty variables - preloadQuery(query, { variables: {} }); - // @ts-expect-error empty variables - preloadQuery(query, { returnPartialData: true, variables: {} }); + // @ts-expect-error missing variables option + preloadQuery(query); + preloadQuery(query, { + // @ts-expect-error empty variables + variables: {}, + }); + preloadQuery(query, { + // @ts-expect-error empty variables + variables: {}, + }); + preloadQuery(query, { + returnPartialData: true, + // @ts-expect-error empty variables + variables: {}, + }); + preloadQuery(query, { + returnPartialData: true, + // @ts-expect-error empty variables + variables: {}, + }); preloadQuery(query, { variables: { id: "1" } }); + preloadQuery(query, { variables: { id: "1" } }); preloadQuery(query, { returnPartialData: true, variables: { id: "1" } }); + preloadQuery(query, { + returnPartialData: true, + variables: { id: "1" }, + }); preloadQuery(query, { variables: { // @ts-expect-error unknown variable foo: "bar", }, }); + preloadQuery(query, { + variables: { + // @ts-expect-error unknown variable + foo: "bar", + }, + }); preloadQuery(query, { returnPartialData: true, variables: { @@ -1102,6 +1224,13 @@ describe.skip("type tests", () => { foo: "bar", }, }); + preloadQuery(query, { + returnPartialData: true, + variables: { + // @ts-expect-error unknown variable + foo: "bar", + }, + }); preloadQuery(query, { variables: { id: "1", @@ -1109,6 +1238,21 @@ describe.skip("type tests", () => { foo: "bar", }, }); + preloadQuery(query, { + variables: { + id: "1", + // @ts-expect-error unknown variable + foo: "bar", + }, + }); + preloadQuery(query, { + returnPartialData: true, + variables: { + id: "1", + // @ts-expect-error unknown variable + foo: "bar", + }, + }); preloadQuery(query, { returnPartialData: true, variables: { @@ -1120,27 +1264,62 @@ describe.skip("type tests", () => { }); test("requires variables with mixed TVariables", () => { - const query: TypedDocumentNode< - { character: string }, - { id: string; language?: string } - > = gql``; + type Data = { character: string }; + type Variables = { id: string; language?: string }; + const query: TypedDocumentNode = gql``; // @ts-expect-error missing variables argument preloadQuery(query); // @ts-expect-error missing variables argument - preloadQuery(query, { variables: {} }); + preloadQuery(query); // @ts-expect-error missing variables argument - preloadQuery(query, { returnPartialData: true, variables: {} }); + preloadQuery(query, {}); + // @ts-expect-error missing variables argument + preloadQuery(query, {}); + preloadQuery(query, { + // @ts-expect-error missing required variables + variables: {}, + }); + preloadQuery(query, { + // @ts-expect-error missing required variables + variables: {}, + }); + preloadQuery(query, { + returnPartialData: true, + // @ts-expect-error missing required variables + variables: {}, + }); + preloadQuery(query, { + returnPartialData: true, + // @ts-expect-error missing required variables + variables: {}, + }); preloadQuery(query, { variables: { id: "1" } }); - // @ts-expect-error missing required variable - preloadQuery(query, { variables: { language: "en" } }); + preloadQuery(query, { variables: { id: "1" } }); + preloadQuery(query, { + // @ts-expect-error missing required variable + variables: { language: "en" }, + }); + preloadQuery(query, { + // @ts-expect-error missing required variable + variables: { language: "en" }, + }); preloadQuery(query, { variables: { id: "1", language: "en" } }); + preloadQuery(query, { + variables: { id: "1", language: "en" }, + }); preloadQuery(query, { variables: { // @ts-expect-error unknown variable foo: "bar", }, }); + preloadQuery(query, { + variables: { + // @ts-expect-error unknown variable + foo: "bar", + }, + }); preloadQuery(query, { returnPartialData: true, variables: { @@ -1148,6 +1327,13 @@ describe.skip("type tests", () => { foo: "bar", }, }); + preloadQuery(query, { + returnPartialData: true, + variables: { + // @ts-expect-error unknown variable + foo: "bar", + }, + }); preloadQuery(query, { variables: { id: "1", @@ -1155,6 +1341,13 @@ describe.skip("type tests", () => { foo: "bar", }, }); + preloadQuery(query, { + variables: { + id: "1", + // @ts-expect-error unknown variable + foo: "bar", + }, + }); preloadQuery(query, { returnPartialData: true, variables: { @@ -1163,6 +1356,14 @@ describe.skip("type tests", () => { foo: "bar", }, }); + preloadQuery(query, { + returnPartialData: true, + variables: { + id: "1", + // @ts-expect-error unknown variable + foo: "bar", + }, + }); preloadQuery(query, { variables: { id: "1", @@ -1171,6 +1372,14 @@ describe.skip("type tests", () => { foo: "bar", }, }); + preloadQuery(query, { + variables: { + id: "1", + language: "en", + // @ts-expect-error unknown variable + foo: "bar", + }, + }); }); test("returns QueryReference when TData cannot be inferred", () => {