Skip to content

Commit 5b53a19

Browse files
committed
Added tests for public query, fixed linting and all
1 parent 7e9b347 commit 5b53a19

5 files changed

Lines changed: 93 additions & 50 deletions

File tree

packages/hypergraph-react/src/internal/translate-filter-to-graphql.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,53 @@ describe('translateFilterToGraphql with complex nested filters', () => {
346346
});
347347
});
348348
});
349+
350+
describe('translateFilterToGraphql date/point filters', () => {
351+
class Event extends Entity.Class<Event>('Event')({
352+
name: Type.String,
353+
when: Type.Date,
354+
location: Type.Point,
355+
}) {}
356+
357+
const eventMapping: Mapping.Mapping = {
358+
Event: {
359+
typeIds: [Id('a288444f-06a3-4037-9ace-66fe325864d0')],
360+
properties: {
361+
name: Id('a126ca53-0c8e-48d5-b888-82c734c38935'),
362+
when: Id('9b53690f-ea6d-4bd8-b4d3-9ea01e7f837f'),
363+
location: Id('45e707a5-4364-42fb-bb0b-927a5a8bc061'),
364+
},
365+
},
366+
};
367+
368+
it('should translate Date `is` filter correctly', () => {
369+
const dt = new Date('2024-01-01T00:00:00Z');
370+
const filter: Entity.EntityFilter<Event> = { when: { is: dt } };
371+
372+
const result = translateFilterToGraphql(filter, Event, eventMapping);
373+
374+
expect(result).toEqual({
375+
values: {
376+
some: {
377+
propertyId: { is: '9b53690f-ea6d-4bd8-b4d3-9ea01e7f837f' },
378+
time: { is: Graph.serializeDate(dt) },
379+
},
380+
},
381+
});
382+
});
383+
384+
it('should translate Point `is` filter correctly', () => {
385+
const filter: Entity.EntityFilter<Event> = { location: { is: [1, 2] as const } };
386+
387+
const result = translateFilterToGraphql(filter, Event, eventMapping);
388+
389+
expect(result).toEqual({
390+
values: {
391+
some: {
392+
propertyId: { is: '45e707a5-4364-42fb-bb0b-927a5a8bc061' },
393+
point: { is: Graph.serializePoint([1, 2]) },
394+
},
395+
},
396+
});
397+
});
398+
});

packages/hypergraph-react/src/internal/translate-filter-to-graphql.ts

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ type GraphqlFilterEntry =
1919
number: { is: string } | { greaterThan: string } | { lessThan: string };
2020
}
2121
| {
22-
propertyId: {is: string};
23-
time: {is: string};
22+
propertyId: { is: string };
23+
time: { is: string };
2424
}
2525
| {
26-
propertyId: {is: string};
27-
point: {is: string};
26+
propertyId: { is: string };
27+
point: { is: string };
2828
};
2929
};
3030
}
@@ -136,24 +136,28 @@ export function translateFilterToGraphql<S extends Entity.AnyNoContext>(
136136

137137
if (TypeUtils.isDateOrOptionalDateType(type.fields[fieldName]) && fieldFilter.is instanceof Date) {
138138
graphqlFilter.push({
139-
values:{
140-
some:{
141-
propertyId:{is:propertyId},
142-
time: {is: Graph.serializeDate(fieldFilter.is)},
139+
values: {
140+
some: {
141+
propertyId: { is: propertyId },
142+
time: { is: Graph.serializeDate(fieldFilter.is) },
143143
},
144144
},
145145
});
146146
}
147147

148-
if(TypeUtils.isPointOrOptionalPointType(type.fields[fieldName]) && Array.isArray(fieldFilter.is)&& fieldFilter.is.length === 2){
148+
if (
149+
TypeUtils.isPointOrOptionalPointType(type.fields[fieldName]) &&
150+
Array.isArray(fieldFilter.is) &&
151+
fieldFilter.is.length === 2
152+
) {
149153
graphqlFilter.push({
150-
values:{
151-
some:{
152-
propertyId: {is:propertyId},
154+
values: {
155+
some: {
156+
propertyId: { is: propertyId },
153157
point: { is: Graph.serializePoint([...(fieldFilter.is as ReadonlyArray<number>)]) },
154-
}
155-
}
156-
})
158+
},
159+
},
160+
});
157161
}
158162
}
159163
}

packages/hypergraph/src/entity/findMany.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,14 @@ export function findMany<const S extends AnyNoContext>(
308308
}
309309
}
310310
if (fieldValue instanceof Date && 'is' in fieldFilter) {
311-
const target = (fieldFilter as {is? : Date}).is;
312-
if (target instanceof Date){
311+
const target = (fieldFilter as { is?: Date }).is;
312+
if (target instanceof Date) {
313313
return fieldValue.getTime() === target.getTime();
314314
}
315315
}
316316

317-
if(Array.isArray(fieldValue) && fieldValue.length === 2 && 'is' in fieldFilter){
318-
const target = (fieldFilter as {is? : ReadonlyArray<number>}).is;
317+
if (Array.isArray(fieldValue) && fieldValue.length === 2 && 'is' in fieldFilter) {
318+
const target = (fieldFilter as { is?: ReadonlyArray<number> }).is;
319319
if (Array.isArray(target) && target.length === 2) {
320320
return fieldValue[0] === target[0] && fieldValue[1] === target[1];
321321
}

packages/hypergraph/src/entity/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ export type EntityFieldFilter<T> = {
8888
endsWith?: string;
8989
contains?: string;
9090
}
91-
: T extends Date
92-
? {
93-
is?: Date
94-
}
95-
: T extends ReadonlyArray<number>
91+
: T extends Date
9692
? {
97-
is?: ReadonlyArray<number>
93+
is?: Date;
9894
}
99-
: Record<string, never>);
95+
: T extends ReadonlyArray<number>
96+
? {
97+
is?: ReadonlyArray<number>;
98+
}
99+
: Record<string, never>);
100100

101101
export type EntityFilter<T> = CrossFieldFilter<T>;

packages/hypergraph/test/entity/findMany.test.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,11 @@ describe('findMany with filters', () => {
507507
});
508508
});
509509

510-
describe('Date & Point filters',()=>{
510+
describe('Date & Point filters', () => {
511511
class Meetup extends Entity.Class<Meetup>('Meetup')({
512-
name:Type.String,
513-
when:Type.Date,
514-
location:Type.Point,
512+
name: Type.String,
513+
when: Type.Date,
514+
location: Type.Point,
515515
}) {}
516516

517517
const spaceId = '9a4a5a1b-2c3d-4e5f-8a9b-1234567890ab';
@@ -520,37 +520,26 @@ describe('Date & Point filters',()=>{
520520
let repo: Repo;
521521
let handle: DocHandle<Entity.DocumentContent>;
522522

523-
beforeEach(()=>{
523+
beforeEach(() => {
524524
repo = new Repo({});
525525
const result = repo.findWithProgress<Entity.DocumentContent>(automergeDocId);
526526
handle = result.handle;
527527
handle.doneLoading();
528-
})
528+
});
529529

530-
it('filters by Date is ',()=>{
531-
Entity.create(handle,Meetup)({name:'A',when:new Date('2024-01-01T00:00:00Z'),location:[0,0]});
530+
it('filters by Date is ', () => {
531+
Entity.create(handle, Meetup)({ name: 'A', when: new Date('2024-01-01T00:00:00Z'), location: [0, 0] });
532532
Entity.create(handle, Meetup)({ name: 'B', when: new Date('2025-01-01T00:00:00Z'), location: [0, 0] });
533533

534-
const r = Entity.findMany(
535-
handle,
536-
Meetup,
537-
{when:{is:new Date('2024-01-01T00:00:00Z')}},
538-
undefined,
539-
);
540-
expect(r.entities.map(e=>e.name)).toEqual(['A']);
534+
const r = Entity.findMany(handle, Meetup, { when: { is: new Date('2024-01-01T00:00:00Z') } }, undefined);
535+
expect(r.entities.map((e) => e.name)).toEqual(['A']);
541536
});
542537

543-
it('filters by Point is ',()=>{
538+
it('filters by Point is ', () => {
544539
Entity.create(handle, Meetup)({ name: 'X', when: new Date('2024-01-01T00:00:00Z'), location: [1, 2] });
545540
Entity.create(handle, Meetup)({ name: 'Y', when: new Date('2024-01-01T00:00:00Z'), location: [3, 4] });
546541

547-
const r = Entity.findMany(
548-
handle,
549-
Meetup,
550-
{location:{is:[1,2]}},
551-
undefined
552-
);
553-
expect(r.entities.map(e=>e.name)).toEqual(['X']);
542+
const r = Entity.findMany(handle, Meetup, { location: { is: [1, 2] } }, undefined);
543+
expect(r.entities.map((e) => e.name)).toEqual(['X']);
554544
});
555-
556545
});

0 commit comments

Comments
 (0)