Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Commit

Permalink
[AAE-7317] fix copying value from previous json type input to next em…
Browse files Browse the repository at this point in the history
…… (#3475)

* [AAE-7317] fix copying value from previous json type input to next empty one

* CR fixes

* CR fix

* trigger travis
  • Loading branch information
ddohcim authored and Travis CI User committed Feb 23, 2022
1 parent e0f3b07 commit 20cf9ed
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down

0 comments on commit 20cf9ed

Please sign in to comment.