Skip to content

Commit

Permalink
test(types): remove non-void tests, and add tests for handling fetch …
Browse files Browse the repository at this point in the history
…-> promise<any>
  • Loading branch information
artysidorenko committed Jun 2, 2022
1 parent 8e604e2 commit ed43e34
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 86 deletions.
101 changes: 31 additions & 70 deletions src/reactjs/tests/useQueries.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -607,76 +607,6 @@ describe('useQueries', () => {
// @ts-expect-error (Page component is not rendered)
// eslint-disable-next-line
function Page() {
// Rejects queryFn that returns/resolved to undefined or void
// @ts-expect-error (queryFn must not return undefined)
useQueries({ queries: [{ queryKey: key1, queryFn: () => undefined }] })
// @ts-expect-error (queryFn must not return void)
// eslint-disable-next-line @typescript-eslint/no-empty-function
useQueries({ queries: [{ queryKey: key1, queryFn: () => {} }] })

useQueries({
// @ts-expect-error (queryFn must not return explicitly undefined)
queries: [{ queryKey: key1, queryFn: (): undefined => undefined }],
})

useQueries({
// @ts-expect-error (queryFn must not return explicitly void)
queries: [{ queryKey: key1, queryFn: (): void => undefined }],
})

useQueries({
// @ts-expect-error (queryFn must not return explicitly Promise<void>)
queries: [{ queryKey: key1, queryFn: (): Promise<void> => undefined }],
})

useQueries({
queries: [
// @ts-expect-error (queryFn must not return explicitly Promise<undefined>)
{ queryKey: key1, queryFn: (): Promise<undefined> => undefined },
],
})
useQueries({
queries: [
// @ts-expect-error (queryFn must not return Promise<undefined>)
{ queryKey: key2, queryFn: () => Promise.resolve(undefined) },
],
})
useQueries({
// @ts-expect-error (queryFn must not return Promise<undefined>)
queries: Array(50).map((_, i) => ({
queryKey: ['key', i] as const,
queryFn: () => Promise.resolve(undefined),
})),
})

// Rejects queryFn that always throws
useQueries({
queries: [
// @ts-expect-error (queryFn must not return undefined)
{
queryKey: key3,
queryFn: async () => {
throw new Error('')
},
},
],
})

// Accepts queryFn that *sometimes* throws
useQueries({
queries: [
{
queryKey: key3,
queryFn: async () => {
if (Math.random() > 0.1) {
throw new Error('')
}
return 'result'
},
},
],
})

// Array.map preserves TQueryFnData
const result1 = useQueries({
queries: Array(50).map((_, i) => ({
Expand Down Expand Up @@ -898,6 +828,37 @@ describe('useQueries', () => {
someInvalidField: '',
})),
})

// field names should be enforced - array literal
useQueries({
queries: [
{
queryKey: key1,
queryFn: () => 'string',
// @ts-expect-error (invalidField)
someInvalidField: [],
},
],
})

// supports queryFn using fetch() to return Promise<any> - Array.map() result
useQueries({
queries: Array(50).map((_, i) => ({
queryKey: ['key', i] as const,
queryFn: () => fetch('return Promise<any>').then(resp => resp.json()),
})),
})

// supports queryFn using fetch() to return Promise<any> - array literal
useQueries({
queries: [
{
queryKey: key1,
queryFn: () =>
fetch('return Promise<any>').then(resp => resp.json()),
},
],
})
}
})

Expand Down
60 changes: 44 additions & 16 deletions src/reactjs/tests/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
QueryCache,
QueryFunction,
QueryFunctionContext,
UseQueryOptions,
} from '../..'
import { ErrorBoundary } from 'react-error-boundary'

Expand All @@ -36,22 +37,6 @@ describe('useQuery', () => {
expectType<unknown>(noQueryFn.data)
expectType<unknown>(noQueryFn.error)

// it should not be possible for queryFn to return undefined
// @ts-expect-error (queryFn returns undefined)
useQuery(key, () => undefined)

// it should not be possible for queryFn to have explicit void return type
// @ts-expect-error (queryFn explicit return type is void)
useQuery(key, (): void => undefined)

// it should not be possible for queryFn to have explicit Promise<void> return type
// @ts-expect-error (queryFn explicit return type is Promise<void>)
useQuery(key, (): Promise<void> => Promise.resolve())

// it should not be possible for queryFn to have explicit Promise<undefined> return type
// @ts-expect-error (queryFn explicit return type is Promise<undefined>)
useQuery(key, (): Promise<undefined> => Promise.resolve(undefined))

// it should infer the result type from the query function
const fromQueryFn = useQuery(key, () => 'test')
expectType<string | undefined>(fromQueryFn.data)
Expand Down Expand Up @@ -137,6 +122,49 @@ describe('useQuery', () => {
queryKey: ['1'],
queryFn: getMyDataStringKey,
})

// it should handle query-functions that return Promise<any>
useQuery(key, () =>
fetch('return Promise<any>').then(resp => resp.json())
)

// handles wrapped queries with custom fetcher passed as inline queryFn
const useWrappedQuery = <
TQueryKey extends [string, Record<string, unknown>?],
TQueryFnData,
TError,
TData = TQueryFnData
>(
qk: TQueryKey,
fetcher: (
obj: TQueryKey[1],
token: string
// return type must be wrapped with TQueryFnReturn
) => Promise<TQueryFnData>,
options?: Omit<
UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
'queryKey' | 'queryFn'
>
) => useQuery(qk, () => fetcher(qk[1], 'token'), options)
const test = useWrappedQuery([''], async () => '1')
expectType<string | undefined>(test.data)

// handles wrapped queries with custom fetcher passed directly to useQuery
const useWrappedFuncStyleQuery = <
TQueryKey extends [string, Record<string, unknown>?],
TQueryFnData,
TError,
TData = TQueryFnData
>(
qk: TQueryKey,
fetcher: () => Promise<TQueryFnData>,
options?: Omit<
UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
'queryKey' | 'queryFn'
>
) => useQuery(qk, fetcher, options)
const testFuncStyle = useWrappedFuncStyleQuery([''], async () => true)
expectType<boolean | undefined>(testFuncStyle.data)
}
})

Expand Down

0 comments on commit ed43e34

Please sign in to comment.