|
1 | | -import { describe, it, expectTypeOf } from "vitest"; |
| 1 | +import { describe, expectTypeOf, it } from "vitest"; |
| 2 | +import { SanitySchema } from "../tests/schemas/nextjs-sanity-fe"; |
| 3 | +import { Slug } from "../tests/schemas/nextjs-sanity-fe.sanity-typegen"; |
2 | 4 | import { |
3 | 5 | ProjectionPathEntries, |
4 | 6 | ProjectionPaths, |
5 | 7 | ProjectionPathsByType, |
6 | 8 | ProjectionPathValue, |
7 | 9 | } from "./projection-paths"; |
8 | | -import { SanitySchema } from "../tests/schemas/nextjs-sanity-fe"; |
9 | | -import { Slug } from "../tests/schemas/nextjs-sanity-fe.sanity-typegen"; |
10 | 10 | import { UndefinedToNull } from "./utils"; |
11 | 11 |
|
12 | 12 | type TestObject = { |
@@ -252,12 +252,65 @@ describe("ProjectionPathEntries", () => { |
252 | 252 | >(); |
253 | 253 | }); |
254 | 254 |
|
| 255 | + describe("built-in types should not be deeply parsed", () => { |
| 256 | + type Block = SanitySchema.Description[number]; |
| 257 | + type Reference = NonNullable<SanitySchema.Variant["flavour"]>[number]; |
| 258 | + type Example = { |
| 259 | + blocks: Block[]; |
| 260 | + ref: Reference; |
| 261 | + optionalRef?: Reference; |
| 262 | + deep: { foo: "bar" }; |
| 263 | + }; |
| 264 | + it("should not show deep results for built-in types", () => { |
| 265 | + expectTypeOf<ProjectionPathEntries<Example>>().toEqualTypeOf<{ |
| 266 | + "blocks[]": Block[]; |
| 267 | + ref: Reference; |
| 268 | + optionalRef: Reference | null; |
| 269 | + deep: Example["deep"]; |
| 270 | + "deep.foo": "bar"; |
| 271 | + }>(); |
| 272 | + }); |
| 273 | + describe("when at the top level", () => { |
| 274 | + it("should show deep results for a 'block'", () => { |
| 275 | + type Actual = ProjectionPathEntries<Block>; |
| 276 | + type Expected = { |
| 277 | + _type: "block"; |
| 278 | + _key: string; |
| 279 | + style: UndefinedToNull<Block["style"]>; |
| 280 | + level: UndefinedToNull<Block["level"]>; |
| 281 | + listItem: UndefinedToNull<Block["listItem"]>; |
| 282 | + "children[]": UndefinedToNull<Block["children"]>; |
| 283 | + "children[]._type": null | Array<"span">; |
| 284 | + "children[]._key": null | Array<string>; |
| 285 | + "children[].text": null | Array<string | null>; |
| 286 | + "children[].marks[]": null | Array<string[] | null>; |
| 287 | + "markDefs[]": UndefinedToNull<Block["markDefs"]>; |
| 288 | + "markDefs[]._type": null | Array<"link">; |
| 289 | + "markDefs[]._key": null | Array<string>; |
| 290 | + "markDefs[].href": null | Array<string | null>; |
| 291 | + }; |
| 292 | + |
| 293 | + expectTypeOf<Actual>().toEqualTypeOf<Expected>(); |
| 294 | + }); |
| 295 | + it("should show deep results for a 'reference'", () => { |
| 296 | + type Actual = ProjectionPathEntries<Reference>; |
| 297 | + type Expected = { |
| 298 | + _type: "reference"; |
| 299 | + _key: string; |
| 300 | + _ref: string; |
| 301 | + _weak: null | boolean; |
| 302 | + }; |
| 303 | + expectTypeOf<Actual>().toEqualTypeOf<Expected>(); |
| 304 | + }); |
| 305 | + }); |
| 306 | + }); |
| 307 | + |
255 | 308 | describe("when dealing with unions", () => { |
256 | 309 | type Union = { _type: "TypeA"; a: "A" } | { _type: "TypeB"; b: "B" }; |
257 | 310 |
|
258 | 311 | it("should only return types for common properties", () => { |
259 | | - type Result = ProjectionPathEntries<Union>; |
260 | | - expectTypeOf<Result>().toEqualTypeOf<{ |
| 312 | + type Actual = ProjectionPathEntries<Union>; |
| 313 | + expectTypeOf<Actual>().toEqualTypeOf<{ |
261 | 314 | _type: "TypeA" | "TypeB"; |
262 | 315 | }>(); |
263 | 316 | }); |
|
0 commit comments