|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 10 | +import { ObjectStackAdapter } from './index'; |
| 11 | + |
| 12 | +// We test the adapter's $expand handling by mocking the underlying ObjectStack client. |
| 13 | +// The key scenarios: |
| 14 | +// 1. find() with $expand should use data.query() (POST) instead of data.find() (GET) |
| 15 | +// 2. find() without $expand should use data.find() (GET) as before |
| 16 | +// 3. findOne() with $expand should use data.query() with an _id filter |
| 17 | +// 4. findOne() without $expand should use data.get() as before |
| 18 | + |
| 19 | +describe('ObjectStackAdapter $expand support', () => { |
| 20 | + let adapter: ObjectStackAdapter; |
| 21 | + let mockClient: any; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + adapter = new ObjectStackAdapter({ |
| 25 | + baseUrl: 'http://localhost:3000', |
| 26 | + autoReconnect: false, |
| 27 | + }); |
| 28 | + |
| 29 | + // Mock the internal client after construction |
| 30 | + mockClient = { |
| 31 | + data: { |
| 32 | + find: vi.fn().mockResolvedValue({ records: [], total: 0 }), |
| 33 | + query: vi.fn().mockResolvedValue({ records: [], total: 0 }), |
| 34 | + get: vi.fn().mockResolvedValue({ record: { _id: '1', name: 'Test' } }), |
| 35 | + }, |
| 36 | + connect: vi.fn().mockResolvedValue(undefined), |
| 37 | + discover: vi.fn().mockResolvedValue({ status: 'ok' }), |
| 38 | + }; |
| 39 | + |
| 40 | + // Inject mock client and mark as connected to bypass connect() |
| 41 | + (adapter as any).client = mockClient; |
| 42 | + (adapter as any).connected = true; |
| 43 | + }); |
| 44 | + |
| 45 | + describe('find() with $expand', () => { |
| 46 | + it('should use data.query() when $expand is present', async () => { |
| 47 | + mockClient.data.query.mockResolvedValue({ |
| 48 | + records: [{ _id: '1', name: 'Order 1', customer: { _id: '2', name: 'Alice' } }], |
| 49 | + total: 1, |
| 50 | + }); |
| 51 | + |
| 52 | + const result = await adapter.find('order', { |
| 53 | + $top: 10, |
| 54 | + $expand: ['customer', 'account'], |
| 55 | + }); |
| 56 | + |
| 57 | + expect(mockClient.data.query).toHaveBeenCalledWith('order', expect.objectContaining({ |
| 58 | + expand: { |
| 59 | + customer: {}, |
| 60 | + account: {}, |
| 61 | + }, |
| 62 | + limit: 10, |
| 63 | + })); |
| 64 | + expect(mockClient.data.find).not.toHaveBeenCalled(); |
| 65 | + expect(result.data).toHaveLength(1); |
| 66 | + expect(result.data[0].customer).toEqual({ _id: '2', name: 'Alice' }); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should pass filters and sort to data.query()', async () => { |
| 70 | + mockClient.data.query.mockResolvedValue({ records: [], total: 0 }); |
| 71 | + |
| 72 | + await adapter.find('order', { |
| 73 | + $filter: [['status', '=', 'active']], |
| 74 | + $orderby: [{ field: 'name', order: 'asc' }], |
| 75 | + $top: 50, |
| 76 | + $skip: 10, |
| 77 | + $expand: ['customer'], |
| 78 | + }); |
| 79 | + |
| 80 | + expect(mockClient.data.query).toHaveBeenCalledWith('order', expect.objectContaining({ |
| 81 | + filters: [['status', '=', 'active']], |
| 82 | + sort: ['name'], |
| 83 | + limit: 50, |
| 84 | + offset: 10, |
| 85 | + expand: { customer: {} }, |
| 86 | + })); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should use data.find() when $expand is not present', async () => { |
| 90 | + mockClient.data.find.mockResolvedValue({ records: [{ _id: '1', name: 'Test' }], total: 1 }); |
| 91 | + |
| 92 | + const result = await adapter.find('order', { $top: 10 }); |
| 93 | + |
| 94 | + expect(mockClient.data.find).toHaveBeenCalled(); |
| 95 | + expect(mockClient.data.query).not.toHaveBeenCalled(); |
| 96 | + expect(result.data).toHaveLength(1); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should use data.find() when $expand is an empty array', async () => { |
| 100 | + mockClient.data.find.mockResolvedValue({ records: [], total: 0 }); |
| 101 | + |
| 102 | + await adapter.find('order', { $top: 10, $expand: [] }); |
| 103 | + |
| 104 | + expect(mockClient.data.find).toHaveBeenCalled(); |
| 105 | + expect(mockClient.data.query).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('findOne() with $expand', () => { |
| 110 | + it('should use data.query() with _id filter when $expand is present', async () => { |
| 111 | + mockClient.data.query.mockResolvedValue({ |
| 112 | + records: [{ _id: 'order-1', name: 'Order 1', customer: { _id: '2', name: 'Alice' } }], |
| 113 | + }); |
| 114 | + |
| 115 | + const result = await adapter.findOne('order', 'order-1', { |
| 116 | + $expand: ['customer', 'account'], |
| 117 | + }); |
| 118 | + |
| 119 | + expect(mockClient.data.query).toHaveBeenCalledWith('order', expect.objectContaining({ |
| 120 | + filters: [['_id', '=', 'order-1']], |
| 121 | + expand: { |
| 122 | + customer: {}, |
| 123 | + account: {}, |
| 124 | + }, |
| 125 | + limit: 1, |
| 126 | + })); |
| 127 | + expect(mockClient.data.get).not.toHaveBeenCalled(); |
| 128 | + expect(result).toEqual({ _id: 'order-1', name: 'Order 1', customer: { _id: '2', name: 'Alice' } }); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should return null when data.query() returns no records', async () => { |
| 132 | + mockClient.data.query.mockResolvedValue({ records: [] }); |
| 133 | + |
| 134 | + const result = await adapter.findOne('order', 'nonexistent', { |
| 135 | + $expand: ['customer'], |
| 136 | + }); |
| 137 | + |
| 138 | + expect(result).toBeNull(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should use data.get() when $expand is not present', async () => { |
| 142 | + mockClient.data.get.mockResolvedValue({ record: { _id: '1', name: 'Test' } }); |
| 143 | + |
| 144 | + const result = await adapter.findOne('order', '1'); |
| 145 | + |
| 146 | + expect(mockClient.data.get).toHaveBeenCalledWith('order', '1'); |
| 147 | + expect(mockClient.data.query).not.toHaveBeenCalled(); |
| 148 | + expect(result).toEqual({ _id: '1', name: 'Test' }); |
| 149 | + }); |
| 150 | + |
| 151 | + it('should return null for 404 errors without $expand', async () => { |
| 152 | + mockClient.data.get.mockRejectedValue({ status: 404 }); |
| 153 | + |
| 154 | + const result = await adapter.findOne('order', 'nonexistent'); |
| 155 | + |
| 156 | + expect(result).toBeNull(); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments