diff --git a/projects/modeling-shared/sdk/src/lib/variables/properties-viewer/value-type-inputs/json-input/json-input.component.spec.ts b/projects/modeling-shared/sdk/src/lib/variables/properties-viewer/value-type-inputs/json-input/json-input.component.spec.ts index dceba1a44..331bfb341 100644 --- a/projects/modeling-shared/sdk/src/lib/variables/properties-viewer/value-type-inputs/json-input/json-input.component.spec.ts +++ b/projects/modeling-shared/sdk/src/lib/variables/properties-viewer/value-type-inputs/json-input/json-input.component.spec.ts @@ -15,31 +15,30 @@ * limitations under the License. */ +import { TranslationMock, TranslationService } from '@alfresco/adf-core'; import { async, ComponentFixture, TestBed } from '@angular/core/testing'; - -import { TranslateModule } from '@ngx-translate/core'; -import { TranslationService, TranslationMock } from '@alfresco/adf-core'; -import { NoopAnimationsModule } from '@angular/platform-browser/animations'; -import { ValueTypeInputComponent } from '../../value-type-input.component'; import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms'; +import { MatAutocompleteModule } from '@angular/material/autocomplete'; +import { MatChipsModule } from '@angular/material/chips'; import { MatDialogModule } from '@angular/material/dialog'; import { MatFormFieldModule } from '@angular/material/form-field'; +import { MatIconModule } from '@angular/material/icon'; import { MatInputModule } from '@angular/material/input'; +import { NoopAnimationsModule } from '@angular/platform-browser/animations'; +import { Store } from '@ngrx/store'; +import { TranslateModule } from '@ngx-translate/core'; +import { CodeEditorService } from '../../../../code-editor/services/code-editor-service.service'; +import { AllowedCharactersDirective } from '../../../../helpers/directives/allowed-characters.directive'; import { JSONSchemaToEntityPropertyService } from '../../../../services/json-schema-to-entity-property.service'; -import { PropertiesViewerStringInputComponent } from '../string-input/string-input.component'; +import { provideModelingJsonSchemaProvider } from '../../../../services/modeling-json-schema-provider.service'; import { ModelingJSONSchemaService } from '../../../../services/modeling-json-schema.service'; -import { CodeEditorService } from '../../../../code-editor/services/code-editor-service.service'; -import { provideInputTypeItemHandler } from '../value-type-inputs'; -import { MatChipsModule } from '@angular/material/chips'; -import { MatIconModule } from '@angular/material/icon'; -import { MatAutocompleteModule } from '@angular/material/autocomplete'; -import { PropertiesViewerArrayInputComponent } from '../array-input/array-input.component'; +import { RegisteredInputsModelingJsonSchemaProvider } from '../../../../services/registered-inputs-modeling-json-schema-provider.service'; +import { ValueTypeInputComponent } from '../../value-type-input.component'; import { VariableValuePipe } from '../../variable-value.pipe'; -import { AllowedCharactersDirective } from '../../../../helpers/directives/allowed-characters.directive'; -import { Store } from '@ngrx/store'; +import { PropertiesViewerArrayInputComponent } from '../array-input/array-input.component'; +import { PropertiesViewerStringInputComponent } from '../string-input/string-input.component'; +import { provideInputTypeItemHandler } from '../value-type-inputs'; import { PropertiesViewerJsonInputComponent } from './json-input.component'; -import { RegisteredInputsModelingJsonSchemaProvider } from '../../../../services/registered-inputs-modeling-json-schema-provider.service'; -import { provideModelingJsonSchemaProvider } from '../../../../services/modeling-json-schema-provider.service'; describe('PropertiesViewerJsonInputComponent', () => { let component: PropertiesViewerJsonInputComponent; @@ -148,6 +147,11 @@ describe('PropertiesViewerJsonInputComponent', () => { component.onChange('[1, 2, 3]'); expect(component.change.emit).toHaveBeenCalledWith([1, 2, 3]); }); + + it('undefined', () => { + component.onChange(undefined); + expect(component.change.emit).toHaveBeenCalledWith(''); + }); }); describe('should emit the modeled object stringified when is valid', () => { @@ -195,4 +199,34 @@ describe('PropertiesViewerJsonInputComponent', () => { component.model = { $ref: '#/$defs/primitive/json' }; expect(component.isPrimitiveJSONInput).toEqual(true); }); + + describe('should transform to string', () => { + it('null', () => { + component.value = null; + component.ngOnChanges(); + + expect(component.stringValue).toBe(''); + }); + + it('number', () => { + component.value = 123; + component.ngOnChanges(); + + expect(component.stringValue).toBe('123'); + }); + + it('string', () => { + component.value = 'a'; + component.ngOnChanges(); + + expect(component.stringValue).toBe('a'); + }); + + it('boolean', () => { + component.value = true; + component.ngOnChanges(); + + expect(component.stringValue).toBe('true'); + }); + }); }); diff --git a/projects/modeling-shared/sdk/src/lib/variables/properties-viewer/value-type-inputs/json-input/json-input.component.ts b/projects/modeling-shared/sdk/src/lib/variables/properties-viewer/value-type-inputs/json-input/json-input.component.ts index 325599d6c..d2c8c9830 100644 --- a/projects/modeling-shared/sdk/src/lib/variables/properties-viewer/value-type-inputs/json-input/json-input.component.ts +++ b/projects/modeling-shared/sdk/src/lib/variables/properties-viewer/value-type-inputs/json-input/json-input.component.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { Component, Output, EventEmitter, Input, OnChanges } from '@angular/core'; +import { Component, EventEmitter, Input, OnChanges, Output } from '@angular/core'; import { EntityProperty, JSONSchemaInfoBasics } from '../../../../api/types'; import { ModelingJSONSchemaService } from '../../../../services/modeling-json-schema.service'; import { ModeledObjectChanges } from '../modeled-object/modeled-object-input.component'; @@ -49,25 +49,37 @@ export class PropertiesViewerJsonInputComponent implements OnChanges { } ngOnChanges(): void { - if (this.value && this.contentChanges()) { - if (typeof this.value !== 'string') { - this.stringValue = this.stringifyValue(); - } else { - this.stringValue = this.value; - } + if (this.contentChanges()) { + this.stringValue = this.getStringValue(this.value); } } + private getStringValue(value: unknown) { + if (!value) { + return ''; + } + + if (typeof value !== 'string') { + return this.stringifyValue(value); + } + + return value; + } + private contentChanges(): boolean { let check = this.stringValue; try { - check = this.stringifyValue(); + check = this.stringifyValue(this.value); } catch (error) { } return this.stringValue !== check; } - private stringifyValue(): string { - return JSON.stringify(this.value, null, 4); + private stringifyValue(value: unknown): string { + try { + return JSON.stringify(value, null, 4); + } catch (e) { } + + return ''; } onChange(value: string) { @@ -84,12 +96,22 @@ export class PropertiesViewerJsonInputComponent implements OnChanges { this.emitValue(); } - private emitValue() { + private getValueForEmit() { + if (!this.value) { + return ''; + } + if (this.valid && this.value) { - this.change.emit(this.value); - } else { - this.change.emit(null); + return this.value; } + + return null; + } + + private emitValue() { + const value = this.getValueForEmit(); + + this.change.emit(value); } get isPrimitiveJSONInput() {