|
| 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 } from 'vitest'; |
| 10 | +import { buildExpandFields } from '../expand-fields'; |
| 11 | + |
| 12 | +describe('buildExpandFields', () => { |
| 13 | + const sampleFields = { |
| 14 | + name: { type: 'text', label: 'Name' }, |
| 15 | + email: { type: 'email', label: 'Email' }, |
| 16 | + account: { type: 'lookup', label: 'Account', reference_to: 'accounts' }, |
| 17 | + parent: { type: 'master_detail', label: 'Parent', reference_to: 'contacts' }, |
| 18 | + status: { type: 'select', label: 'Status' }, |
| 19 | + }; |
| 20 | + |
| 21 | + it('should return lookup and master_detail field names', () => { |
| 22 | + const result = buildExpandFields(sampleFields); |
| 23 | + expect(result).toEqual(['account', 'parent']); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should return empty array when no lookup/master_detail fields exist', () => { |
| 27 | + const fields = { |
| 28 | + name: { type: 'text' }, |
| 29 | + age: { type: 'number' }, |
| 30 | + }; |
| 31 | + expect(buildExpandFields(fields)).toEqual([]); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should return empty array for null/undefined schema', () => { |
| 35 | + expect(buildExpandFields(null)).toEqual([]); |
| 36 | + expect(buildExpandFields(undefined)).toEqual([]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should return empty array for empty fields object', () => { |
| 40 | + expect(buildExpandFields({})).toEqual([]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should filter by string columns when provided', () => { |
| 44 | + const result = buildExpandFields(sampleFields, ['name', 'account']); |
| 45 | + expect(result).toEqual(['account']); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should filter by ListColumn objects with field property', () => { |
| 49 | + const columns = [ |
| 50 | + { field: 'name', label: 'Name' }, |
| 51 | + { field: 'parent', label: 'Parent Contact' }, |
| 52 | + ]; |
| 53 | + const result = buildExpandFields(sampleFields, columns); |
| 54 | + expect(result).toEqual(['parent']); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should support columns with name property', () => { |
| 58 | + const columns = [ |
| 59 | + { name: 'account', label: 'Account' }, |
| 60 | + ]; |
| 61 | + const result = buildExpandFields(sampleFields, columns); |
| 62 | + expect(result).toEqual(['account']); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should support columns with fieldName property', () => { |
| 66 | + const columns = [ |
| 67 | + { fieldName: 'parent', label: 'Parent' }, |
| 68 | + ]; |
| 69 | + const result = buildExpandFields(sampleFields, columns); |
| 70 | + expect(result).toEqual(['parent']); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return empty array when columns have no lookup fields', () => { |
| 74 | + const result = buildExpandFields(sampleFields, ['name', 'email']); |
| 75 | + expect(result).toEqual([]); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should handle mixed string and object columns', () => { |
| 79 | + const columns = [ |
| 80 | + 'name', |
| 81 | + { field: 'account' }, |
| 82 | + 'parent', |
| 83 | + ]; |
| 84 | + const result = buildExpandFields(sampleFields, columns); |
| 85 | + expect(result).toEqual(['account', 'parent']); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should return all lookup fields when columns is empty array', () => { |
| 89 | + // Empty columns array does not satisfy the length > 0 check, |
| 90 | + // so no column restriction is applied → all lookup fields returned |
| 91 | + const result = buildExpandFields(sampleFields, []); |
| 92 | + expect(result).toEqual(['account', 'parent']); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should handle malformed field definitions gracefully', () => { |
| 96 | + const fields = { |
| 97 | + name: null, |
| 98 | + account: { type: 'lookup' }, |
| 99 | + broken: 'not-an-object', |
| 100 | + empty: {}, |
| 101 | + }; |
| 102 | + const result = buildExpandFields(fields as any); |
| 103 | + expect(result).toEqual(['account']); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should handle only lookup fields', () => { |
| 107 | + const fields = { |
| 108 | + ref1: { type: 'lookup', reference_to: 'obj1' }, |
| 109 | + ref2: { type: 'lookup', reference_to: 'obj2' }, |
| 110 | + }; |
| 111 | + expect(buildExpandFields(fields)).toEqual(['ref1', 'ref2']); |
| 112 | + }); |
| 113 | + |
| 114 | + it('should handle only master_detail fields', () => { |
| 115 | + const fields = { |
| 116 | + detail1: { type: 'master_detail', reference_to: 'obj1' }, |
| 117 | + }; |
| 118 | + expect(buildExpandFields(fields)).toEqual(['detail1']); |
| 119 | + }); |
| 120 | +}); |
0 commit comments