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