Skip to content

Commit f1a945f

Browse files
committed
add type tests
1 parent 5d1d7da commit f1a945f

2 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Id } from '@graphprotocol/grc-20';
2+
import { Entity, Type } from '@graphprotocol/hypergraph';
3+
import { describe, expectTypeOf, it } from 'vitest';
4+
import type { useEntities } from '../src/hooks/use-entities.js';
5+
import type { useEntity } from '../src/hooks/use-entity.js';
6+
7+
const Example = Entity.Schema(
8+
{
9+
title: Type.String,
10+
},
11+
{
12+
types: [Id('2f608104-53e2-41bc-9efd-9e0fc984d02e')],
13+
properties: {
14+
title: Id('7f52b98b-6d7d-4131-b5d8-fd2b2cf597a5'),
15+
},
16+
},
17+
);
18+
19+
describe('spaceIds type inference for React hooks', () => {
20+
it('includes spaceIds for useEntity when includeSpaceIds is true', () => {
21+
type Result = ReturnType<typeof useEntity<typeof Example, true, 'public'>>;
22+
expectTypeOf<Result['data']>().toMatchTypeOf<(Entity.Entity<typeof Example> & { spaceIds: string[] }) | null>();
23+
});
24+
25+
it('omits spaceIds for useEntity by default', () => {
26+
type Result = ReturnType<typeof useEntity<typeof Example, false, 'public'>>;
27+
expectTypeOf<Result['data']>().toMatchTypeOf<Entity.Entity<typeof Example> | null>();
28+
expectTypeOf<Result['data']>().not.toMatchTypeOf<Entity.Entity<typeof Example> & { spaceIds: string[] }>();
29+
});
30+
31+
it('includes spaceIds for useEntities when includeSpaceIds is true', () => {
32+
type Result = ReturnType<typeof useEntities<typeof Example, true, 'public'>>;
33+
expectTypeOf<Result['data'][number]>().toMatchTypeOf<Entity.Entity<typeof Example> & { spaceIds: string[] }>();
34+
});
35+
36+
it('omits spaceIds for useEntities by default', () => {
37+
type Result = ReturnType<typeof useEntities<typeof Example, false, 'public'>>;
38+
expectTypeOf<Result['data'][number]>().toMatchTypeOf<Entity.Entity<typeof Example>>();
39+
expectTypeOf<Result['data'][number]>().not.toMatchTypeOf<Entity.Entity<typeof Example> & { spaceIds: string[] }>();
40+
});
41+
});
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)