Open History to see the last commit
1import { describe, expectTypeOf, test } from 'vitest'
2import { get } from 'svelte/store'
3import { skipToken } from '@tanstack/query-core'
4import { createQueries, queryOptions } from '../../src/index.js'
5import type { OmitKeyof, QueryObserverResult } from '@tanstack/query-core'
6import type { CreateQueryOptions } from '../../src/index.js'
7β
8describe('createQueries', () => {
9 test('TData should be defined when passed through queryOptions', () => {
10 const options = queryOptions({
11 queryKey: ['key'],
12 queryFn: () => {
13 return {
14 wow: true,
15 }
16 },
17 initialData: {
18 wow: true,
19 },
20 })
21 const queryResults = createQueries({ queries: [options] })
22β
23 const data = get(queryResults)[0].data
24β
25 expectTypeOf(data).toEqualTypeOf<{ wow: boolean }>()
26 })
27β
28 test('Allow custom hooks using UseQueryOptions', () => {
29 type Data = string
30β
31 const useCustomQueries = (
32 options?: OmitKeyof<CreateQueryOptions<Data>, 'queryKey' | 'queryFn'>,
33 ) => {
34 return createQueries({
35 queries: [
36 {
37 ...options,
38 queryKey: ['todos-key'],
39 queryFn: () => Promise.resolve('data'),
40 },
41 ],
42 })
43 }
44β
45 const query = useCustomQueries()
46 const data = get(query)[0].data
47β
48 expectTypeOf(data).toEqualTypeOf<Data | undefined>()
49 })
50β
51 test('TData should have correct type when conditional skipToken is passed', () => {
52 const queryResults = createQueries({
53 queries: [
54 {
55 queryKey: ['withSkipToken'],
56 queryFn: Math.random() > 0.5 ? skipToken : () => Promise.resolve(5),
57 },
58 ],
59 })
60β
61 const firstResult = get(queryResults)[0]
62β
63 expectTypeOf(firstResult).toEqualTypeOf<
64 QueryObserverResult<number, Error>
65 >()
66 expectTypeOf(firstResult.data).toEqualTypeOf<number | undefined>()
67 })
68})