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 () {
+78
View File
@@ -0,0 +1,78 @@
import { FormulaField } from '../src/models/FormulaField';
import { expect } from 'chai';
describe('FormulaField', function () {
it('should be defined', function () {
expect(FormulaField).to.not.be.undefined;
});
it('should be a function', function () {
expect(FormulaField).to.be.a('function');
});
it('should have a constructor', function () {
expect(FormulaField).to.have.property('constructor');
});
it('should throw an error if the outputType is not a valid FormulaOutputType', function () {
expect(
() =>
new FormulaField(
1,
1,
'Formula Field',
'Formula',
'Enabled',
false,
false,
'NotAType',
[]
)
).to.throw();
});
it('should create a new FormulaField', function () {
const formulaField = new FormulaField(
1,
1,
'Formula Field',
'Formula',
'Enabled',
false,
false,
'Text',
[]
);
expect(formulaField).to.be.an.instanceOf(FormulaField);
});
it('should have a outputType property', function () {
const formulaField = new FormulaField(
1,
1,
'Formula Field',
'Formula',
'Enabled',
false,
false,
'Text',
[]
);
expect(formulaField).to.have.property('outputType');
});
it('should have a values property', function () {
const formulaField = new FormulaField(
1,
1,
'Formula Field',
'Formula',
'Enabled',
false,
false,
'Text',
[]
);
expect(formulaField).to.have.property('values');
});
});
+98
View File
@@ -0,0 +1,98 @@
import { ListField } from '../src/models/ListField';
import { expect } from 'chai';
describe('ListField', function () {
it('should be defined', function () {
expect(ListField).to.not.be.undefined;
});
it('should be a function', function () {
expect(ListField).to.be.a('function');
});
it('should have a constructor', function () {
expect(ListField).to.have.property('constructor');
});
it('should throw an error if the multiplicity is not valid', function () {
expect(
() =>
new ListField(
1,
1,
'List Field',
'List',
'Enabled',
false,
false,
'Alone',
1,
[]
)
).to.throw();
});
it('should create a new ListField', function () {
const listField = new ListField(
1,
1,
'List Field',
'List',
'Enabled',
false,
false,
'SingleSelect',
1,
[]
);
expect(listField).to.be.an.instanceOf(ListField);
});
it('should have a multiplicity property', function () {
const listField = new ListField(
1,
1,
'List Field',
'List',
'Enabled',
false,
false,
'SingleSelect',
1,
[]
);
expect(listField).to.have.property('multiplicity');
});
it('should have a listId property', function () {
const listField = new ListField(
1,
1,
'List Field',
'List',
'Enabled',
false,
false,
'SingleSelect',
1,
[]
);
expect(listField).to.have.property('listId');
});
it('should have a values property', function () {
const listField = new ListField(
1,
1,
'List Field',
'List',
'Enabled',
false,
false,
'SingleSelect',
1,
[]
);
expect(listField).to.have.property('values');
});
});
+46
View File
@@ -0,0 +1,46 @@
import { ListValue } from '../src/models/ListValue';
import { expect } from 'chai';
describe('ListValue', function () {
it('should be defined', function () {
expect(ListValue).to.not.be.undefined;
});
it('should be a function', function () {
expect(ListValue).to.be.a('function');
});
it('should have a constructor', function () {
expect(ListValue).to.have.property('constructor');
});
it('should create a new ListValue', function () {
const listValue = new ListValue('1', 'List Value', 1, 1, 'red');
expect(listValue).to.be.an.instanceOf(ListValue);
});
it('should have an id property', function () {
const listValue = new ListValue('1', 'List Value', 1, 1, 'red');
expect(listValue).to.have.property('id');
});
it('should have a name property', function () {
const listValue = new ListValue('1', 'List Value', 1, 1, 'red');
expect(listValue).to.have.property('name');
});
it('should have a sortOrder property', function () {
const listValue = new ListValue('1', 'List Value', 1, 1, 'red');
expect(listValue).to.have.property('sortOrder');
});
it('should have a numericValue property', function () {
const listValue = new ListValue('1', 'List Value', 1, 1, 'red');
expect(listValue).to.have.property('numericValue');
});
it('should have a color property', function () {
const listValue = new ListValue('1', 'List Value', 1, 1, 'red');
expect(listValue).to.have.property('color');
});
});
+78
View File
@@ -0,0 +1,78 @@
import { ReferenceField } from '../src/models/ReferenceField';
import { expect } from 'chai';
describe('ReferenceField', function () {
it('should be defined', function () {
expect(ReferenceField).to.not.be.undefined;
});
it('should be a function', function () {
expect(ReferenceField).to.be.a('function');
});
it('should have a constructor', function () {
expect(ReferenceField).to.have.property('constructor');
});
it('should throw an error if the multiplicity is not valid', function () {
expect(
() =>
new ReferenceField(
1,
1,
'Reference Field',
'Reference',
'Enabled',
false,
false,
'Alone',
1
)
).to.throw();
});
it('should create a new ReferenceField', function () {
const referenceField = new ReferenceField(
1,
1,
'Reference Field',
'Reference',
'Enabled',
false,
false,
'SingleSelect',
1
);
expect(referenceField).to.be.an.instanceOf(ReferenceField);
});
it('should have a multiplicity property', function () {
const referenceField = new ReferenceField(
1,
1,
'Reference Field',
'Reference',
'Enabled',
false,
false,
'SingleSelect',
1
);
expect(referenceField).to.have.property('multiplicity');
});
it('should have a referencedAppId property', function () {
const referenceField = new ReferenceField(
1,
1,
'Reference Field',
'Reference',
'Enabled',
false,
false,
'SingleSelect',
1
);
expect(referenceField).to.have.property('referencedAppId');
});
});