-
Notifications
You must be signed in to change notification settings - Fork 16
/
fragment.test.ts
199 lines (181 loc) · 5.4 KB
/
fragment.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { describe, it, expect, expectTypeOf } from "vitest";
import { SanitySchema, SchemaConfig } from "../tests/schemas/nextjs-sanity-fe";
import { InferFragmentType, InferResultType } from "../types/public-types";
import { createGroqBuilder } from "../index";
import { TypeMismatchError } from "../types/utils";
const q = createGroqBuilder<SchemaConfig>({ indent: " " });
describe("fragment", () => {
// define a fragment:
const variantFragment = q.fragment<SanitySchema.Variant>().project({
name: true,
price: true,
slug: "slug.current",
});
type VariantFragment = InferFragmentType<typeof variantFragment>;
it("simple fragment should have the correct type", () => {
expectTypeOf<VariantFragment>().toEqualTypeOf<{
name: string;
price: number;
slug: string;
}>();
});
const productFrag = q.fragment<SanitySchema.Product>().project((qP) => ({
name: true,
slug: "slug.current",
variants: qP
.field("variants[]")
.deref()
.project({
...variantFragment,
msrp: true,
}),
}));
type ProductFragment = InferFragmentType<typeof productFrag>;
it("nested fragments should have the correct types", () => {
expectTypeOf<ProductFragment>().toEqualTypeOf<{
name: string;
slug: string;
variants: null | Array<{
name: string;
price: number;
slug: string;
msrp: number;
}>;
}>();
});
it("fragments can be used in a query", () => {
const qVariants = q.star.filterByType("variant").project(variantFragment);
expectTypeOf<InferResultType<typeof qVariants>>().toEqualTypeOf<
Array<VariantFragment>
>();
expect(qVariants.query).toMatchInlineSnapshot(
`
"*[_type == "variant"] {
name,
price,
"slug": slug.current
}"
`
);
});
it("fragments can be spread in a query", () => {
const qVariantsPlus = q.star.filterByType("variant").project({
...variantFragment,
msrp: true,
});
expectTypeOf<InferResultType<typeof qVariantsPlus>>().toEqualTypeOf<
Array<{ name: string; price: number; slug: string; msrp: number }>
>();
expect(qVariantsPlus.query).toMatchInlineSnapshot(
`
"*[_type == "variant"] {
name,
price,
"slug": slug.current,
msrp
}"
`
);
});
it("should have errors if the variant is used incorrectly", () => {
// @ts-expect-error ---
const qInvalid = q.star.filterByType("product").project(variantFragment);
expectTypeOf<
InferResultType<typeof qInvalid>[number]["price"]
>().toEqualTypeOf<
TypeMismatchError<{
error: "⛔️ 'true' can only be used for known properties ⛔️";
expected: keyof SanitySchema.Product;
actual: "price";
}>
>();
});
it("can be composed", () => {
const idFrag = q.fragment<SanitySchema.Variant>().project({ id: true });
const variantDetailsFrag = q.fragment<SanitySchema.Variant>().project({
...idFrag,
...variantFragment,
msrp: true,
});
type VariantDetails = InferFragmentType<typeof variantDetailsFrag>;
expectTypeOf<VariantDetails>().toEqualTypeOf<{
slug: string;
name: string;
msrp: number;
price: number;
id: string | null;
}>();
});
it("can be used to query multiple types", () => {
const commonFrag = q
.fragment<
SanitySchema.Product | SanitySchema.Variant | SanitySchema.Category
>()
.project({
_type: true,
_id: true,
name: true,
});
type CommonFrag = InferFragmentType<typeof commonFrag>;
expectTypeOf<CommonFrag>().toEqualTypeOf<{
_type: "product" | "variant" | "category";
_id: string;
name: string;
}>();
});
describe("fragments can use conditionals", () => {
const fragmentWithConditional = q
.fragment<SanitySchema.Variant>()
.project((qP) => ({
name: true,
...qP.conditional({
"price == msrp": { onSale: q.value(false) },
"price < msrp": { onSale: q.value(true), price: true, msrp: true },
}),
}));
const qConditional = q.star.filterByType("variant").project({
slug: "slug.current",
...fragmentWithConditional,
});
it("the inferred type is correct", () => {
expectTypeOf<
InferFragmentType<typeof fragmentWithConditional>
>().toEqualTypeOf<
| { name: string }
| { name: string; onSale: false }
| { name: string; onSale: true; price: number; msrp: number }
>();
});
it("the fragment can be used in a query", () => {
expectTypeOf<InferResultType<typeof qConditional>>().toEqualTypeOf<
Array<
| { slug: string; name: string }
| { slug: string; name: string; onSale: false }
| {
slug: string;
name: string;
onSale: true;
price: number;
msrp: number;
}
>
>();
});
it("the query is compiled correctly", () => {
expect(qConditional.query).toMatchInlineSnapshot(`
"*[_type == "variant"] {
"slug": slug.current,
name,
price == msrp => {
"onSale": false
},
price < msrp => {
"onSale": true,
price,
msrp
}
}"
`);
});
});
});