[D4CRIS-1080] Fix issue where a replace patch operation was dispatched instead of an add one when field's previous value is empty

This commit is contained in:
Giuseppe Digilio
2021-05-18 11:52:03 +02:00
parent d6dbbd1f1f
commit 98dde58f9d
2 changed files with 30 additions and 3 deletions

View File

@@ -574,7 +574,7 @@ describe('SectionFormOperationsService test suite', () => {
});
it('should dispatch a json-path remove operation when has a stored value', () => {
const previousValue = new FormFieldPreviousValueObject(['path', 'test'], 'value');
let previousValue = new FormFieldPreviousValueObject(['path', 'test'], 'value');
const event = Object.assign({}, dynamicFormControlChangeEvent, {
model: {
parent: mockRowGroupModel
@@ -597,6 +597,7 @@ describe('SectionFormOperationsService test suite', () => {
spyIndex.and.returnValue(1);
spyPath.and.returnValue('path/1');
previousValue = new FormFieldPreviousValueObject(['path', 'test'], 'value');
serviceAsAny.dispatchOperationsFromChangeEvent(pathCombiner, event, previousValue, true);
expect(jsonPatchOpBuilder.remove).toHaveBeenCalledWith(pathCombiner.getPath('path/1'));
@@ -627,6 +628,32 @@ describe('SectionFormOperationsService test suite', () => {
new FormFieldMetadataValueObject('test'));
});
it('should dispatch a json-path add operation when has a stored value but previous value is empty', () => {
const previousValue = new FormFieldPreviousValueObject(['path', 'test'], null);
const event = Object.assign({}, dynamicFormControlChangeEvent, {
model: {
parent: mockRowGroupModel
}
});
spyOn(service, 'getFieldPathFromEvent').and.returnValue('path/0');
spyOn(service, 'getFieldPathSegmentedFromChangeEvent').and.returnValue('path');
spyOn(service, 'getFieldValueFromChangeEvent').and.returnValue(new FormFieldMetadataValueObject('test'));
spyOn(service, 'getArrayIndexFromEvent').and.returnValue(0);
spyOn(serviceAsAny, 'getValueMap');
spyOn(serviceAsAny, 'dispatchOperationsFromMap');
formBuilderService.isQualdropGroup.and.returnValue(false);
formBuilderService.isRelationGroup.and.returnValue(false);
formBuilderService.hasArrayGroupValue.and.returnValue(false);
spyOn(previousValue, 'isPathEqual').and.returnValue(false);
serviceAsAny.dispatchOperationsFromChangeEvent(pathCombiner, event, previousValue, true);
expect(jsonPatchOpBuilder.add).toHaveBeenCalledWith(
pathCombiner.getPath('path'),
new FormFieldMetadataValueObject('test'),
true);
});
it('should dispatch a json-path add operation when has a value and field index is zero or undefined', () => {
const previousValue = new FormFieldPreviousValueObject(['path', 'test'], 'value');
const event = Object.assign({}, dynamicFormControlChangeEvent, {

View File

@@ -389,7 +389,7 @@ export class SectionFormOperationsService {
this.operationsBuilder.add(
pathCombiner.getPath(segmentedPath),
value, true);
} else if (previousValue.isPathEqual(this.formBuilder.getPath(event.model)) || hasStoredValue) {
} else if (previousValue.isPathEqual(this.formBuilder.getPath(event.model)) || (hasStoredValue && isNotEmpty(previousValue.value)) ) {
// Here model has a previous value changed or stored in the server
if (hasValue(event.$event) && hasValue(event.$event.previousIndex)) {
if (event.$event.previousIndex < 0) {
@@ -422,7 +422,7 @@ export class SectionFormOperationsService {
previousValue.delete();
} else if (value.hasValue()) {
// Here model has no previous value but a new one
if (isUndefined(this.getArrayIndexFromEvent(event)) || this.getArrayIndexFromEvent(event) === 0) {
if (isUndefined(this.getArrayIndexFromEvent(event)) || this.getArrayIndexFromEvent(event) === 0) {
// Model is single field or is part of an array model but is the first item,
// so dispatch an add operation that initialize the values of a specific metadata
this.operationsBuilder.add(