mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-05-26 17:24:56 +00:00
[AAE-3273] Manage empty option in retrieve content metadata dropdowns (#6024)
* AAE-3273 Manage empty option in retrieve content metadata dropdowns * AAE-3273 Add unit tests * AAE-3273 Refactor add not present fields * AAE-3273 Add unit test in core
This commit is contained in:
parent
a7af27cfad
commit
c1435c53e0
@ -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' });
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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<any>(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<FormCloudComponent>;
|
||||
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<any>(formComponent.form, 'addValuesNotPresent').and.callThrough();
|
||||
const refreshFormSpy = spyOn<any>(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' });
|
||||
});
|
||||
});
|
||||
|
@ -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();
|
||||
});
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user