|
| 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 { |
| 11 | + normalizeFieldReferenceKeys, |
| 12 | + normalizeSchemaReferenceKeys, |
| 13 | +} from '../reference-keys'; |
| 14 | + |
| 15 | +describe('normalizeFieldReferenceKeys', () => { |
| 16 | + it('stamps reference_to from the ObjectStack-convention `reference` key', () => { |
| 17 | + // Exactly what the showcase backend serves for showcase_project.account. |
| 18 | + const field = { type: 'lookup', reference: 'showcase_account' } as any; |
| 19 | + normalizeFieldReferenceKeys(field); |
| 20 | + expect(field.reference_to).toBe('showcase_account'); |
| 21 | + expect(field.reference).toBe('showcase_account'); |
| 22 | + }); |
| 23 | + |
| 24 | + it('stamps `reference` from a reference_to-keyed (ObjectUI-authored) field', () => { |
| 25 | + const field = { type: 'master_detail', reference_to: 'showcase_project' } as any; |
| 26 | + normalizeFieldReferenceKeys(field); |
| 27 | + expect(field.reference).toBe('showcase_project'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('accepts legacy camelCase referenceTo as a source', () => { |
| 31 | + const field = { type: 'lookup', referenceTo: 'accounts' } as any; |
| 32 | + normalizeFieldReferenceKeys(field); |
| 33 | + expect(field.reference_to).toBe('accounts'); |
| 34 | + expect(field.reference).toBe('accounts'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('never overwrites keys that are already set', () => { |
| 38 | + // Divergent keys are broken metadata; first-wins (reference_to) but the |
| 39 | + // existing `reference` value must not be clobbered. |
| 40 | + const field = { reference_to: 'a', reference: 'b' } as any; |
| 41 | + normalizeFieldReferenceKeys(field); |
| 42 | + expect(field.reference_to).toBe('a'); |
| 43 | + expect(field.reference).toBe('b'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('is a no-op for non-relational fields, empty targets, and non-objects', () => { |
| 47 | + const plain = { type: 'text' } as any; |
| 48 | + normalizeFieldReferenceKeys(plain); |
| 49 | + expect('reference_to' in plain).toBe(false); |
| 50 | + expect('reference' in plain).toBe(false); |
| 51 | + |
| 52 | + const empty = { type: 'lookup', reference: '' } as any; |
| 53 | + normalizeFieldReferenceKeys(empty); |
| 54 | + expect('reference_to' in empty).toBe(false); |
| 55 | + |
| 56 | + expect(normalizeFieldReferenceKeys(null)).toBeNull(); |
| 57 | + expect(normalizeFieldReferenceKeys('lookup' as any)).toBe('lookup'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('is idempotent', () => { |
| 61 | + const field = { type: 'user', reference: 'sys_user' } as any; |
| 62 | + normalizeFieldReferenceKeys(normalizeFieldReferenceKeys(field)); |
| 63 | + expect(field).toEqual({ type: 'user', reference: 'sys_user', reference_to: 'sys_user' }); |
| 64 | + }); |
| 65 | +}); |
| 66 | + |
| 67 | +describe('normalizeSchemaReferenceKeys', () => { |
| 68 | + it('normalizes every field of a map-shaped schema in place', () => { |
| 69 | + const schema = { |
| 70 | + name: 'showcase_project', |
| 71 | + fields: { |
| 72 | + name: { type: 'text' }, |
| 73 | + account: { type: 'lookup', reference: 'showcase_account' }, |
| 74 | + team_members: { type: 'user', reference: 'sys_user', multiple: true }, |
| 75 | + }, |
| 76 | + } as any; |
| 77 | + const out = normalizeSchemaReferenceKeys(schema); |
| 78 | + expect(out).toBe(schema); // mutates the cached object, not a copy |
| 79 | + expect(schema.fields.account.reference_to).toBe('showcase_account'); |
| 80 | + expect(schema.fields.team_members.reference_to).toBe('sys_user'); |
| 81 | + expect('reference_to' in schema.fields.name).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it('normalizes array-shaped field containers', () => { |
| 85 | + const schema = { |
| 86 | + fields: [ |
| 87 | + { name: 'project', type: 'master_detail', reference: 'showcase_project' }, |
| 88 | + { name: 'title', type: 'text' }, |
| 89 | + ], |
| 90 | + } as any; |
| 91 | + normalizeSchemaReferenceKeys(schema); |
| 92 | + expect(schema.fields[0].reference_to).toBe('showcase_project'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('tolerates schemas without fields and non-object input', () => { |
| 96 | + expect(normalizeSchemaReferenceKeys(null)).toBeNull(); |
| 97 | + expect(normalizeSchemaReferenceKeys({ name: 'x' } as any)).toEqual({ name: 'x' }); |
| 98 | + expect(normalizeSchemaReferenceKeys({ fields: 'nope' } as any)).toEqual({ fields: 'nope' }); |
| 99 | + }); |
| 100 | +}); |
0 commit comments