From abd9d879aa6433ea166afae09db441155fd85495 Mon Sep 17 00:00:00 2001 From: "Kanduru, Ajay (NIH/NLM/LHC) [C]" Date: Wed, 31 Aug 2022 12:29:59 -0400 Subject: [PATCH] Add unit tests for changes and address review comments --- .../schema-form/src/lib/form.component.ts | 1 - .../src/lib/model/arrayproperty.ts | 4 +- .../src/lib/model/objectproperty.spec.ts | 52 ++++++++++++++++++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/projects/schema-form/src/lib/form.component.ts b/projects/schema-form/src/lib/form.component.ts index 55cba301..9e4368d8 100644 --- a/projects/schema-form/src/lib/form.component.ts +++ b/projects/schema-form/src/lib/form.component.ts @@ -82,7 +82,6 @@ export class FormComponent implements OnChanges, ControlValueAccessor { // tslint:disable-next-line:no-output-on-prefix @Output() onErrorsChange = new EventEmitter<{value: any}>(); - // tslint:disable-next-line:no-output-on-prefix @Output() modelReset = new EventEmitter<{value: any}>(); rootProperty: FormProperty = null; diff --git a/projects/schema-form/src/lib/model/arrayproperty.ts b/projects/schema-form/src/lib/model/arrayproperty.ts index 8d7cf38d..c25d5188 100644 --- a/projects/schema-form/src/lib/model/arrayproperty.ts +++ b/projects/schema-form/src/lib/model/arrayproperty.ts @@ -26,8 +26,8 @@ export class ArrayProperty extends PropertyGroup { private addProperty(value) { let itemSchema = this.schema.items; - if (Array.isArray(this.schema.items)) { - const itemSchemas = this.schema.items as object[]; + if (Array.isArray(itemSchema)) { + const itemSchemas = itemSchema as object[]; if (itemSchemas.length > (this.properties).length) { itemSchema = itemSchema[(this.properties).length]; } else if (this.schema.additionalItems) { diff --git a/projects/schema-form/src/lib/model/objectproperty.spec.ts b/projects/schema-form/src/lib/model/objectproperty.spec.ts index 9513fed2..a26d8d7a 100644 --- a/projects/schema-form/src/lib/model/objectproperty.spec.ts +++ b/projects/schema-form/src/lib/model/objectproperty.spec.ts @@ -32,6 +32,7 @@ describe('ObjectProperty', () => { }; let objProperty: ObjectProperty; + let value: any = null; beforeEach(() => { @@ -43,7 +44,8 @@ describe('ObjectProperty', () => { THE_OBJECT_SCHEMA, null, '', - A_LOGGER + A_LOGGER, + value ); }); @@ -57,4 +59,52 @@ describe('ObjectProperty', () => { } }); + it('should create same properties as in schema with arbitrary parent path', () => { + value = {FOO: 1}; + objProperty = new ObjectProperty( + A_FORM_PROPERTY_FACTORY, + A_SCHEMA_VALIDATOR_FACTORY, + A_VALIDATOR_REGISTRY, + A_EXPRESSION_COMPILER_FACTORY, + THE_OBJECT_SCHEMA, + null, + '/abc/def/*', + A_LOGGER, + value + ); + for (const propertyId in THE_OBJECT_SCHEMA.properties) { + if (THE_OBJECT_SCHEMA.properties.hasOwnProperty(propertyId)) { + const property = objProperty.getProperty(propertyId); + expect(property).toBeDefined(); + } + } + }); + + ['extension', 'modifierExtension'].forEach((field) => { + it('Special handling for ' + field + ': should create same properties as in value', () => { + value = {FOO: 1}; + objProperty = new ObjectProperty( + A_FORM_PROPERTY_FACTORY, + A_SCHEMA_VALIDATOR_FACTORY, + A_VALIDATOR_REGISTRY, + A_EXPRESSION_COMPILER_FACTORY, + THE_OBJECT_SCHEMA, + null, + '/abc/' + field + '/*', + A_LOGGER, + value + ); + for (const propertyId in THE_OBJECT_SCHEMA.properties) { + if (THE_OBJECT_SCHEMA.properties.hasOwnProperty(propertyId)) { + const property = objProperty.getProperty(propertyId); + if (value.hasOwnProperty(propertyId)) { + expect(property).toBeDefined(); + } else { + expect(property).toBeUndefined(); + } + } + } + }); + }); + });