-
-
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.
- Loading branch information
1 parent
837bb0e
commit f1fb234
Showing
3 changed files
with
119 additions
and
5 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
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>() | ||
}) | ||
}) |