File size: 2,500 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {
  computeMorphRelationGqlFieldJoinColumnName,
  computeRelationGqlFieldJoinColumnName,
} from '@/utils/fieldMetadata/compute-relation-gql-field-join-column-name';

describe('computeRelationGqlFieldJoinColumnName', () => {
  it('should append `Id` to a simple field name', () => {
    expect(computeRelationGqlFieldJoinColumnName({ name: 'company' })).toBe(
      'companyId',
    );
  });

  it('should preserve camelCase field names', () => {
    expect(
      computeRelationGqlFieldJoinColumnName({ name: 'pointOfContact' }),
    ).toBe('pointOfContactId');
  });

  it('should append `Id` to a morph-aware field name', () => {
    expect(
      computeRelationGqlFieldJoinColumnName({ name: 'targetOpportunity' }),
    ).toBe('targetOpportunityId');
  });

  it('should not strip an existing trailing `Id` (it just appends)', () => {
    expect(computeRelationGqlFieldJoinColumnName({ name: 'companyId' })).toBe(
      'companyIdId',
    );
  });
});

describe('computeMorphRelationGqlFieldJoinColumnName', () => {
  it('should combine field name and capitalized singular target for MANY_TO_ONE', () => {
    expect(
      computeMorphRelationGqlFieldJoinColumnName({
        fieldName: 'target',
        relationType: 'MANY_TO_ONE' as any,
        targetObjectMetadataNameSingular: 'opportunity',
        targetObjectMetadataNamePlural: 'opportunities',
      }),
    ).toBe('targetOpportunityId');
  });

  it('should combine field name and capitalized plural target for ONE_TO_MANY', () => {
    expect(
      computeMorphRelationGqlFieldJoinColumnName({
        fieldName: 'caretaker',
        relationType: 'ONE_TO_MANY' as any,
        targetObjectMetadataNameSingular: 'person',
        targetObjectMetadataNamePlural: 'people',
      }),
    ).toBe('caretakerPeopleId');
  });

  it('should handle simple plurals (e.g. `companies`)', () => {
    expect(
      computeMorphRelationGqlFieldJoinColumnName({
        fieldName: 'parent',
        relationType: 'ONE_TO_MANY' as any,
        targetObjectMetadataNameSingular: 'company',
        targetObjectMetadataNamePlural: 'companies',
      }),
    ).toBe('parentCompaniesId');
  });

  it('should throw on an invalid relation type', () => {
    expect(() =>
      computeMorphRelationGqlFieldJoinColumnName({
        fieldName: 'target',
        relationType: 'INVALID' as any,
        targetObjectMetadataNameSingular: 'opportunity',
        targetObjectMetadataNamePlural: 'opportunities',
      }),
    ).toThrow();
  });
});