diff --git a/lib/core/form/components/widgets/core/form.model.spec.ts b/lib/core/form/components/widgets/core/form.model.spec.ts index fe2c78d6fc..f03eee4bf9 100644 --- a/lib/core/form/components/widgets/core/form.model.spec.ts +++ b/lib/core/form/components/widgets/core/form.model.spec.ts @@ -24,6 +24,7 @@ import { FormFieldModel } from './form-field.model'; import { FormOutcomeModel } from './form-outcome.model'; import { FormModel } from './form.model'; import { TabModel } from './tab.model'; +import { fakeMetadataForm } from 'process-services-cloud/src/lib/form/mocks/cloud-form.mock'; describe('FormModel', () => { let formService: FormService; @@ -558,4 +559,34 @@ describe('FormModel', () => { expect(missing).toBeUndefined(); }); }); + + describe('add values not present', () => { + let form: FormModel; + + beforeEach(() => { + form = new FormModel(fakeMetadataForm); + form.values['pfx_property_three'] = {}; + form.values['pfx_property_four'] = 'empty'; + form.values['pfx_property_five'] = 'green'; + }); + + it('should not find a process variable', () => { + const values = { + pfx_property_one: 'testValue', + pfx_property_two: true, + pfx_property_three: 'opt_1', + pfx_property_four: 'option_2', + pfx_property_five: 'orange', + pfx_property_none: 'no_form_field' + }; + + const data = form.addValuesNotPresent(values); + + expect(data).toContain({ name: 'pfx_property_one', value: 'testValue' }); + expect(data).toContain({ name: 'pfx_property_two', value: true }); + expect(data).toContain({ name: 'pfx_property_three', value: 'opt_1' }); + expect(data).toContain({ name: 'pfx_property_four', value: 'option_2' }); + expect(data).toContain({ name: 'pfx_property_five', value: 'green' }); + }); + }); }); diff --git a/lib/core/form/components/widgets/core/form.model.ts b/lib/core/form/components/widgets/core/form.model.ts index 5b18cdfb7b..e47934a5e2 100644 --- a/lib/core/form/components/widgets/core/form.model.ts +++ b/lib/core/form/components/widgets/core/form.model.ts @@ -375,4 +375,24 @@ export class FormModel { ); } } + + addValuesNotPresent(valuesToSetIfNotPresent: FormValues): { name: string; value: any }[] { + const keys = Object.keys(valuesToSetIfNotPresent); + keys.forEach(key => { + if (!this.values[key] || this.isEmptyDropdownOption(key)) { + this.values[key] = valuesToSetIfNotPresent[key]; + } + }); + const data = []; + const fields = Object.keys(this.values); + fields.forEach(field => data.push({ name: field, value: this.values[field] })); + return data; + } + + private isEmptyDropdownOption(key: string): boolean { + if (this.getFieldById(key) && (this.getFieldById(key).type === FormFieldTypes.DROPDOWN)) { + return typeof this.values[key] === 'string' ? this.values[key] === 'empty' : Object.keys(this.values[key]).length === 0; + } + return false; + } } diff --git a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts index c71014465d..516fd602bd 100644 --- a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts +++ b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts @@ -57,7 +57,6 @@ describe('FormCloudComponent', () => { let visibilityService: WidgetVisibilityService; let formRenderingService: CloudFormRenderingService; let translateService: TranslateService; - let formService: FormService; @Component({ selector: 'adf-cloud-custom-widget', @@ -120,24 +119,11 @@ describe('FormCloudComponent', () => { const appConfigService = TestBed.inject(AppConfigService); spyOn(appConfigService, 'get').and.returnValue([]); - formService = TestBed.inject(FormService); - fixture = TestBed.createComponent(FormCloudComponent); formComponent = fixture.componentInstance; fixture.detectChanges(); })); - it('should set values when updateFormValuesRequested is updated', async () => { - const fakeForm = new FormModel(JSON.parse(JSON.stringify(fakeMetadataForm))); - formComponent.form = fakeForm; - formComponent.formCloudRepresentationJSON = new FormCloudRepresentation(fakeForm); - - const refreshFormSpy = spyOn(formComponent, 'refreshFormData'); - formService.updateFormValuesRequested.next({ pfx_property_one: 'testValue', pfx_property_two: true }); - expect(refreshFormSpy).toHaveBeenCalled(); - expect(formComponent.data).toContain({ name: 'pfx_property_one', value: 'testValue' }, { name: 'pfx_property_two', value: true }); - }); - it('should register custom [upload] widget', () => { const widget = buildWidget('upload', fixture.componentRef.injector); expect(widget['typeId']).toBe('AttachFileCloudWidgetComponent'); @@ -1107,3 +1093,72 @@ describe('FormCloudWithCustomOutComesComponent', () => { expect(buttonTwoBtn.nativeElement.innerText).toBe('CUSTOM-BUTTON-2'); })); }); + +describe('retrieve metadata on submit', () => { + + setupTestBed({ + imports: [ + NoopAnimationsModule, + TranslateModule.forRoot(), + CoreModule.forRoot(), + FormCloudModule + ], + providers: [ + { + provide: TRANSLATION_PROVIDER, + multi: true, + useValue: { + name: 'app', + source: 'resources' + } + }, + { + provide: VersionCompatibilityService, + useValue: {} + } + ] + }); + + let formComponent: FormCloudComponent; + let fixture: ComponentFixture; + let formService: FormService; + + beforeEach(async(() => { + const appConfigService = TestBed.inject(AppConfigService); + spyOn(appConfigService, 'get').and.returnValue([]); + formService = TestBed.inject(FormService); + + fixture = TestBed.createComponent(FormCloudComponent); + formComponent = fixture.componentInstance; + formComponent.form = formComponent.parseForm(fakeMetadataForm); + fixture.detectChanges(); + })); + + it('should set values when updateFormValuesRequested is updated', async () => { + formComponent.form.values['pfx_property_three'] = {}; + formComponent.form.values['pfx_property_four'] = 'empty'; + formComponent.form.values['pfx_property_five'] = 'green'; + + const addValuesNotPresent = spyOn(formComponent.form, 'addValuesNotPresent').and.callThrough(); + const refreshFormSpy = spyOn(formComponent, 'refreshFormData').and.stub(); + + const values = { + pfx_property_one: 'testValue', + pfx_property_two: true, + pfx_property_three: 'opt_1', + pfx_property_four: 'option_2', + pfx_property_five: 'orange', + pfx_property_none: 'no_form_field' + }; + + formService.updateFormValuesRequested.next(values); + + expect(addValuesNotPresent).toHaveBeenCalledWith(values); + expect(refreshFormSpy).toHaveBeenCalled(); + expect(formComponent.data).toContain({ name: 'pfx_property_one', value: 'testValue' }); + expect(formComponent.data).toContain({ name: 'pfx_property_two', value: true }); + expect(formComponent.data).toContain({ name: 'pfx_property_three', value: 'opt_1' }); + expect(formComponent.data).toContain({ name: 'pfx_property_four', value: 'option_2' }); + expect(formComponent.data).toContain({ name: 'pfx_property_five', value: 'green' }); + }); +}); diff --git a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts index 4fb39ad6c2..4011b11fce 100644 --- a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts +++ b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.ts @@ -116,15 +116,7 @@ export class FormCloudComponent extends FormBaseComponent implements OnChanges, this.formService.updateFormValuesRequested .pipe(takeUntil(this.onDestroy$)) .subscribe((valuesToSetIfNotPresent) => { - const keys = Object.keys(valuesToSetIfNotPresent); - keys.forEach(key => { - if (!this.form.values[key]) { - this.form.values[key] = valuesToSetIfNotPresent[key]; - } - }); - this.data = []; - const fields = Object.keys(this.form.values); - fields.forEach(field => this.data.push({ name: field, value: this.form.values[field] })); + this.data = this.form.addValuesNotPresent(valuesToSetIfNotPresent); this.refreshFormData(); }); } diff --git a/lib/process-services-cloud/src/lib/form/mocks/cloud-form.mock.ts b/lib/process-services-cloud/src/lib/form/mocks/cloud-form.mock.ts index c57fb80eb7..ba69c0bea3 100644 --- a/lib/process-services-cloud/src/lib/form/mocks/cloud-form.mock.ts +++ b/lib/process-services-cloud/src/lib/form/mocks/cloud-form.mock.ts @@ -1045,8 +1045,112 @@ export let fakeMetadataForm = { 'link': false } } + ], + '4': [ + { + 'id': 'pfx_property_three', + 'name': 'pfx_property_three', + 'required': false, + 'readOnly': false, + 'colspan': 1, + 'params': { + 'existingColspan': 1, + 'maxColspan': 2 + }, + 'visibilityCondition': null, + 'type': 'dropdown', + 'optionType': 'manual', + 'options': [ + { + 'id': 'empty', + 'name': 'Choose one...' + }, + { + 'id': 'opt_1', + 'name': 'Option 1' + }, + { + 'id': 'opt_2', + 'name': 'Option 2' + } + ], + 'value': 'empty', + 'restUrl': null, + 'restResponsePath': null, + 'restIdProperty': null, + 'restLabelProperty': null + } + ], + '5': [ + { + 'id': 'pfx_property_four', + 'name': 'pfx_property_four', + 'required': false, + 'readOnly': false, + 'colspan': 1, + 'params': { + 'existingColspan': 1, + 'maxColspan': 2 + }, + 'visibilityCondition': null, + 'type': 'dropdown', + 'optionType': 'manual', + 'options': [ + { + 'id': 'empty', + 'name': 'Choose one...' + }, + { + 'id': 'option_1', + 'name': 'Option: 1' + }, + { + 'id': 'option_2', + 'name': 'Option: 2' + } + ], + 'value': 'empty', + 'restUrl': null, + 'restResponsePath': null, + 'restIdProperty': null, + 'restLabelProperty': null + } + ], + '6': [ + { + 'id': 'pfx_property_five', + 'name': 'pfx_property_five', + 'required': false, + 'readOnly': false, + 'colspan': 1, + 'params': { + 'existingColspan': 1, + 'maxColspan': 2 + }, + 'visibilityCondition': null, + 'type': 'dropdown', + 'optionType': 'manual', + 'options': [ + { + 'id': 'empty', + 'name': 'Choose one...' + }, + { + 'id': 'green', + 'name': 'Colour green' + }, + { + 'id': 'orange', + 'name': 'Colour orange' + } + ], + 'value': 'empty', + 'restUrl': null, + 'restResponsePath': null, + 'restIdProperty': null, + 'restLabelProperty': null + } ] - }, 'numberOfColumns': 2 }