Skip to content

Commit

Permalink
feat: added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbret committed Apr 28, 2024
1 parent 657a866 commit 35405fd
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 124 deletions.
233 changes: 109 additions & 124 deletions src/lib/queries/client/QueryClient.types.rq.test.ts
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

View workflow job for this annotation

GitHub Actions / Release

src/lib/queries/client/QueryClient.types.rq.test.ts > fetchInfiniteQuery > should not allow passing pages without getNextPageParam

TypeError: (intermediate value).fetchInfiniteQuery is not a function ❯ src/lib/queries/client/QueryClient.types.rq.test.ts:134:30
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")
}
}
}
})
})
})
2 changes: 2 additions & 0 deletions src/lib/queries/client/queries/query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export type QueryFunctionContext<
queryKey: TQueryKey
signal: AbortSignal
meta: QueryMeta | undefined
pageParam?: unknown
direction?: "forward" | "backward"
}
: {
queryKey: TQueryKey
Expand Down

0 comments on commit 35405fd

Please sign in to comment.