-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
111 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,177 +1,162 @@ | ||
/* eslint-disable no-new */ | ||
/* eslint-disable @typescript-eslint/promise-function-async */ | ||
/* eslint-disable @typescript-eslint/no-misused-promises */ | ||
/* eslint-disable @typescript-eslint/array-type */ | ||
import { describe, it } from "vitest" | ||
import { describe, it, expectTypeOf } from "vitest" | ||
import { type DataTag } from "./queries/types" | ||
import { QueryClient } from "./QueryClient" | ||
import { doNotExecute } from "../../../tests/utils" | ||
import { type Equal, type Expect } from "../../utils/types" | ||
import { type FetchDirection } from "./queries/query/types" | ||
import { type QueryKey } from "./keys/types" | ||
|
||
describe("getQueryData", () => { | ||
it("should be typed if key is tagged", () => { | ||
doNotExecute(() => { | ||
const queryKey = ["key"] as DataTag<Array<string>, number> | ||
const queryClient = new QueryClient() | ||
const data = queryClient.getQueryData(queryKey) | ||
const queryKey = ["key"] as DataTag<Array<string>, number> | ||
const queryClient = new QueryClient() | ||
const data = queryClient.getQueryData(queryKey) | ||
|
||
const result: Expect<Equal<typeof data, number | undefined>> = true | ||
return result | ||
}) | ||
expectTypeOf(data).toEqualTypeOf<number | undefined>() | ||
}) | ||
|
||
it("should infer unknown if key is not tagged", () => { | ||
doNotExecute(() => { | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.getQueryData(queryKey) | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.getQueryData(queryKey) | ||
|
||
const result: Expect<Equal<typeof data, unknown>> = true | ||
return result | ||
}) | ||
expectTypeOf(data).toEqualTypeOf<unknown>() | ||
}) | ||
|
||
it("should infer passed generic if passed", () => { | ||
doNotExecute(() => { | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.getQueryData<number>(queryKey) | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.getQueryData<number>(queryKey) | ||
|
||
const result: Expect<Equal<typeof data, number | undefined>> = true | ||
return result | ||
}) | ||
expectTypeOf(data).toEqualTypeOf<number | undefined>() | ||
}) | ||
|
||
it("should only allow Arrays to be passed", () => { | ||
doNotExecute(() => { | ||
const queryKey = "key" as const | ||
const queryClient = new QueryClient() | ||
// @ts-expect-error TS2345: Argument of type 'string' is not assignable to parameter of type 'QueryKey' | ||
return queryClient.getQueryData(queryKey) | ||
}) | ||
const queryKey = "key" as const | ||
const queryClient = new QueryClient() | ||
// @ts-expect-error TS2345: Argument of type 'string' is not assignable to parameter of type 'QueryKey' | ||
return queryClient.getQueryData(queryKey) | ||
}) | ||
}) | ||
|
||
describe("setQueryData", () => { | ||
it("updater should be typed if key is tagged", () => { | ||
doNotExecute(() => { | ||
const queryKey = ["key"] as DataTag<Array<string>, number> | ||
const queryClient = new QueryClient() | ||
const data = queryClient.setQueryData(queryKey, (prev) => { | ||
const result: Expect<Equal<typeof prev, number | undefined>> = true | ||
return result ? prev : 1 | ||
}) | ||
|
||
const result: Expect<Equal<typeof data, number | undefined>> = true | ||
return result | ||
const queryKey = ["key"] as DataTag<Array<string>, number> | ||
const queryClient = new QueryClient() | ||
const data = queryClient.setQueryData(queryKey, (prev) => { | ||
expectTypeOf(prev).toEqualTypeOf<number | undefined>() | ||
return prev | ||
}) | ||
|
||
expectTypeOf(data).toEqualTypeOf<number | undefined>() | ||
}) | ||
|
||
it("value should be typed if key is tagged", () => { | ||
doNotExecute(() => { | ||
const queryKey = ["key"] as DataTag<Array<string>, number> | ||
const queryClient = new QueryClient() | ||
const queryKey = ["key"] as DataTag<Array<string>, number> | ||
const queryClient = new QueryClient() | ||
|
||
// @ts-expect-error value should be a number | ||
queryClient.setQueryData(queryKey, "1") | ||
// @ts-expect-error value should be a number | ||
queryClient.setQueryData(queryKey, "1") | ||
|
||
// @ts-expect-error value should be a number | ||
queryClient.setQueryData(queryKey, () => "1") | ||
// @ts-expect-error value should be a number | ||
queryClient.setQueryData(queryKey, () => "1") | ||
|
||
const data = queryClient.setQueryData(queryKey, 1) | ||
const data = queryClient.setQueryData(queryKey, 1) | ||
|
||
const result: Expect<Equal<typeof data, number | undefined>> = true | ||
return result | ||
}) | ||
expectTypeOf(data).toEqualTypeOf<number | undefined>() | ||
}) | ||
|
||
it("should infer unknown for updater if key is not tagged", () => { | ||
doNotExecute(() => { | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.setQueryData(queryKey, (prev) => { | ||
const result: Expect<Equal<typeof prev, unknown>> = true | ||
return result ? prev : 1 | ||
}) | ||
|
||
const result: Expect<Equal<typeof data, unknown>> = true | ||
return result | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.setQueryData(queryKey, (prev) => { | ||
expectTypeOf(prev).toEqualTypeOf<unknown>() | ||
return prev | ||
}) | ||
|
||
expectTypeOf(data).toEqualTypeOf<unknown>() | ||
}) | ||
|
||
it("should infer unknown for value if key is not tagged", () => { | ||
doNotExecute(() => { | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.setQueryData(queryKey, "foo") | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.setQueryData(queryKey, "foo") | ||
|
||
const result: Expect<Equal<typeof data, unknown>> = true | ||
return result | ||
}) | ||
expectTypeOf(data).toEqualTypeOf<unknown>() | ||
}) | ||
|
||
it("should infer passed generic if passed", () => { | ||
doNotExecute(() => { | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.setQueryData<string>(queryKey, (prev) => { | ||
const result: Expect<Equal<typeof prev, string | undefined>> = true | ||
return result ? prev : "1" | ||
}) | ||
|
||
const result: Expect<Equal<typeof data, string | undefined>> = true | ||
return result | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.setQueryData<string>(queryKey, (prev) => { | ||
expectTypeOf(prev).toEqualTypeOf<string | undefined>() | ||
return prev | ||
}) | ||
|
||
expectTypeOf(data).toEqualTypeOf<string | undefined>() | ||
}) | ||
|
||
it("should infer passed generic for value", () => { | ||
doNotExecute(() => { | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.setQueryData<string>(queryKey, "foo") | ||
const queryKey = ["key"] as const | ||
const queryClient = new QueryClient() | ||
const data = queryClient.setQueryData<string>(queryKey, "foo") | ||
|
||
expectTypeOf(data).toEqualTypeOf<string | undefined>() | ||
}) | ||
}) | ||
|
||
const result: Expect<Equal<typeof data, string | undefined>> = true | ||
return result | ||
describe("fetchInfiniteQuery", () => { | ||
// it('should allow passing pages', async () => { | ||
// const data = await new QueryClient().fetchInfiniteQuery({ | ||
// queryKey: ['key'], | ||
// queryFn: () => Promise.resolve('string'), | ||
// getNextPageParam: () => 1, | ||
// initialPageParam: 1, | ||
// pages: 5, | ||
// }) | ||
|
||
// expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>() | ||
// }) | ||
|
||
// it('should not allow passing getNextPageParam without pages', () => { | ||
// new QueryClient().fetchInfiniteQuery({ | ||
// queryKey: ['key'], | ||
// queryFn: () => Promise.resolve('string'), | ||
// initialPageParam: 1, | ||
// getNextPageParam: () => 1, | ||
// }) | ||
// }) | ||
|
||
it("should not allow passing pages without getNextPageParam", () => { | ||
// @ts-expect-error Property 'getNextPageParam' is missing | ||
return new QueryClient().fetchInfiniteQuery({ | ||
Check failure on line 134 in src/lib/queries/client/QueryClient.types.rq.test.ts GitHub Actions / Releasesrc/lib/queries/client/QueryClient.types.rq.test.ts > fetchInfiniteQuery > should not allow passing pages without getNextPageParam
|
||
queryKey: ["key"], | ||
queryFn: () => Promise.resolve("string"), | ||
initialPageParam: 1, | ||
pages: 5 | ||
}) | ||
}) | ||
}) | ||
|
||
// describe('fetchInfiniteQuery', () => { | ||
// it('should allow passing pages', () => { | ||
// doNotExecute(async () => { | ||
// const data = await new QueryClient().fetchInfiniteQuery({ | ||
// queryKey: ['key'], | ||
// queryFn: () => Promise.resolve('string'), | ||
// getNextPageParam: () => 1, | ||
// initialPageParam: 1, | ||
// pages: 5, | ||
// }) | ||
|
||
// const result: Expect<Equal<typeof data, InfiniteData<string, number>>> = | ||
// true | ||
// return result | ||
// }) | ||
// }) | ||
|
||
// it('should not allow passing getNextPageParam without pages', () => { | ||
// doNotExecute(async () => { | ||
// return new QueryClient().fetchInfiniteQuery({ | ||
// queryKey: ['key'], | ||
// queryFn: () => Promise.resolve('string'), | ||
// initialPageParam: 1, | ||
// getNextPageParam: () => 1, | ||
// }) | ||
// }) | ||
// }) | ||
|
||
// it('should not allow passing pages without getNextPageParam', () => { | ||
// doNotExecute(async () => { | ||
// // @ts-expect-error Property 'getNextPageParam' is missing | ||
// return new QueryClient().fetchInfiniteQuery({ | ||
// queryKey: ['key'], | ||
// queryFn: () => Promise.resolve('string'), | ||
// initialPageParam: 1, | ||
// pages: 5, | ||
// }) | ||
// }) | ||
// }) | ||
// }) | ||
describe("defaultOptions", () => { | ||
it("should have a typed QueryFunctionContext", () => { | ||
new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
queryFn: (context) => { | ||
expectTypeOf(context).toEqualTypeOf<{ | ||
queryKey: QueryKey | ||
meta: Record<string, unknown> | undefined | ||
signal: AbortSignal | ||
pageParam?: unknown | ||
direction?: FetchDirection | ||
}>() | ||
return Promise.resolve("data") | ||
} | ||
} | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters