|
| 1 | +import { Id } from '@geoprotocol/geo-sdk'; |
| 2 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 3 | +import { findManyPublic } from '../../src/entity/find-many-public.js'; |
| 4 | +import * as Entity from '../../src/entity/index.js'; |
| 5 | +import * as Type from '../../src/type/type.js'; |
| 6 | +import { getOrderByDataType } from '../../src/utils/convert-property-value.js'; |
| 7 | + |
| 8 | +const mockRequest = vi.hoisted(() => vi.fn()); |
| 9 | + |
| 10 | +vi.mock('graphql-request', () => ({ |
| 11 | + request: mockRequest, |
| 12 | +})); |
| 13 | + |
| 14 | +const TITLE_PROPERTY_ID = Id('79c1a9510074401087d07501ef9d7b3d'); |
| 15 | +const SCORE_PROPERTY_ID = Id('0f0f62df02194f16983ad2ae5fc43ee5'); |
| 16 | +const CHILDREN_RELATION_PROPERTY_ID = Id('ca7c7167250249c490b084c147f9b12b'); |
| 17 | +const CHILD_NAME_PROPERTY_ID = Id('25584af039414ab986f7a603305b19bb'); |
| 18 | + |
| 19 | +const Child = Entity.Schema( |
| 20 | + { |
| 21 | + name: Type.String, |
| 22 | + }, |
| 23 | + { |
| 24 | + types: [Id('3c2ae3aa4ec141e3bc4c1fe7a5e07bc1')], |
| 25 | + properties: { |
| 26 | + name: CHILD_NAME_PROPERTY_ID, |
| 27 | + }, |
| 28 | + }, |
| 29 | +); |
| 30 | + |
| 31 | +const Parent = Entity.Schema( |
| 32 | + { |
| 33 | + title: Type.String, |
| 34 | + score: Type.Number, |
| 35 | + children: Type.Relation(Child), |
| 36 | + }, |
| 37 | + { |
| 38 | + types: [Id('af571d8c06d44add8cfa4c6b50412254')], |
| 39 | + properties: { |
| 40 | + title: TITLE_PROPERTY_ID, |
| 41 | + score: SCORE_PROPERTY_ID, |
| 42 | + children: CHILDREN_RELATION_PROPERTY_ID, |
| 43 | + }, |
| 44 | + }, |
| 45 | +); |
| 46 | + |
| 47 | +describe('findManyPublic orderBy', () => { |
| 48 | + beforeEach(() => { |
| 49 | + mockRequest.mockReset(); |
| 50 | + mockRequest.mockResolvedValue({ entities: [] }); |
| 51 | + }); |
| 52 | + |
| 53 | + it('passes inferred dataType for sortable fields', async () => { |
| 54 | + await findManyPublic(Parent, { |
| 55 | + space: 'space-1', |
| 56 | + orderBy: { |
| 57 | + property: 'score', |
| 58 | + direction: 'desc', |
| 59 | + }, |
| 60 | + logInvalidResults: false, |
| 61 | + }); |
| 62 | + |
| 63 | + expect(mockRequest).toHaveBeenCalledTimes(1); |
| 64 | + const [, queryDocument, queryVariables] = mockRequest.mock.calls[0]; |
| 65 | + |
| 66 | + expect(queryDocument as string).toContain('$dataType: String'); |
| 67 | + expect(queryDocument as string).toContain('dataType: $dataType'); |
| 68 | + expect(queryVariables).toMatchObject({ |
| 69 | + propertyId: SCORE_PROPERTY_ID, |
| 70 | + dataType: 'float', |
| 71 | + sortDirection: 'DESC', |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('omits dataType for unresolved orderBy field types', async () => { |
| 76 | + await findManyPublic(Parent, { |
| 77 | + space: 'space-1', |
| 78 | + orderBy: { |
| 79 | + property: 'children', |
| 80 | + direction: 'asc', |
| 81 | + }, |
| 82 | + logInvalidResults: false, |
| 83 | + }); |
| 84 | + |
| 85 | + expect(mockRequest).toHaveBeenCalledTimes(1); |
| 86 | + const [, queryDocument, queryVariables] = mockRequest.mock.calls[0]; |
| 87 | + |
| 88 | + expect(queryDocument as string).not.toContain('$dataType: String'); |
| 89 | + expect(queryDocument as string).not.toContain('dataType: $dataType'); |
| 90 | + expect(queryVariables).toMatchObject({ |
| 91 | + propertyId: CHILDREN_RELATION_PROPERTY_ID, |
| 92 | + sortDirection: 'ASC', |
| 93 | + }); |
| 94 | + expect((queryVariables as Record<string, unknown>).dataType).toBeUndefined(); |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +describe('getOrderByDataType', () => { |
| 99 | + it('maps schema builder outputs to GraphQL order dataType values', () => { |
| 100 | + expect(getOrderByDataType(Type.String('prop').ast)).toBe('text'); |
| 101 | + expect(getOrderByDataType(Type.Number('prop').ast)).toBe('float'); |
| 102 | + expect(getOrderByDataType(Type.Boolean('prop').ast)).toBe('boolean'); |
| 103 | + expect(getOrderByDataType(Type.Date('prop').ast)).toBe('datetime'); |
| 104 | + expect(getOrderByDataType(Type.Point('prop').ast)).toBe('point'); |
| 105 | + expect(getOrderByDataType(Type.ScheduleString('prop').ast)).toBe('schedule'); |
| 106 | + expect(getOrderByDataType(Type.Relation(Child)('prop').ast)).toBeUndefined(); |
| 107 | + }); |
| 108 | +}); |
0 commit comments