|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | /* eslint-disable @typescript-eslint/no-unused-vars */ |
6 | | -import { test, describe, expect } from "vitest"; |
| 6 | +import { test, describe, expect, expectTypeOf } from "vitest"; |
7 | 7 | import { anyApi } from "../server/api.js"; |
8 | 8 |
|
9 | | -import { ApiFromModules, QueryBuilder } from "../server/index.js"; |
| 9 | +import type { ApiFromModules, QueryBuilder } from "../server/index.js"; |
10 | 10 | import { useQuery as useQueryReal } from "./client.js"; |
| 11 | +import type { Preloaded } from "./hydration.js"; |
11 | 12 |
|
12 | 13 | // Intentional noop, we're just testing types. |
13 | 14 | const useQuery = (() => {}) as unknown as typeof useQueryReal; |
@@ -68,4 +69,70 @@ describe("useQuery types", () => { |
68 | 69 | // @ts-expect-error adding args is not allowed |
69 | 70 | useQuery(api.module.noArgs, { _arg: 1 }); |
70 | 71 | }); |
| 72 | + |
| 73 | + test("Queries with object options", () => { |
| 74 | + useQuery({ |
| 75 | + query: api.module.noArgs, |
| 76 | + }); |
| 77 | + |
| 78 | + useQuery({ |
| 79 | + query: api.module.noArgs, |
| 80 | + args: {}, |
| 81 | + }); |
| 82 | + |
| 83 | + useQuery({ |
| 84 | + query: api.module.args, |
| 85 | + args: { _arg: "asdf" }, |
| 86 | + }); |
| 87 | + |
| 88 | + useQuery({ |
| 89 | + query: api.module.args, |
| 90 | + args: { _arg: "asdf" }, |
| 91 | + initialValue: "initial value", |
| 92 | + }); |
| 93 | + |
| 94 | + useQuery({ |
| 95 | + query: api.module.args, |
| 96 | + args: { _arg: "asdf" }, |
| 97 | + throwOnError: true, |
| 98 | + }); |
| 99 | + |
| 100 | + useQuery({ |
| 101 | + query: api.module.args, |
| 102 | + args: { _arg: "asdf" }, |
| 103 | + skip: true, |
| 104 | + }); |
| 105 | + |
| 106 | + const { |
| 107 | + status: _status, |
| 108 | + value: _value, |
| 109 | + error: _error, |
| 110 | + } = useQuery({ |
| 111 | + query: api.module.args, |
| 112 | + args: { _arg: "asdf" }, |
| 113 | + initialValue: "initial value", |
| 114 | + throwOnError: true, |
| 115 | + }); |
| 116 | + if (_status === "success") { |
| 117 | + expectTypeOf(_value).toEqualTypeOf("initial value"); |
| 118 | + } |
| 119 | + if (_status === "error") { |
| 120 | + expectTypeOf(_error).toEqualTypeOf<Error>(); |
| 121 | + } |
| 122 | + if (_status === "loading") { |
| 123 | + expectTypeOf(_value).toEqualTypeOf<undefined>(); |
| 124 | + } |
| 125 | + |
| 126 | + useQuery("skip"); |
| 127 | + }); |
| 128 | + |
| 129 | + test("Queries with preloaded options", () => { |
| 130 | + const { |
| 131 | + status: _status, |
| 132 | + value: _value, |
| 133 | + error: _error, |
| 134 | + } = useQuery({ |
| 135 | + preloaded: {} as Preloaded<typeof api.module.noArgs>, |
| 136 | + }); |
| 137 | + }); |
71 | 138 | }); |
0 commit comments