Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle undefined in cmdt record creation #662

Merged
merged 3 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions src/shared/helpers/createUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,18 @@ export const getFieldNames = (fileData: CustomField[], nameField: string): strin
* @param fileData Array of objects that contain field data
* @return {string} String representation of XML
*/
const buildCustomFieldXml = (
export const buildCustomFieldXml = (
fileData: CustomField[] = [],
cliParams: Record<string, string> = {},
ignoreFields = false
): string => {
let ret = '';
for (const fieldName of Object.keys(cliParams)) {
const type = getFieldPrimitiveType(fileData, fieldName);
const dataType = getFieldDataType(fileData, fieldName);
// Added functionality to handle the ignore fields scenario.
if (canConvert(dataType) || !ignoreFields) {
ret += getFieldTemplate(fieldName, cliParams[fieldName], type);
}
}

return ret;
};
): string =>
Object.entries(cliParams)
.filter(
([fieldName, value]) =>
value !== undefined && (canConvert(getFieldDataType(fileData, fieldName)) || !ignoreFields)
)
.map(([fieldName, value]) => getFieldTemplate(fieldName, value, getFieldPrimitiveType(fileData, fieldName)))
.join('');

/**
* Get the number type based on the scale.
Expand Down
2 changes: 1 addition & 1 deletion src/shared/templates/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export const createFieldXML = (data: CustomField, defaultToString: boolean): str
returnValue += getLengthTag(data);
returnValue += getVisibleLines(data);

// preventing standard objects that have fields that are being convered from passing in data
// preventing standard objects that have fields that are being converted from passing in data
// that is no longer relevant
// e.g. multiselectpicklist are being converted to long text area and long text area's do not support valuesets
if (canConvert(data.type)) {
Expand Down
73 changes: 72 additions & 1 deletion test/unit/createUtil.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
/* eslint-disable camelcase */

import { expect } from 'chai';
import { appendDirectorySuffix } from '../../src/shared/helpers/createUtil.js';
import { CustomField } from 'jsforce/lib/api/metadata.js';
import { appendDirectorySuffix, buildCustomFieldXml } from '../../src/shared/helpers/createUtil.js';

describe('CreateUtil', () => {
describe('appendDirectorySuffix', () => {
Expand All @@ -18,4 +20,73 @@ describe('CreateUtil', () => {
expect(output2 === 'foobar__mdt').to.be.true;
});
});

describe('buildCustomFieldXml', () => {
const fields = [
{
fullName: 'Test__c',
label: 'Test',
type: 'Text',
},
{
fullName: 'Test2__c',
label: 'Test2',
type: 'Checkbox',
},
] as CustomField[];

it('2 fields that are convertible become records', () => {
const xml = buildCustomFieldXml(fields, { Test__c: 'foo', Test2__c: 'true' });
expect(xml).to.equal(`
<values>
<field>Test__c</field>
<value xsi:type="xsd:string">foo</value>
</values>
<values>
<field>Test2__c</field>
<value xsi:type="xsd:boolean">true</value>
</values>`);
});
it('undefined values do not become records', () => {
const xml = buildCustomFieldXml(fields, { Test__c: 'foo' });

expect(xml).to.equal(`
<values>
<field>Test__c</field>
<value xsi:type="xsd:string">foo</value>
</values>`);
});
describe('non-convertible fields', () => {
const fieldsWithLookup = [
...fields,
{
fullName: 'Test3__c',
label: 'Test3',
type: 'Lookup',
} as CustomField,
];

it('2 fields that are convertible with a non-convertible one when ignoreFields=true', () => {
const xml = buildCustomFieldXml(fieldsWithLookup, { Test__c: 'foo', Test3__c: 'badValue' }, true);
// that 2nd one is omitted because it has no value
expect(xml).to.equal(`
<values>
<field>Test__c</field>
<value xsi:type="xsd:string">foo</value>
</values>`);
});
it('2 fields that are convertible with a non-convertible one and ignoreFields=false', () => {
const xml = buildCustomFieldXml(fieldsWithLookup, { Test__c: 'foo', Test3__c: 'badValue' }, false);
expect(xml).to.equal(`
<values>
<field>Test__c</field>
<value xsi:type="xsd:string">foo</value>
</values>
<values>
<field>Test3__c</field>
<value xsi:type="xsd:string">badValue</value>
</values>`);
});
});
});
});