|
1 |
| -import { assertType, describe, expectTypeOf } from 'vitest' |
2 |
| -import { QueryClient } from '@tanstack/query-core' |
3 |
| -import { dataTagSymbol } from '@tanstack/query-core' |
4 |
| -import { queryOptions } from '../query-options' |
5 |
| -import { injectQuery } from '../inject-query' |
6 |
| -import type { Signal } from '@angular/core' |
7 |
| - |
8 |
| -describe('queryOptions', () => { |
9 |
| - test('should not allow excess properties', () => { |
| 1 | +import { assertType, describe, expectTypeOf } from "vitest"; |
| 2 | +import { QueryClient } from "@tanstack/query-core"; |
| 3 | +import { dataTagSymbol } from "@tanstack/query-core"; |
| 4 | +import { queryOptions } from "../query-options"; |
| 5 | +import { injectQuery } from "../inject-query"; |
| 6 | +import type { Signal } from "@angular/core"; |
| 7 | + |
| 8 | +describe("queryOptions", () => { |
| 9 | + test("should not allow excess properties", () => { |
10 | 10 | return queryOptions({
|
11 |
| - queryKey: ['key'], |
| 11 | + queryKey: ["key"], |
12 | 12 | queryFn: () => Promise.resolve(5),
|
13 | 13 | // @ts-expect-error this is a good error, because stallTime does not exist!
|
14 | 14 | stallTime: 1000,
|
15 |
| - }) |
16 |
| - }) |
| 15 | + }); |
| 16 | + }); |
17 | 17 |
|
18 |
| - test('should infer types for callbacks', () => { |
| 18 | + test("should infer types for callbacks", () => { |
19 | 19 | return queryOptions({
|
20 |
| - queryKey: ['key'], |
| 20 | + queryKey: ["key"], |
21 | 21 | queryFn: () => Promise.resolve(5),
|
22 | 22 | staleTime: 1000,
|
23 | 23 | select: (data) => {
|
24 |
| - expectTypeOf(data).toEqualTypeOf<number>() |
| 24 | + expectTypeOf(data).toEqualTypeOf<number>(); |
25 | 25 | },
|
26 |
| - }) |
27 |
| - }) |
28 |
| -}) |
29 |
| - |
30 |
| -test('should work when passed to injectQuery', () => { |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + test("should allow undefined response in initialData", () => { |
| 30 | + return (id: string | null) => |
| 31 | + queryOptions({ |
| 32 | + queryKey: ["todo", id], |
| 33 | + queryFn: () => |
| 34 | + Promise.resolve({ |
| 35 | + id: "1", |
| 36 | + title: "Do Laundry", |
| 37 | + }), |
| 38 | + initialData: () => |
| 39 | + !id |
| 40 | + ? undefined |
| 41 | + : { |
| 42 | + id, |
| 43 | + title: "Initial Data", |
| 44 | + }, |
| 45 | + }); |
| 46 | + }); |
| 47 | +}); |
| 48 | + |
| 49 | +test("should work when passed to injectQuery", () => { |
31 | 50 | const options = queryOptions({
|
32 |
| - queryKey: ['key'], |
| 51 | + queryKey: ["key"], |
33 | 52 | queryFn: () => Promise.resolve(5),
|
34 |
| - }) |
| 53 | + }); |
35 | 54 |
|
36 |
| - const { data } = injectQuery(() => options) |
37 |
| - expectTypeOf(data).toEqualTypeOf<Signal<number | undefined>>() |
38 |
| -}) |
| 55 | + const { data } = injectQuery(() => options); |
| 56 | + expectTypeOf(data).toEqualTypeOf<Signal<number | undefined>>(); |
| 57 | +}); |
39 | 58 |
|
40 |
| -test('should work when passed to fetchQuery', () => { |
| 59 | +test("should work when passed to fetchQuery", () => { |
41 | 60 | const options = queryOptions({
|
42 |
| - queryKey: ['key'], |
| 61 | + queryKey: ["key"], |
43 | 62 | queryFn: () => Promise.resolve(5),
|
44 |
| - }) |
| 63 | + }); |
45 | 64 |
|
46 |
| - const data = new QueryClient().fetchQuery(options) |
47 |
| - assertType<Promise<number>>(data) |
48 |
| -}) |
| 65 | + const data = new QueryClient().fetchQuery(options); |
| 66 | + assertType<Promise<number>>(data); |
| 67 | +}); |
49 | 68 |
|
50 |
| -test('should tag the queryKey with the result type of the QueryFn', () => { |
| 69 | +test("should tag the queryKey with the result type of the QueryFn", () => { |
51 | 70 | const { queryKey } = queryOptions({
|
52 |
| - queryKey: ['key'], |
| 71 | + queryKey: ["key"], |
53 | 72 | queryFn: () => Promise.resolve(5),
|
54 |
| - }) |
55 |
| - assertType<number>(queryKey[dataTagSymbol]) |
56 |
| -}) |
| 73 | + }); |
| 74 | + assertType<number>(queryKey[dataTagSymbol]); |
| 75 | +}); |
57 | 76 |
|
58 |
| -test('should tag the queryKey even if no promise is returned', () => { |
| 77 | +test("should tag the queryKey even if no promise is returned", () => { |
59 | 78 | const { queryKey } = queryOptions({
|
60 |
| - queryKey: ['key'], |
| 79 | + queryKey: ["key"], |
61 | 80 | queryFn: () => 5,
|
62 |
| - }) |
63 |
| - assertType<number>(queryKey[dataTagSymbol]) |
64 |
| -}) |
| 81 | + }); |
| 82 | + assertType<number>(queryKey[dataTagSymbol]); |
| 83 | +}); |
65 | 84 |
|
66 |
| -test('should tag the queryKey with unknown if there is no queryFn', () => { |
| 85 | +test("should tag the queryKey with unknown if there is no queryFn", () => { |
67 | 86 | const { queryKey } = queryOptions({
|
68 |
| - queryKey: ['key'], |
69 |
| - }) |
| 87 | + queryKey: ["key"], |
| 88 | + }); |
70 | 89 |
|
71 |
| - assertType<unknown>(queryKey[dataTagSymbol]) |
72 |
| -}) |
| 90 | + assertType<unknown>(queryKey[dataTagSymbol]); |
| 91 | +}); |
73 | 92 |
|
74 |
| -test('should tag the queryKey with the result type of the QueryFn if select is used', () => { |
| 93 | +test("should tag the queryKey with the result type of the QueryFn if select is used", () => { |
75 | 94 | const { queryKey } = queryOptions({
|
76 |
| - queryKey: ['key'], |
| 95 | + queryKey: ["key"], |
77 | 96 | queryFn: () => Promise.resolve(5),
|
78 | 97 | select: (data) => data.toString(),
|
79 |
| - }) |
| 98 | + }); |
80 | 99 |
|
81 |
| - assertType<number>(queryKey[dataTagSymbol]) |
82 |
| -}) |
| 100 | + assertType<number>(queryKey[dataTagSymbol]); |
| 101 | +}); |
83 | 102 |
|
84 |
| -test('should return the proper type when passed to getQueryData', () => { |
| 103 | +test("should return the proper type when passed to getQueryData", () => { |
85 | 104 | const { queryKey } = queryOptions({
|
86 |
| - queryKey: ['key'], |
| 105 | + queryKey: ["key"], |
87 | 106 | queryFn: () => Promise.resolve(5),
|
88 |
| - }) |
| 107 | + }); |
89 | 108 |
|
90 |
| - const queryClient = new QueryClient() |
91 |
| - const data = queryClient.getQueryData(queryKey) |
| 109 | + const queryClient = new QueryClient(); |
| 110 | + const data = queryClient.getQueryData(queryKey); |
92 | 111 |
|
93 |
| - expectTypeOf(data).toEqualTypeOf<number | undefined>() |
94 |
| -}) |
| 112 | + expectTypeOf(data).toEqualTypeOf<number | undefined>(); |
| 113 | +}); |
95 | 114 |
|
96 |
| -test('should properly type updaterFn when passed to setQueryData', () => { |
| 115 | +test("should properly type updaterFn when passed to setQueryData", () => { |
97 | 116 | const { queryKey } = queryOptions({
|
98 |
| - queryKey: ['key'], |
| 117 | + queryKey: ["key"], |
99 | 118 | queryFn: () => Promise.resolve(5),
|
100 |
| - }) |
| 119 | + }); |
101 | 120 |
|
102 |
| - const queryClient = new QueryClient() |
| 121 | + const queryClient = new QueryClient(); |
103 | 122 | const data = queryClient.setQueryData(queryKey, (prev) => {
|
104 |
| - expectTypeOf(prev).toEqualTypeOf<number | undefined>() |
105 |
| - return prev |
106 |
| - }) |
| 123 | + expectTypeOf(prev).toEqualTypeOf<number | undefined>(); |
| 124 | + return prev; |
| 125 | + }); |
107 | 126 |
|
108 |
| - expectTypeOf(data).toEqualTypeOf<number | undefined>() |
109 |
| -}) |
| 127 | + expectTypeOf(data).toEqualTypeOf<number | undefined>(); |
| 128 | +}); |
110 | 129 |
|
111 |
| -test('should properly type value when passed to setQueryData', () => { |
| 130 | +test("should properly type value when passed to setQueryData", () => { |
112 | 131 | const { queryKey } = queryOptions({
|
113 |
| - queryKey: ['key'], |
| 132 | + queryKey: ["key"], |
114 | 133 | queryFn: () => Promise.resolve(5),
|
115 |
| - }) |
| 134 | + }); |
116 | 135 |
|
117 |
| - const queryClient = new QueryClient() |
| 136 | + const queryClient = new QueryClient(); |
118 | 137 |
|
119 | 138 | // @ts-expect-error value should be a number
|
120 |
| - queryClient.setQueryData(queryKey, '5') |
| 139 | + queryClient.setQueryData(queryKey, "5"); |
121 | 140 | // @ts-expect-error value should be a number
|
122 |
| - queryClient.setQueryData(queryKey, () => '5') |
| 141 | + queryClient.setQueryData(queryKey, () => "5"); |
123 | 142 |
|
124 |
| - const data = queryClient.setQueryData(queryKey, 5) |
| 143 | + const data = queryClient.setQueryData(queryKey, 5); |
125 | 144 |
|
126 |
| - expectTypeOf(data).toEqualTypeOf<number | undefined>() |
127 |
| -}) |
| 145 | + expectTypeOf(data).toEqualTypeOf<number | undefined>(); |
| 146 | +}); |
0 commit comments