-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathspecific-issues.test.ts
98 lines (89 loc) · 2.67 KB
/
specific-issues.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
import { describe, expect, it } from "@jest/globals";
import { expectType } from "@saiichihashimoto/test-utils";
import { evaluate, parse } from "groq-js";
import type { WritableDeep } from "type-fest";
import type { ExecuteQuery, Parse } from ".";
import type { ScopeFromPartialContext } from "./internal";
describe("specific issues", () => {
it("#242", async () => {
// https://github.com/saiichihashimoto/sanity-typed/issues/242
const query = '*[_type == "foo" && slug.current == $slug]{ title, slug }';
const tree = parse(query);
const expectedTree = {
base: {
base: { type: "Everything" },
expr: {
left: {
left: { name: "_type", type: "AccessAttribute" },
op: "==",
right: { type: "Value", value: "foo" },
type: "OpCall",
},
right: {
left: {
base: { name: "slug", type: "AccessAttribute" },
name: "current",
type: "AccessAttribute",
},
op: "==",
right: { name: "slug", type: "Parameter" },
type: "OpCall",
},
type: "And",
},
type: "Filter",
},
expr: {
base: { type: "This" },
expr: {
attributes: [
{
name: "title",
type: "ObjectAttributeValue",
value: { name: "title", type: "AccessAttribute" },
},
{
name: "slug",
type: "ObjectAttributeValue",
value: { name: "slug", type: "AccessAttribute" },
},
],
type: "Object",
},
type: "Projection",
},
type: "Map",
} as const;
expect(tree).toStrictEqual(expectedTree);
expectType<Parse<typeof query>>().toStrictEqual<
WritableDeep<typeof expectedTree>
>();
const dataset = [
{
_type: "bar",
slug: { _type: "slug", current: "bar" },
title: "notTitle",
},
{
_type: "foo",
slug: { _type: "slug", current: "foo" },
title: "title",
},
] as const;
const params = { slug: "foo" } as const;
const result = await (await evaluate(tree, { dataset, params })).get();
const expectedResult = [
{ slug: { _type: "slug", current: "foo" }, title: "title" },
] as const;
expect(result).toStrictEqual(expectedResult);
expectType<
ExecuteQuery<
typeof query,
ScopeFromPartialContext<{
dataset: WritableDeep<typeof dataset>;
params: WritableDeep<typeof params>;
}>
>
>().toStrictEqual<WritableDeep<typeof expectedResult>>();
});
});