From ec82dd066e495dd7157829f387ceb24b733de790 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Mon, 13 Feb 2023 12:36:21 -0600 Subject: [PATCH] fix: allow value property of record value model to be generically typed. feat: added methods to record model to allow adding a record value to the field data property or adding multiple record values to the field data property. wrote tests for these two new methods. --- src/models/Record.ts | 28 +++++++++++++++++--- src/models/RecordValue.ts | 8 +++--- tests/Record.spec.ts | 55 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 7 deletions(-) diff --git a/src/models/Record.ts b/src/models/Record.ts index c831b76..efd7614 100644 --- a/src/models/Record.ts +++ b/src/models/Record.ts @@ -17,18 +17,40 @@ export class Record { /** * @property {RecordValue[]} fieldData - The data for the fields in the record. */ - public fieldData: RecordValue[]; + public fieldData: Array>; /** * @constructor - Creates a new instance of Record. * @param {number} appId - The id of the app that the record belongs to. * @param {number} recordId - The id of the record. - * @param {RecordValue[]} fieldData - The data for the fields in the record. + * @param {RecordValue[]} fieldData - The data for the fields in the record. * @returns {Record} - A new instance of Record. */ - constructor(appId: number, recordId: number, fieldData: RecordValue[] = []) { + constructor( + appId: number, + recordId: number, + fieldData: Array> = [] + ) { this.appId = appId; this.recordId = recordId; this.fieldData = fieldData; } + + /** + * @method addValue - Adds a value to the record. + * @param {RecordValue} fieldData - The value to add to the record. + * @returns {void} + */ + public addValue(fieldData: RecordValue): void { + this.fieldData.push(fieldData); + } + + /** + * @method addValues - Adds values to the record. + * @param {Array>} fieldData - The values to add to the record. + * @returns {void} + */ + public addValues(fieldData: Array>): void { + this.fieldData = this.fieldData.concat(fieldData); + } } diff --git a/src/models/RecordValue.ts b/src/models/RecordValue.ts index 8435cdf..72ec850 100644 --- a/src/models/RecordValue.ts +++ b/src/models/RecordValue.ts @@ -11,7 +11,7 @@ import { TimeSpanData } from './TimeSpanData'; /** * @class RecordValue - A value for a field in a record. */ -export class RecordValue { +export class RecordValue { /** * @property {RecordValueType} type - The type of the record value. */ @@ -23,9 +23,9 @@ export class RecordValue { public fieldId: number; /** - * @property {any} value - The value of the field. + * @property {T | any} value - The value of the field. */ - public value: any; + public value: T | any; /** * @constructor - Creates a new instance of RecordValue. @@ -34,7 +34,7 @@ export class RecordValue { * @param {any} value - The value of the field. * @returns {RecordValue} - A new instance of RecordValue. */ - constructor(type: RecordValueType, fieldId: number, value: any) { + constructor(type: RecordValueType, fieldId: number, value: T | any) { this.type = type; this.fieldId = fieldId; this.value = value; diff --git a/tests/Record.spec.ts b/tests/Record.spec.ts index f95aa7f..5d578ea 100644 --- a/tests/Record.spec.ts +++ b/tests/Record.spec.ts @@ -65,4 +65,59 @@ describe('Record', function () { ]) ).to.have.property('fieldData'); }); + + describe('addValue', function () { + it('should be defined', function () { + expect(Record.prototype.addValue).to.not.be.undefined; + }); + + it('should be a function', function () { + expect(Record.prototype.addValue).to.be.a('function'); + }); + + it('should have a method that has 1 parameter', function () { + expect(Record.prototype.addValue).to.have.lengthOf(1); + }); + + it('should add a value to the fieldData array', function () { + const record = new Record(1, 2, [ + new RecordValue(RecordValueType.String, 1, 'test'), + new RecordValue(RecordValueType.String, 2, 'test'), + ]); + record.addValue(new RecordValue(RecordValueType.String, 3, 'test')); + expect(record) + .to.have.property('fieldData') + .that.is.an('array') + .with.lengthOf(3); + }); + }); + + describe('addValues', function () { + it('should be defined', function () { + expect(Record.prototype.addValues).to.not.be.undefined; + }); + + it('should be a function', function () { + expect(Record.prototype.addValues).to.be.a('function'); + }); + + it('should have a method that has 1 parameter', function () { + expect(Record.prototype.addValues).to.have.lengthOf(1); + }); + + it('should add values to the fieldData array', function () { + const record = new Record(1, 2, [ + new RecordValue(RecordValueType.String, 1, 'test'), + new RecordValue(RecordValueType.String, 2, 'test'), + ]); + record.addValues([ + new RecordValue(RecordValueType.String, 3, 'test'), + new RecordValue(RecordValueType.String, 4, 'test'), + ]); + expect(record) + .to.have.property('fieldData') + .that.is.an('array') + .with.lengthOf(4); + }); + }); });