Skip to content

Commit 6515faa

Browse files
committed
Changelog unit tests
1 parent 411b134 commit 6515faa

File tree

4 files changed

+152
-52
lines changed

4 files changed

+152
-52
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { CustomFieldMetadata } from '../../../reflection/sobject/reflect-custom-field-source';
2+
3+
export default class CustomFieldMetadataBuilder {
4+
name: string = 'MyField';
5+
description: string | null = null;
6+
7+
withName(name: string): CustomFieldMetadataBuilder {
8+
this.name = name;
9+
return this;
10+
}
11+
12+
withDescription(testDescription: string) {
13+
this.description = testDescription;
14+
return this;
15+
}
16+
17+
build(): CustomFieldMetadata {
18+
return {
19+
type: 'Text',
20+
type_name: 'customfield',
21+
label: 'MyField',
22+
name: this.name,
23+
description: this.description,
24+
parentName: 'MyObject',
25+
required: false,
26+
};
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { CustomMetadataMetadata } from '../../../reflection/sobject/reflect-custom-metadata-source';
2+
3+
export default class CustomMetadataMetadataBuilder {
4+
parentName: string = 'MyObject';
5+
name: string = 'FieldName__c';
6+
7+
withName(name: string): CustomMetadataMetadataBuilder {
8+
this.name = name;
9+
return this;
10+
}
11+
12+
build(): CustomMetadataMetadata {
13+
return {
14+
type_name: 'custommetadata',
15+
apiName: `${this.parentName}.${this.name}`,
16+
protected: false,
17+
label: 'MyMetadata',
18+
name: this.name,
19+
parentName: this.parentName,
20+
};
21+
}
22+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { CustomFieldMetadata } from '../../../reflection/sobject/reflect-custom-field-source';
2+
import { CustomObjectMetadata } from '../../../reflection/sobject/reflect-custom-object-sources';
3+
import { CustomMetadataMetadata } from '../../../reflection/sobject/reflect-custom-metadata-source';
4+
5+
export default class CustomObjectMetadataBuilder {
6+
label: string = 'MyObject';
7+
fields: CustomFieldMetadata[] = [];
8+
metadataRecords: CustomMetadataMetadata[] = [];
9+
10+
withField(field: CustomFieldMetadata): CustomObjectMetadataBuilder {
11+
this.fields.push(field);
12+
return this;
13+
}
14+
15+
withMetadataRecord(metadataRecord: CustomMetadataMetadata): CustomObjectMetadataBuilder {
16+
this.metadataRecords.push(metadataRecord);
17+
return this;
18+
}
19+
20+
build(): CustomObjectMetadata {
21+
return {
22+
type_name: 'customobject',
23+
deploymentStatus: 'Deployed',
24+
visibility: 'Public',
25+
label: this.label,
26+
name: 'MyObject',
27+
description: null,
28+
fields: this.fields,
29+
metadataRecords: this.metadataRecords,
30+
};
31+
}
32+
}

src/core/changelog/__test__/processing-changelog.spec.ts

Lines changed: 70 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { processChangelog } from '../process-changelog';
22
import { reflect, Type } from '@cparra/apex-reflection';
3-
import { CustomObjectMetadata } from '../../reflection/sobject/reflect-custom-object-sources';
4-
import { CustomFieldMetadata } from '../../reflection/sobject/reflect-custom-field-source';
3+
import CustomFieldMetadataBuilder from './helpers/custom-field-metadata-builder';
4+
import CustomObjectMetadataBuilder from './helpers/custom-object-metadata-builder';
5+
import CustomMetadataMetadataBuilder from './helpers/custom-metadata-metadata-builder';
56

67
function apexTypeFromRawString(raw: string): Type {
78
const result = reflect(raw);
@@ -12,56 +13,6 @@ function apexTypeFromRawString(raw: string): Type {
1213
return result.typeMirror!;
1314
}
1415

15-
class CustomFieldMetadataBuilder {
16-
name: string = 'MyField';
17-
description: string | null = null;
18-
19-
withName(name: string): CustomFieldMetadataBuilder {
20-
this.name = name;
21-
return this;
22-
}
23-
24-
withDescription(testDescription: string) {
25-
this.description = testDescription;
26-
return this;
27-
}
28-
29-
build(): CustomFieldMetadata {
30-
return {
31-
type: 'Text',
32-
type_name: 'customfield',
33-
label: 'MyField',
34-
name: this.name,
35-
description: this.description,
36-
parentName: 'MyObject',
37-
required: false,
38-
};
39-
}
40-
}
41-
42-
class CustomObjectMetadataBuilder {
43-
label: string = 'MyObject';
44-
fields: CustomFieldMetadata[] = [];
45-
46-
withField(field: CustomFieldMetadata): CustomObjectMetadataBuilder {
47-
this.fields.push(field);
48-
return this;
49-
}
50-
51-
build(): CustomObjectMetadata {
52-
return {
53-
type_name: 'customobject',
54-
deploymentStatus: 'Deployed',
55-
visibility: 'Public',
56-
label: this.label,
57-
name: 'MyObject',
58-
description: null,
59-
fields: this.fields,
60-
metadataRecords: [],
61-
};
62-
}
63-
}
64-
6516
describe('when generating a changelog', () => {
6617
it('has no new types when both the old and new versions are empty', () => {
6718
const oldVersion = { types: [] };
@@ -669,4 +620,71 @@ describe('when generating a changelog', () => {
669620
]);
670621
});
671622
});
623+
624+
describe('with custom metadata records', () => {
625+
it('does not list custom metadata records that are the same in both versions', () => {
626+
// The record uniqueness is determined by its api name.
627+
628+
const oldCustomMetadata = new CustomMetadataMetadataBuilder().build();
629+
const newCustomMetadata = new CustomMetadataMetadataBuilder().build();
630+
631+
const oldManifest = { types: [oldCustomMetadata] };
632+
const newManifest = { types: [newCustomMetadata] };
633+
634+
const changeLog = processChangelog(oldManifest, newManifest);
635+
636+
expect(changeLog.customObjectModifications).toEqual([]);
637+
});
638+
639+
it('lists new records of a custom object', () => {
640+
const oldObject = new CustomObjectMetadataBuilder()
641+
.withMetadataRecord(new CustomMetadataMetadataBuilder().build())
642+
.build();
643+
const newObject = new CustomObjectMetadataBuilder()
644+
.withMetadataRecord(new CustomMetadataMetadataBuilder().build())
645+
.withMetadataRecord(new CustomMetadataMetadataBuilder().withName('NewField__c').build())
646+
.build();
647+
648+
const oldManifest = { types: [oldObject] };
649+
const newManifest = { types: [newObject] };
650+
651+
const changeLog = processChangelog(oldManifest, newManifest);
652+
653+
expect(changeLog.customObjectModifications).toEqual([
654+
{
655+
typeName: newObject.name,
656+
modifications: [
657+
{
658+
__typename: 'NewCustomMetadataRecord',
659+
name: 'NewField__c',
660+
},
661+
],
662+
},
663+
]);
664+
});
665+
666+
it('lists removed records of a custom object', () => {
667+
const oldObject = new CustomObjectMetadataBuilder()
668+
.withMetadataRecord(new CustomMetadataMetadataBuilder().withName('OldField__c').build())
669+
.build();
670+
const newObject = new CustomObjectMetadataBuilder().build();
671+
672+
const oldManifest = { types: [oldObject] };
673+
const newManifest = { types: [newObject] };
674+
675+
const changeLog = processChangelog(oldManifest, newManifest);
676+
677+
expect(changeLog.customObjectModifications).toEqual([
678+
{
679+
typeName: oldObject.name,
680+
modifications: [
681+
{
682+
__typename: 'RemovedCustomMetadataRecord',
683+
name: 'OldField__c',
684+
},
685+
],
686+
},
687+
]);
688+
});
689+
});
672690
});

0 commit comments

Comments
 (0)