fix default selection required

This commit is contained in:
eromano
2025-05-14 16:42:44 +02:00
parent 7c127888db
commit 81d1572301
3 changed files with 87 additions and 1 deletions

View File

@@ -983,6 +983,20 @@ describe('DropdownCloudWidgetComponent', () => {
expect(widget.field.options.length).toEqual(0); expect(widget.field.options.length).toEqual(0);
}; };
it('should set dropdownControl value without emitting events', () => {
widget.field = {
value: 'testValue',
options: [],
isVisible: true
} as any; // Mock field
spyOn(widget.dropdownControl, 'setValue').and.callThrough();
widget['setFormControlValue']();
expect(widget.dropdownControl.setValue).toHaveBeenCalledWith({ id: 'testValue', name: '' }, { emitEvent: false });
expect(widget.dropdownControl.value).toEqual({ id: 'testValue', name: '' });
});
it('should display options persisted from process variable', async () => { it('should display options persisted from process variable', async () => {
widget.field = getVariableDropdownWidget( widget.field = getVariableDropdownWidget(
'variables.json-variable', 'variables.json-variable',

View File

@@ -195,7 +195,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI
} }
private setFormControlValue(): void { private setFormControlValue(): void {
this.dropdownControl.setValue(this.field?.value, { emitEvent: false }); this.dropdownControl.setValue({ id: this.field?.value, name: '' }, { emitEvent: false });
} }
private updateFormControlState(): void { private updateFormControlState(): void {

View File

@@ -0,0 +1,72 @@
/*!
* @license
* Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { FormFieldModel } from '@alfresco/adf-core';
import { FormControl } from '@angular/forms';
import { defaultValueValidator } from './validators';
import { DEFAULT_OPTION } from './dropdown-cloud.widget';
describe('defaultValueValidator', () => {
let mockField: FormFieldModel;
beforeEach(() => {
mockField = new FormFieldModel(null, {
options: [
{ id: DEFAULT_OPTION.id, name: DEFAULT_OPTION.name },
{ id: 'opt_1', name: 'Option 1' },
{ id: 'opt_2', name: 'Option 2' }
]
});
});
it('should return null when a valid option is selected', () => {
const validator = defaultValueValidator(mockField);
const control = new FormControl('opt_1');
const result = validator(control);
expect(result).toBeNull();
});
it('should return a required error when no valid option is selected', () => {
const validator = defaultValueValidator(mockField);
const control = new FormControl(null);
const result = validator(control);
expect(result).toEqual({ required: true });
});
it('should return a required error when the default option is selected', () => {
const validator = defaultValueValidator(mockField);
const control = new FormControl(DEFAULT_OPTION.id);
const result = validator(control);
expect(result).toEqual({ required: true });
});
it('should return null when the field has no options', () => {
mockField.options = [];
const validator = defaultValueValidator(mockField);
const control = new FormControl('opt_1');
const result = validator(control);
expect(result).toBeNull();
});
});