fix: implement formula field, list field, and reference field as chilrden of the field class. fix: when converting api response data to appropriate field response make sure json field objects are converted to correct field class based upon type.

This commit is contained in:
StevanFreeborn
2023-02-04 22:00:19 -06:00
parent 48de5ec3b8
commit e6eae35e03
11 changed files with 717 additions and 34 deletions
+201 -1
View File
@@ -8,6 +8,12 @@ import { FieldStatus } from '../src/enums/FieldStatus';
import { FieldType } from '../src/enums/FieldType';
import { GetPagedFieldsResponse } from '../src/models/GetPagedFieldsResponse';
import { CreatedWithIdResponse } from '../src/models/CreatedWithIdResponse';
import { FormulaField } from '../src/models/FormulaField';
import { FormulaOutputType } from '../src/enums/FormulaOutputType';
import { ListValue } from '../src/models/ListValue';
import { ReferenceField } from '../src/models/ReferenceField';
import { Multiplicity } from '../src/enums/Multiplicity';
import { ListField } from '../src/models/ListField';
describe('ApiResponse', function () {
it('should be defined', function () {
@@ -247,7 +253,7 @@ describe('ApiResponse', function () {
expect(ApiResponse.prototype.asFieldType).to.have.lengthOf(0);
});
it('should return an ApiResponse<Field> when data contain a field', function () {
it('should return an ApiResponse<Field> when data contains a field', function () {
const mockResponseData = {
id: 1,
appId: 1,
@@ -274,6 +280,200 @@ describe('ApiResponse', function () {
expect(fieldResponse.data.isUnique).to.equal(false);
}
});
it('should return an ApiResponse<Field> when data contains a list field', function () {
const mockResponseData = {
multiplicity: 'SingleSelect',
listId: 1581,
values: [
{
id: 'e4ee09cf-f93c-40c8-89a4-070514acb25b',
name: 'List Value 1',
sortOrder: 1,
numericValue: 1,
color: '#ffffff',
},
{
id: 'a44fbfb0-6fa9-4d9d-912a-b1807bb1693c',
name: 'List Value 2',
sortOrder: 2,
numericValue: 2,
color: '#ffffff',
},
{
id: '27021e2c-b27a-4fab-84f6-570a5a83a1cd',
name: 'List Value 3',
sortOrder: 3,
numericValue: 3,
color: '#ffffff',
},
],
id: 11929,
appId: 373,
name: 'single-select list field',
type: 'List',
status: 'Enabled',
isRequired: false,
isUnique: false,
};
const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const fieldResponse = apiResponse.asFieldType();
expect(fieldResponse).to.be.instanceOf(ApiResponse<Field>);
expect(fieldResponse.data).to.be.instanceOf(Field);
expect(fieldResponse.data).to.be.instanceOf(ListField);
expect(fieldResponse.data).to.not.be.null;
if (fieldResponse.data != null) {
const listField = fieldResponse.data as ListField;
expect(listField.id).to.equal(11929);
expect(listField.appId).to.equal(373);
expect(listField.name).to.equal('single-select list field');
expect(listField.type).to.equal(FieldType.List);
expect(listField.status).to.equal(FieldStatus.Enabled);
expect(listField.isRequired).to.equal(false);
expect(listField.isUnique).to.equal(false);
expect(listField.multiplicity).to.equal(Multiplicity.SingleSelect);
expect(listField.listId).to.equal(1581);
expect(listField.values).to.be.instanceOf(Array);
expect(listField.values).to.have.lengthOf(3);
listField.values.forEach((value) => {
expect(value).to.be.instanceOf(ListValue);
expect(value).to.have.property('id');
expect(value).to.have.property('name');
expect(value).to.have.property('sortOrder');
expect(value).to.have.property('numericValue');
expect(value).to.have.property('color');
});
}
});
it('should return an ApiResponse<Field> when data contains a reference field', function () {
const mockResponseData = {
multiplicity: 'SingleSelect',
referencedAppId: 2,
id: 11866,
appId: 373,
name: 'Created By',
type: 'Reference',
status: 'Enabled',
isRequired: false,
isUnique: false,
};
const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const fieldResponse = apiResponse.asFieldType();
expect(fieldResponse).to.be.instanceOf(ApiResponse<Field>);
expect(fieldResponse.data).to.be.instanceOf(Field);
expect(fieldResponse.data).to.be.instanceOf(ReferenceField);
expect(fieldResponse.data).to.not.be.null;
if (fieldResponse.data != null) {
const referenceField = fieldResponse.data as ReferenceField;
expect(referenceField.id).to.equal(11866);
expect(referenceField.appId).to.equal(373);
expect(referenceField.name).to.equal('Created By');
expect(referenceField.type).to.equal(FieldType.Reference);
expect(referenceField.status).to.equal(FieldStatus.Enabled);
expect(referenceField.isRequired).to.equal(false);
expect(referenceField.isUnique).to.equal(false);
expect(referenceField.multiplicity).to.equal(Multiplicity.SingleSelect);
expect(referenceField.referencedAppId).to.equal(2);
}
});
it('should return an ApiResponse<Field> when data contains a list formula field', function () {
const mockResponseData = {
outputType: 'ListValue',
values: [
{
id: '2dcc75c2-cd96-4bbc-babb-be78fb465e02',
name: 'List Value 1',
sortOrder: 1,
numericValue: 1,
color: '#d64646',
},
{
id: '1f537f54-bf27-446b-a6a5-76aad5d3b5f6',
name: 'List Value 2',
sortOrder: 2,
numericValue: 1,
color: '#d64646',
},
],
id: 12680,
appId: 373,
name: 'List Formula Field',
type: 'Formula',
status: 'Enabled',
isRequired: false,
isUnique: false,
};
const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const fieldResponse = apiResponse.asFieldType();
expect(fieldResponse).to.be.instanceOf(ApiResponse<Field>);
expect(fieldResponse.data).to.be.instanceOf(Field);
expect(fieldResponse.data).to.be.instanceOf(FormulaField);
expect(fieldResponse.data).to.not.be.null;
if (fieldResponse.data != null) {
const formulaField = fieldResponse.data as FormulaField;
expect(formulaField.id).to.equal(12680);
expect(formulaField.appId).to.equal(373);
expect(formulaField.name).to.equal('List Formula Field');
expect(formulaField.type).to.equal(FieldType.Formula);
expect(formulaField.status).to.equal(FieldStatus.Enabled);
expect(formulaField.isRequired).to.equal(false);
expect(formulaField.isUnique).to.equal(false);
expect(formulaField.outputType).to.equal(FormulaOutputType.ListValue);
expect(formulaField.values).to.be.instanceOf(Array);
expect(formulaField.values).to.have.lengthOf(2);
formulaField.values.forEach((value) => {
expect(value).to.be.instanceOf(ListValue);
expect(value).to.have.property('id');
expect(value).to.have.property('name');
expect(value).to.have.property('sortOrder');
expect(value).to.have.property('numericValue');
expect(value).to.have.property('color');
});
}
});
it('should return an ApiResponse<Field> when data contains a text formula field', function () {
const mockResponseData = {
outputType: 'Text',
values: [],
id: 12060,
appId: 373,
name: 'GetWeekOfYear',
type: 'Formula',
status: 'Enabled',
isRequired: false,
isUnique: false,
};
const apiResponse = new ApiResponse(200, 'OK', mockResponseData);
const fieldResponse = apiResponse.asFieldType();
expect(fieldResponse).to.be.instanceOf(ApiResponse<Field>);
expect(fieldResponse.data).to.be.instanceOf(Field);
expect(fieldResponse.data).to.be.instanceOf(FormulaField);
expect(fieldResponse.data).to.not.be.null;
if (fieldResponse.data != null) {
const formulaField = fieldResponse.data as FormulaField;
expect(formulaField.id).to.equal(12060);
expect(formulaField.appId).to.equal(373);
expect(formulaField.name).to.equal('GetWeekOfYear');
expect(formulaField.type).to.equal(FieldType.Formula);
expect(formulaField.status).to.equal(FieldStatus.Enabled);
expect(formulaField.isRequired).to.equal(false);
expect(formulaField.isUnique).to.equal(false);
expect(formulaField.outputType).to.equal(FormulaOutputType.Text);
expect(formulaField.values).to.be.instanceOf(Array);
expect(formulaField.values).to.have.lengthOf(0);
}
});
});
describe('asFieldCollectionType', function () {