|
| 1 | +import { Id } from '@graphprotocol/grc-20'; |
| 2 | +import { describe, expectTypeOf, it } from 'vitest'; |
| 3 | +import type { FindManyPublicParams, findManyPublic } from '../../src/entity/find-many-public.js'; |
| 4 | +import type { FindOnePublicParams, findOnePublic } from '../../src/entity/find-one-public.js'; |
| 5 | +import * as Entity from '../../src/entity/index.js'; |
| 6 | +import type { SearchManyPublicParams, searchManyPublic } from '../../src/entity/search-many-public.js'; |
| 7 | +import * as Type from '../../src/type/type.js'; |
| 8 | + |
| 9 | +const Example = Entity.Schema( |
| 10 | + { |
| 11 | + title: Type.String, |
| 12 | + }, |
| 13 | + { |
| 14 | + types: [Id('e3c9a9ea-4a76-4d93-bcd7-bcfd277f1d43')], |
| 15 | + properties: { |
| 16 | + title: Id('2a2c8a86-1f6c-4051-868f-b0f2eddd86db'), |
| 17 | + }, |
| 18 | + }, |
| 19 | +); |
| 20 | + |
| 21 | +// biome-ignore lint/suspicious/noExplicitAny: fine here |
| 22 | +type AsyncReturnTypeWithParams<Fn extends (...args: any) => any, Params extends any[]> = Fn extends ( |
| 23 | + ...args: Params |
| 24 | +) => infer Result |
| 25 | + ? Awaited<Result> |
| 26 | + : never; |
| 27 | + |
| 28 | +describe('spaceIds type inference for entity helpers', () => { |
| 29 | + it('adds spaceIds for findOnePublic when includeSpaceIds is true', () => { |
| 30 | + type Result = AsyncReturnTypeWithParams< |
| 31 | + typeof findOnePublic, |
| 32 | + [typeof Example, FindOnePublicParams<typeof Example, true>] |
| 33 | + >['entity']; |
| 34 | + expectTypeOf<Result>().toEqualTypeOf<(Entity.Entity<typeof Example> & { spaceIds: string[] }) | null>(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('omits spaceIds for findOnePublic by default', () => { |
| 38 | + type Result = AsyncReturnTypeWithParams< |
| 39 | + typeof findOnePublic, |
| 40 | + [typeof Example, FindOnePublicParams<typeof Example>] |
| 41 | + >['entity']; |
| 42 | + expectTypeOf<Result>().toEqualTypeOf<Entity.Entity<typeof Example> | null>(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('adds spaceIds for findManyPublic when includeSpaceIds is true', () => { |
| 46 | + type Result = AsyncReturnTypeWithParams< |
| 47 | + typeof findManyPublic, |
| 48 | + [typeof Example, FindManyPublicParams<typeof Example, true>] |
| 49 | + >['data'][number]; |
| 50 | + expectTypeOf<Result>().toEqualTypeOf<Entity.Entity<typeof Example> & { spaceIds: string[] }>(); |
| 51 | + }); |
| 52 | + |
| 53 | + it('omits spaceIds for findManyPublic by default', () => { |
| 54 | + type Result = AsyncReturnTypeWithParams< |
| 55 | + typeof findManyPublic, |
| 56 | + [typeof Example, FindManyPublicParams<typeof Example>] |
| 57 | + >['data'][number]; |
| 58 | + expectTypeOf<Result>().toEqualTypeOf<Entity.Entity<typeof Example>>(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('adds spaceIds for searchManyPublic when includeSpaceIds is true', () => { |
| 62 | + type Result = AsyncReturnTypeWithParams< |
| 63 | + typeof searchManyPublic, |
| 64 | + [typeof Example, SearchManyPublicParams<typeof Example, true>] |
| 65 | + >['data'][number]; |
| 66 | + expectTypeOf<Result>().toEqualTypeOf<Entity.Entity<typeof Example> & { spaceIds: string[] }>(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('omits spaceIds for searchManyPublic by default', () => { |
| 70 | + type Result = AsyncReturnTypeWithParams< |
| 71 | + typeof searchManyPublic, |
| 72 | + [typeof Example, SearchManyPublicParams<typeof Example>] |
| 73 | + >['data'][number]; |
| 74 | + expectTypeOf<Result>().toEqualTypeOf<Entity.Entity<typeof Example>>(); |
| 75 | + }); |
| 76 | +}); |
0 commit comments