Skip to content

Commit

Permalink
test for #641
Browse files Browse the repository at this point in the history
  • Loading branch information
saiichihashimoto committed Jan 20, 2024
1 parent c9a25ff commit ed94ef0
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 1 deletion.
201 changes: 201 additions & 0 deletions packages/groq/src/specific-issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ import { expectType } from "@saiichihashimoto/test-utils";
import { evaluate, parse } from "groq-js";
import type { WritableDeep } from "type-fest";

import {
defineArrayMember,
defineConfig,
defineField,
defineType,
} from "@sanity-typed/types";
import type { InferSchemaValues } from "@sanity-typed/types";

import type { ExecuteQuery, Parse } from ".";
import type { ScopeFromPartialContext } from "./internal";

Expand Down Expand Up @@ -95,4 +103,197 @@ describe("specific issues", () => {
>
>().toStrictEqual<WritableDeep<typeof expectedResult>>();
});

it("#641 FIXME", async () => {
// https://github.com/saiichihashimoto/sanity-typed/issues/641
const query = `
*[_type == "foo" && type == "links"]{
_id,
title,
links[]{
_key,
title,
url,
target
}
}
`;

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: { name: "type", type: "AccessAttribute" },
op: "==",
right: { type: "Value", value: "links" },
type: "OpCall",
},
type: "And",
},
type: "Filter",
},
expr: {
base: { type: "This" },
expr: {
attributes: [
{
name: "_id",
type: "ObjectAttributeValue",
value: { name: "_id", type: "AccessAttribute" },
},
{
name: "title",
type: "ObjectAttributeValue",
value: { name: "title", type: "AccessAttribute" },
},
{
name: "links",
type: "ObjectAttributeValue",
value: {
base: {
base: { name: "links", type: "AccessAttribute" },
type: "ArrayCoerce",
},
expr: {
base: { type: "This" },
expr: {
attributes: [
{
name: "_key",
type: "ObjectAttributeValue",
value: { name: "_key", type: "AccessAttribute" },
},
{
name: "title",
type: "ObjectAttributeValue",
value: { name: "title", type: "AccessAttribute" },
},
{
name: "url",
type: "ObjectAttributeValue",
value: { name: "url", type: "AccessAttribute" },
},
{
name: "target",
type: "ObjectAttributeValue",
value: { name: "target", type: "AccessAttribute" },
},
],
type: "Object",
},
type: "Projection",
},
type: "Map",
},
},
],
type: "Object",
},
type: "Projection",
},
type: "Map",
} as const;

expect(tree).toStrictEqual(expectedTree);
expectType<Parse<typeof query>>().toStrictEqual<
WritableDeep<typeof expectedTree>
>();

const config = defineConfig({
dataset: "dataset",
projectId: "projectId",
schema: {
types: [
defineType({
name: "foo",
type: "document",
fields: [
defineField({
name: "type",
type: "string",
options: {
list: [
{ title: "About", value: "about" },
{ title: "Social", value: "social" },
{ title: "Links", value: "links" },
],
},
validation: (Rule) => Rule.required(),
}),
defineField({
name: "title",
type: "string",
validation: (Rule) => Rule.required(),
}),
defineField({
name: "links",
type: "array",
of: [
defineArrayMember({
type: "object",
fields: [
defineField({
name: "title",
type: "string",
}),
defineField({
name: "url",
type: "string",
}),
defineField({
name: "target",
type: "string",
options: {
list: [
{ title: "Self", value: "_self" },
{ title: "Blank", value: "_blank" },
],
},
}),
],
}),
],
}),
],
}),
],
},
});

const dataset: InferSchemaValues<typeof config>["foo"][] = [];

const result = await (await evaluate(tree, { dataset })).get();

const expectedResult = [] as {
_id: string;
links:
| {
_key: string;
target: "_blank" | "_self" | null;
title: string | null;
url: string | null;
}[]
| null;
title: string;
}[];

expect(result).toStrictEqual(expectedResult);
expectType<
ExecuteQuery<
typeof query,
ScopeFromPartialContext<{
dataset: WritableDeep<typeof dataset>;
}>
>
>().toStrictEqual<WritableDeep<typeof expectedResult>>();
});
});
2 changes: 1 addition & 1 deletion packages/types/src/specific-issues.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ describe("specific issues", () => {
}>();
});

it("#589 object -> array -> block -> object -> field", async () => {
it("#589 object -> array -> block -> object -> field", () => {
const config = defineConfig({
dataset: "dataset",
projectId: "projectId",
Expand Down

0 comments on commit ed94ef0

Please sign in to comment.