-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(svelte-query): Correct data type when
initialData
is set (#7769)
* Fix createQueries initialData type * Fix createQuery types
- Loading branch information
1 parent
e4e65be
commit b764009
Showing
9 changed files
with
287 additions
and
67 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
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
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
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
68 changes: 68 additions & 0 deletions
68
packages/svelte-query/tests/createQueries/createQueries.test-d.ts
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { describe, expectTypeOf, test } from 'vitest' | ||
import { get } from 'svelte/store' | ||
import { skipToken } from '@tanstack/query-core' | ||
import { createQueries, queryOptions } from '../../src/index' | ||
import type { OmitKeyof, QueryObserverResult } from '@tanstack/query-core' | ||
import type { CreateQueryOptions } from '../../src/index' | ||
|
||
describe('createQueries', () => { | ||
test('TData should be defined when passed through queryOptions', () => { | ||
const options = queryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => { | ||
return { | ||
wow: true, | ||
} | ||
}, | ||
initialData: { | ||
wow: true, | ||
}, | ||
}) | ||
const queryResults = createQueries({ queries: [options] }) | ||
|
||
const data = get(queryResults)[0].data | ||
|
||
expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>() | ||
}) | ||
|
||
test('Allow custom hooks using UseQueryOptions', () => { | ||
type Data = string | ||
|
||
const useCustomQueries = ( | ||
options?: OmitKeyof<CreateQueryOptions<Data>, 'queryKey' | 'queryFn'>, | ||
) => { | ||
return createQueries({ | ||
queries: [ | ||
{ | ||
...options, | ||
queryKey: ['todos-key'], | ||
queryFn: () => Promise.resolve('data'), | ||
}, | ||
], | ||
}) | ||
} | ||
|
||
const query = useCustomQueries() | ||
const data = get(query)[0].data | ||
|
||
expectTypeOf(data).toEqualTypeOf<Data | undefined>() | ||
}) | ||
|
||
test('TData should have correct type when conditional skipToken is passed', () => { | ||
const queryResults = createQueries({ | ||
queries: [ | ||
{ | ||
queryKey: ['withSkipToken'], | ||
queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5), | ||
}, | ||
], | ||
}) | ||
|
||
const firstResult = get(queryResults)[0] | ||
|
||
expectTypeOf(firstResult).toEqualTypeOf< | ||
QueryObserverResult<number, Error> | ||
>() | ||
expectTypeOf(firstResult.data).toEqualTypeOf<number | undefined>() | ||
}) | ||
}) |
65 changes: 65 additions & 0 deletions
65
packages/svelte-query/tests/createQuery/createQuery.test-d.ts
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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { describe, expectTypeOf, test } from 'vitest' | ||
import { get } from 'svelte/store' | ||
import { createQuery, queryOptions } from '../../src/index' | ||
import type { OmitKeyof } from '@tanstack/query-core' | ||
import type { CreateQueryOptions } from '../../src/index' | ||
|
||
describe('createQuery', () => { | ||
test('TData should always be defined when initialData is provided as an object', () => { | ||
const query = createQuery({ | ||
queryKey: ['key'], | ||
queryFn: () => ({ wow: true }), | ||
initialData: { wow: true }, | ||
}) | ||
|
||
expectTypeOf(get(query).data).toEqualTypeOf<{ wow: boolean }>() | ||
}) | ||
|
||
test('TData should be defined when passed through queryOptions', () => { | ||
const options = queryOptions({ | ||
queryKey: ['key'], | ||
queryFn: () => { | ||
return { | ||
wow: true, | ||
} | ||
}, | ||
initialData: { | ||
wow: true, | ||
}, | ||
}) | ||
const query = createQuery(options) | ||
|
||
expectTypeOf(get(query).data).toEqualTypeOf<{ wow: boolean }>() | ||
}) | ||
|
||
test('TData should have undefined in the union when initialData is NOT provided', () => { | ||
const query = createQuery({ | ||
queryKey: ['key'], | ||
queryFn: () => { | ||
return { | ||
wow: true, | ||
} | ||
}, | ||
}) | ||
|
||
expectTypeOf(get(query).data).toEqualTypeOf<{ wow: boolean } | undefined>() | ||
}) | ||
|
||
test('Allow custom hooks using CreateQueryOptions', () => { | ||
type Data = string | ||
|
||
const useCustomQuery = ( | ||
options?: OmitKeyof<CreateQueryOptions<Data>, 'queryKey' | 'queryFn'>, | ||
) => { | ||
return createQuery({ | ||
...options, | ||
queryKey: ['todos-key'], | ||
queryFn: () => Promise.resolve('data'), | ||
}) | ||
} | ||
|
||
const query = useCustomQuery() | ||
|
||
expectTypeOf(get(query).data).toEqualTypeOf<Data | undefined>() | ||
}) | ||
}) |
Oops, something went wrong.