Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions packages/react-query/src/__tests__/infiniteQueryOptions.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertType, describe, expectTypeOf, it, test } from 'vitest'
import { QueryClient, dataTagSymbol, skipToken } from '@tanstack/query-core'
import { queryKey } from '@tanstack/query-test-utils'
import { infiniteQueryOptions } from '../infiniteQueryOptions'
import { useInfiniteQuery } from '../useInfiniteQuery'
import { useSuspenseInfiniteQuery } from '../useSuspenseInfiniteQuery'
Expand All @@ -14,7 +15,7 @@ describe('infiniteQueryOptions', () => {
it('should not allow excess properties', () => {
assertType(
infiniteQueryOptions({
queryKey: ['key'],
queryKey: queryKey(),
queryFn: () => Promise.resolve('data'),
getNextPageParam: () => 1,
initialPageParam: 1,
Expand All @@ -25,7 +26,7 @@ describe('infiniteQueryOptions', () => {
})
it('should infer types for callbacks', () => {
infiniteQueryOptions({
queryKey: ['key'],
queryKey: queryKey(),
queryFn: () => Promise.resolve('data'),
staleTime: 1000,
getNextPageParam: () => 1,
Expand All @@ -37,7 +38,7 @@ describe('infiniteQueryOptions', () => {
})
it('should work when passed to useInfiniteQuery', () => {
const options = infiniteQueryOptions({
queryKey: ['key'],
queryKey: queryKey(),
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
Expand All @@ -52,7 +53,7 @@ describe('infiniteQueryOptions', () => {
})
it('should work when passed to useSuspenseInfiniteQuery', () => {
const options = infiniteQueryOptions({
queryKey: ['key'],
queryKey: queryKey(),
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
Expand All @@ -64,7 +65,7 @@ describe('infiniteQueryOptions', () => {
})
it('should work when passed to fetchInfiniteQuery', async () => {
const options = infiniteQueryOptions({
queryKey: ['key'],
queryKey: queryKey(),
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
Expand All @@ -75,61 +76,61 @@ describe('infiniteQueryOptions', () => {
expectTypeOf(data).toEqualTypeOf<InfiniteData<string, number>>()
})
it('should tag the queryKey with the result type of the QueryFn', () => {
const { queryKey } = infiniteQueryOptions({
queryKey: ['key'],
const { queryKey: tagged } = infiniteQueryOptions({
queryKey: queryKey(),
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
})

expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
})
it('should tag the queryKey even if no promise is returned', () => {
const { queryKey } = infiniteQueryOptions({
queryKey: ['key'],
const { queryKey: tagged } = infiniteQueryOptions({
queryKey: queryKey(),
queryFn: () => 'string',
getNextPageParam: () => 1,
initialPageParam: 1,
})

expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
})
it('should tag the queryKey with the result type of the QueryFn if select is used', () => {
const { queryKey } = infiniteQueryOptions({
queryKey: ['key'],
const { queryKey: tagged } = infiniteQueryOptions({
queryKey: queryKey(),
queryFn: () => Promise.resolve('string'),
select: (data) => data.pages,
getNextPageParam: () => 1,
initialPageParam: 1,
})

expectTypeOf(queryKey[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
expectTypeOf(tagged[dataTagSymbol]).toEqualTypeOf<InfiniteData<string>>()
})
it('should return the proper type when passed to getQueryData', () => {
const { queryKey } = infiniteQueryOptions({
queryKey: ['key'],
const { queryKey: tagged } = infiniteQueryOptions({
queryKey: queryKey(),
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
})

const queryClient = new QueryClient()
const data = queryClient.getQueryData(queryKey)
const data = queryClient.getQueryData(tagged)

expectTypeOf(data).toEqualTypeOf<
InfiniteData<string, unknown> | undefined
>()
})
it('should properly type when passed to setQueryData', () => {
const { queryKey } = infiniteQueryOptions({
queryKey: ['key'],
const { queryKey: tagged } = infiniteQueryOptions({
queryKey: queryKey(),
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
})

const queryClient = new QueryClient()
const data = queryClient.setQueryData(queryKey, (prev) => {
const data = queryClient.setQueryData(tagged, (prev) => {
expectTypeOf(prev).toEqualTypeOf<
InfiniteData<string, unknown> | undefined
>()
Expand All @@ -142,7 +143,7 @@ describe('infiniteQueryOptions', () => {
})
it('should throw a type error when using queryFn with skipToken in a suspense query', () => {
const options = infiniteQueryOptions({
queryKey: ['key'],
queryKey: queryKey(),
queryFn:
Math.random() > 0.5 ? skipToken : () => Promise.resolve('string'),
getNextPageParam: () => 1,
Expand All @@ -156,7 +157,7 @@ describe('infiniteQueryOptions', () => {
test('should not be allowed to be passed to non-infinite query functions', () => {
const queryClient = new QueryClient()
const options = infiniteQueryOptions({
queryKey: ['key'],
queryKey: queryKey(),
queryFn: () => Promise.resolve('string'),
getNextPageParam: () => 1,
initialPageParam: 1,
Expand All @@ -182,7 +183,7 @@ describe('infiniteQueryOptions', () => {
test('allow optional initialData function', () => {
const initialData: { example: boolean } | undefined = { example: true }
const queryOptions = infiniteQueryOptions({
queryKey: ['example'],
queryKey: queryKey(),
queryFn: () => initialData,
initialData: initialData
? () => ({ pages: [initialData], pageParams: [] })
Expand All @@ -200,7 +201,7 @@ describe('infiniteQueryOptions', () => {
test('allow optional initialData object', () => {
const initialData: { example: boolean } | undefined = { example: true }
const queryOptions = infiniteQueryOptions({
queryKey: ['example'],
queryKey: queryKey(),
queryFn: () => initialData,
initialData: initialData
? { pages: [initialData], pageParams: [] }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { describe, expect, it } from 'vitest'
import { queryKey } from '@tanstack/query-test-utils'

import { infiniteQueryOptions } from '../infiniteQueryOptions'
import type { UseInfiniteQueryOptions } from '../types'

describe('infiniteQueryOptions', () => {
it('should return the object received as a parameter without any modification.', () => {
const object: UseInfiniteQueryOptions = {
queryKey: ['key'],
queryKey: queryKey(),
queryFn: () => Promise.resolve(5),
getNextPageParam: () => null,
initialPageParam: null,
Expand Down
23 changes: 12 additions & 11 deletions packages/react-query/src/__tests__/mutationOptions.test-d.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { assertType, describe, expectTypeOf, it } from 'vitest'
import { QueryClient } from '@tanstack/query-core'
import { queryKey } from '@tanstack/query-test-utils'
import { useIsMutating, useMutation, useMutationState } from '..'
import { mutationOptions } from '../mutationOptions'
import type {
Expand All @@ -15,7 +16,7 @@ describe('mutationOptions', () => {
// @ts-expect-error this is a good error, because onMutates does not exist!
mutationOptions({
mutationFn: () => Promise.resolve(5),
mutationKey: ['key'],
mutationKey: queryKey(),
onMutates: 1000,
onSuccess: (data) => {
expectTypeOf(data).toEqualTypeOf<number>()
Expand All @@ -26,7 +27,7 @@ describe('mutationOptions', () => {
it('should infer types for callbacks', () => {
mutationOptions({
mutationFn: () => Promise.resolve(5),
mutationKey: ['key'],
mutationKey: queryKey(),
onSuccess: (data) => {
expectTypeOf(data).toEqualTypeOf<number>()
},
Expand All @@ -38,7 +39,7 @@ describe('mutationOptions', () => {
mutationFn: () => {
throw new Error('fail')
},
mutationKey: ['key'],
mutationKey: queryKey(),
onError: (error) => {
expectTypeOf(error).toEqualTypeOf<DefaultError>()
},
Expand All @@ -51,14 +52,14 @@ describe('mutationOptions', () => {
expectTypeOf(vars).toEqualTypeOf<{ id: string }>()
return Promise.resolve(5)
},
mutationKey: ['with-vars'],
mutationKey: queryKey(),
})
})

it('should infer result type correctly', () => {
mutationOptions<number, DefaultError, void, { name: string }>({
mutationFn: () => Promise.resolve(5),
mutationKey: ['key'],
mutationKey: queryKey(),
onMutate: () => {
return { name: 'onMutateResult' }
},
Expand All @@ -74,7 +75,7 @@ describe('mutationOptions', () => {
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
return Promise.resolve(5)
},
mutationKey: ['key'],
mutationKey: queryKey(),
onMutate: (_variables, context) => {
expectTypeOf(context).toEqualTypeOf<MutationFunctionContext>()
},
Expand Down Expand Up @@ -112,7 +113,7 @@ describe('mutationOptions', () => {
expectTypeOf(
mutationOptions({
mutationFn: (id: string) => Promise.resolve(id.length),
mutationKey: ['key'],
mutationKey: queryKey(),
onSuccess: (data) => {
expectTypeOf(data).toEqualTypeOf<number>()
},
Expand All @@ -138,7 +139,7 @@ describe('mutationOptions', () => {
it('should infer types when used with useMutation', () => {
const mutation = useMutation(
mutationOptions({
mutationKey: ['key'],
mutationKey: queryKey(),
mutationFn: () => Promise.resolve('data'),
onSuccess: (data) => {
expectTypeOf(data).toEqualTypeOf<string>()
Expand All @@ -163,7 +164,7 @@ describe('mutationOptions', () => {
it('should infer types when used with useIsMutating', () => {
const isMutating = useIsMutating(
mutationOptions({
mutationKey: ['key'],
mutationKey: queryKey(),
mutationFn: () => Promise.resolve(5),
}),
)
Expand All @@ -182,7 +183,7 @@ describe('mutationOptions', () => {

const isMutating = queryClient.isMutating(
mutationOptions({
mutationKey: ['key'],
mutationKey: queryKey(),
mutationFn: () => Promise.resolve(5),
}),
)
Expand All @@ -199,7 +200,7 @@ describe('mutationOptions', () => {
it('should infer types when used with useMutationState', () => {
const mutationState = useMutationState({
filters: mutationOptions({
mutationKey: ['key'],
mutationKey: queryKey(),
mutationFn: () => Promise.resolve(5),
}),
})
Expand Down
20 changes: 10 additions & 10 deletions packages/react-query/src/__tests__/mutationOptions.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { QueryClient } from '@tanstack/query-core'
import { sleep } from '@tanstack/query-test-utils'
import { queryKey, sleep } from '@tanstack/query-test-utils'
import { fireEvent } from '@testing-library/react'
import { mutationOptions } from '../mutationOptions'
import { useIsMutating, useMutation, useMutationState } from '..'
Expand Down Expand Up @@ -38,7 +38,7 @@ describe('mutationOptions', () => {
const isMutatingArray: Array<number> = []
const queryClient = new QueryClient()
const mutationOpts = mutationOptions({
mutationKey: ['key'],
mutationKey: queryKey(),
mutationFn: () => sleep(50).then(() => 'data'),
})

Expand Down Expand Up @@ -129,7 +129,7 @@ describe('mutationOptions', () => {
const isMutatingArray: Array<number> = []
const queryClient = new QueryClient()
const mutationOpts1 = mutationOptions({
mutationKey: ['key'],
mutationKey: queryKey(),
mutationFn: () => sleep(50).then(() => 'data1'),
})
const mutationOpts2 = mutationOptions({
Expand Down Expand Up @@ -181,7 +181,7 @@ describe('mutationOptions', () => {
const isMutatingArray: Array<number> = []
const queryClient = new QueryClient()
const mutationOpts1 = mutationOptions({
mutationKey: ['key'],
mutationKey: queryKey(),
mutationFn: () => sleep(50).then(() => 'data1'),
})
const mutationOpts2 = mutationOptions({
Expand Down Expand Up @@ -235,7 +235,7 @@ describe('mutationOptions', () => {
const isMutatingArray: Array<number> = []
const queryClient = new QueryClient()
const mutationOpts = mutationOptions({
mutationKey: ['mutation'],
mutationKey: queryKey(),
mutationFn: () => sleep(500).then(() => 'data'),
})

Expand Down Expand Up @@ -298,7 +298,7 @@ describe('mutationOptions', () => {
const isMutatingArray: Array<number> = []
const queryClient = new QueryClient()
const mutationOpts1 = mutationOptions({
mutationKey: ['mutation'],
mutationKey: queryKey(),
mutationFn: () => sleep(500).then(() => 'data1'),
})
const mutationOpts2 = mutationOptions({
Expand Down Expand Up @@ -336,7 +336,7 @@ describe('mutationOptions', () => {
const isMutatingArray: Array<number> = []
const queryClient = new QueryClient()
const mutationOpts1 = mutationOptions({
mutationKey: ['mutation'],
mutationKey: queryKey(),
mutationFn: () => sleep(500).then(() => 'data1'),
})
const mutationOpts2 = mutationOptions({
Expand Down Expand Up @@ -378,7 +378,7 @@ describe('mutationOptions', () => {
> = []
const queryClient = new QueryClient()
const mutationOpts = mutationOptions({
mutationKey: ['mutation'],
mutationKey: queryKey(),
mutationFn: () => sleep(10).then(() => 'data'),
})

Expand Down Expand Up @@ -447,7 +447,7 @@ describe('mutationOptions', () => {
> = []
const queryClient = new QueryClient()
const mutationOpts1 = mutationOptions({
mutationKey: ['mutation'],
mutationKey: queryKey(),
mutationFn: () => sleep(10).then(() => 'data1'),
})
const mutationOpts2 = mutationOptions({
Expand Down Expand Up @@ -489,7 +489,7 @@ describe('mutationOptions', () => {
> = []
const queryClient = new QueryClient()
const mutationOpts1 = mutationOptions({
mutationKey: ['mutation'],
mutationKey: queryKey(),
mutationFn: () => sleep(10).then(() => 'data1'),
})
const mutationOpts2 = mutationOptions({
Expand Down
Loading
Loading