From 6eacb4032075c80b2c7a9c3dd02869d07d8c0457 Mon Sep 17 00:00:00 2001 From: Diogo Bastos Date: Mon, 24 Jul 2023 16:16:11 +0100 Subject: [PATCH] [AAE-12065] add optional isDefault flag to formOptions interface --- .../widgets/core/form-field-option.ts | 1 + .../dropdown/dropdown-cloud.widget.spec.ts | 67 ++++++-------- .../widgets/dropdown/dropdown-cloud.widget.ts | 18 ++-- .../src/lib/form/mocks/dropdown.mock.ts | 87 ++++++++----------- 4 files changed, 70 insertions(+), 103 deletions(-) diff --git a/lib/core/src/lib/form/components/widgets/core/form-field-option.ts b/lib/core/src/lib/form/components/widgets/core/form-field-option.ts index 6a9483877b..cf77c15fa6 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field-option.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field-option.ts @@ -20,4 +20,5 @@ export interface FormFieldOption { id: string; name: string; + isDefault?: boolean; } diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.spec.ts b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.spec.ts index a4205e69d1..ed96719b4c 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.spec.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.spec.ts @@ -18,6 +18,7 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { of, throwError } from 'rxjs'; +import { DropdownCloudWidgetComponent } from './dropdown-cloud.widget'; import { FormFieldModel, FormModel, @@ -26,7 +27,6 @@ import { FormFieldTypes, LogService } from '@alfresco/adf-core'; -import { DropdownCloudWidgetComponent, DropdownFormFieldOption } from './dropdown-cloud.widget'; import { FormCloudService } from '../../../services/form-cloud.service'; import { ProcessServiceCloudTestingModule } from '../../../../testing/process-service-cloud.testing.module'; import { TranslateModule } from '@ngx-translate/core'; @@ -91,7 +91,7 @@ describe('DropdownCloudWidgetComponent', () => { readOnly: false, restUrl: 'https://fake-rest-url' }); - widget.field.emptyOption = { id: 'empty', name: 'Choose one...', isDefault: true } as DropdownFormFieldOption; + widget.field.emptyOption = { id: 'empty', name: 'Choose one...' }; widget.field.isVisible = true; fixture.detectChanges(); }); @@ -184,20 +184,17 @@ describe('DropdownCloudWidgetComponent', () => { widget.field.optionType = 'rest'; widget.field.value = { id: 'opt1', - name: 'default1_value', - isDefault: false + name: 'default1_value' }; spyOn(formCloudService, 'getRestWidgetData').and.returnValue(of([ { id: 'opt1', - name: 'default1_value', - isDefault: false + name: 'default1_value' }, { id: 2, - name: 'default2_value', - isDefault: false + name: 'default2_value' } ] as any)); @@ -218,13 +215,11 @@ describe('DropdownCloudWidgetComponent', () => { spyOn(formCloudService, 'getRestWidgetData').and.returnValue(of([ { id: 'opt1', - name: 'default1_value', - isDefault: false + name: 'default1_value' }, { id: 2, - name: 'default2_value', - isDefault: false + name: 'default2_value' } ] as any)); @@ -234,12 +229,12 @@ describe('DropdownCloudWidgetComponent', () => { await openSelect(); const options = fixture.debugElement.queryAll(By.css('.mat-option-text')); expect(options[0].nativeElement.innerText).toBe('default1_value'); - expect(widget.field.form.values['dropdown-id']).toEqual({ id: 'opt1', name: 'default1_value', isDefault: false }); + expect(widget.field.form.values['dropdown-id']).toEqual({ id: 'opt1', name: 'default1_value' }); }); it('should not display required error for a non required dropdown when selecting the none option', async () => { widget.field.options = [ - { id: 'empty', name: 'Choose empty', isDefault: true }, + { id: 'empty', name: 'Choose empty' }, ...fakeOptionList ]; @@ -259,7 +254,7 @@ describe('DropdownCloudWidgetComponent', () => { it('should not display required error when selecting a valid option for a required dropdown', async () => { widget.field.required = true; widget.field.options = [ - { id: 'empty', name: 'Choose empty', isDefault: true }, + { id: 'empty', name: 'Choose empty' }, ...fakeOptionList ]; @@ -278,7 +273,7 @@ describe('DropdownCloudWidgetComponent', () => { it('should not have a value when switching from an available option to the None option', async () => { widget.field.options = [ - { id: 'empty', name: 'This is a mock none option', isDefault: true }, + { id: 'empty', name: 'This is a mock none option' }, ...fakeOptionList ]; @@ -444,8 +439,8 @@ describe('DropdownCloudWidgetComponent', () => { options: fakeOptionList, selectionType: 'multiple', value: [ - { id: 'opt_1', name: 'option_1', isDefault: false }, - { id: 'opt_2', name: 'option_2', isDefault: false } + { id: 'opt_1', name: 'option_1' }, + { id: 'opt_2', name: 'option_2' } ] }); fixture.detectChanges(); @@ -479,8 +474,8 @@ describe('DropdownCloudWidgetComponent', () => { optionOne.triggerEventHandler('click', null); optionTwo.triggerEventHandler('click', null); expect(widget.field.value).toEqual([ - { id: 'opt_1', name: 'option_1', isDefault: false }, - { id: 'opt_2', name: 'option_2', isDefault: false } + { id: 'opt_1', name: 'option_1' }, + { id: 'opt_2', name: 'option_2' } ]); }); @@ -494,30 +489,26 @@ describe('DropdownCloudWidgetComponent', () => { optionType : 'rest', selectionType: 'multiple', value: [ - { id: 'opt_3', name: 'option_3', isDefault: false }, - { id: 'opt_4', name: 'option_4', isDefault: false } + { id: 'opt_3', name: 'option_3' }, + { id: 'opt_4', name: 'option_4' } ] }); spyOn(formCloudService, 'getRestWidgetData').and.returnValue(of([ { id: 'opt_1', - name: 'option_1', - isDefault: false + name: 'option_1' }, { id: 'opt_2', - name: 'option_2', - isDefault: false + name: 'option_2' }, { id: 'opt_3', - name: 'option_3', - isDefault: false + name: 'option_3' }, { id: 'opt_4', - name: 'option_4', - isDefault: false + name: 'option_4' } ] as any)); @@ -549,23 +540,19 @@ describe('DropdownCloudWidgetComponent', () => { spyOn(formCloudService, 'getRestWidgetData').and.returnValue(of([ { id: 'opt_1', - name: 'option_1', - isDefault: false + name: 'option_1' }, { id: 'opt_2', - name: 'option_2', - isDefault: false + name: 'option_2' }, { id: 'opt_3', - name: 'option_3', - isDefault: false + name: 'option_3' }, { id: 'opt_4', - name: 'option_4', - isDefault: false + name: 'option_4' } ] as any)); @@ -577,8 +564,8 @@ describe('DropdownCloudWidgetComponent', () => { optionOne.triggerEventHandler('click', null); optionTwo.triggerEventHandler('click', null); expect(widget.field.value).toEqual([ - { id: 'opt_2', name: 'option_2', isDefault: false }, - { id: 'opt_4', name: 'option_4', isDefault: false } + { id: 'opt_2', name: 'option_2' }, + { id: 'opt_4', name: 'option_4' } ]); }); }); diff --git a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts index 966d01b681..23404e5c0c 100644 --- a/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts +++ b/lib/process-services-cloud/src/lib/form/components/widgets/dropdown/dropdown-cloud.widget.ts @@ -38,10 +38,6 @@ export const DEFAULT_OPTION = { }; export const HIDE_FILTER_LIMIT = 5; -export interface DropdownFormFieldOption extends FormFieldOption { - isDefault: boolean; -} - /* eslint-disable @angular-eslint/component-selector */ @Component({ @@ -68,7 +64,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI variableOptionsFailed = false; previewState = false; restApiHostName: string; - list$: Observable; + list$: Observable; filter$ = new BehaviorSubject(''); private readonly defaultVariableOptionId = 'id'; @@ -199,7 +195,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI const bodyParam = this.buildBodyParam(); this.formCloudService.getRestWidgetData(this.field.form.id, this.field.id, bodyParam) .pipe(takeUntil(this.onDestroy$)) - .subscribe((result: DropdownFormFieldOption[]) => { + .subscribe((result: FormFieldOption[]) => { this.resetRestApiErrorMessage(); this.field.options = result; this.updateOptions(); @@ -338,7 +334,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI return this.field?.rule?.ruleOn; } - compareDropdownValues(opt1: DropdownFormFieldOption | string, opt2: DropdownFormFieldOption | string): boolean { + compareDropdownValues(opt1: FormFieldOption | string, opt2: FormFieldOption | string): boolean { if (!opt1 || !opt2) { return false; } @@ -358,7 +354,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI return opt1 === opt2; } - getOptionValue(option: DropdownFormFieldOption, fieldValue: string): string | DropdownFormFieldOption { + getOptionValue(option: FormFieldOption, fieldValue: string): string | FormFieldOption { if (this.field.hasMultipleValues) { return option; } @@ -399,7 +395,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI updateOptions(): void { this.showInputFilter = this.field.options.length > this.appConfig.get('form.dropDownFilterLimit', HIDE_FILTER_LIMIT); - this.list$ = combineLatest([of(this.field.options as DropdownFormFieldOption[]), this.filter$]) + this.list$ = combineLatest([of(this.field.options), this.filter$]) .pipe( map(([items, search]) => { if (!search) { @@ -438,7 +434,7 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI !this.variableOptionsFailed; } - getDefaultOption(options: DropdownFormFieldOption[]): DropdownFormFieldOption { - return options.find((option: DropdownFormFieldOption) => option.isDefault); + getDefaultOption(options: FormFieldOption[]): FormFieldOption { + return options.find((option: FormFieldOption) => option.isDefault === undefined ? option.id === DEFAULT_OPTION.id : option.isDefault); } } diff --git a/lib/process-services-cloud/src/lib/form/mocks/dropdown.mock.ts b/lib/process-services-cloud/src/lib/form/mocks/dropdown.mock.ts index 500c7395f3..20262f4297 100644 --- a/lib/process-services-cloud/src/lib/form/mocks/dropdown.mock.ts +++ b/lib/process-services-cloud/src/lib/form/mocks/dropdown.mock.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { DropdownFormFieldOption } from '../components/widgets/dropdown/dropdown-cloud.widget'; +import { FormFieldOption } from '@alfresco/adf-core'; import { TaskVariableCloud } from '../models/task-variable-cloud.model'; export const mockConditionalEntries = [ @@ -24,18 +24,15 @@ export const mockConditionalEntries = [ options: [ { id: 'empty', - name: 'Choose one...', - isDefault: true + name: 'Choose one...' }, { id: 'ATH', - name: 'Athens', - isDefault: false + name: 'Athens' }, { id: 'SKG', - name: 'Thessaloniki', - isDefault: false + name: 'Thessaloniki' } ] }, @@ -44,18 +41,15 @@ export const mockConditionalEntries = [ options: [ { id: 'empty', - name: 'Choose one...', - isDefault: true + name: 'Choose one...' }, { id: 'MI', - name: 'MILAN', - isDefault: false + name: 'MILAN' }, { id: 'RM', - name: 'ROME', - isDefault: false + name: 'ROME' } ] }, @@ -64,56 +58,51 @@ export const mockConditionalEntries = [ options: [ { id: 'empty', - name: 'Choose one...', - isDefault: true + name: 'Choose one...' }, { id: 'LDN', - name: 'London', - isDefault: false + name: 'London' }, { id: 'MAN', - name: 'Manchester', - isDefault: false + name: 'Manchester' }, { id: 'SHE', - name: 'Sheffield', - isDefault: false + name: 'Sheffield' }, { id: 'LEE', - name: 'Leeds', - isDefault: false + name: 'Leeds' } ] } ]; -export const mockRestDropdownOptions: DropdownFormFieldOption[] = [ - { id: 'LO', name: 'LONDON', isDefault: false }, - { id: 'MA', name: 'MANCHESTER', isDefault: false } +export const mockRestDropdownOptions: FormFieldOption[] = [ + { id: 'LO', name: 'LONDON' }, + { id: 'MA', name: 'MANCHESTER' } ]; -export const mockSecondRestDropdownOptions: DropdownFormFieldOption[] = [ - { id: 'MI', name: 'MILAN', isDefault: false }, - { id: 'RM', name: 'ROME', isDefault: false } +export const mockSecondRestDropdownOptions: FormFieldOption[] = [ + { id: 'MI', name: 'MILAN' }, + { id: 'RM', name: 'ROME' } ]; -export const fakeOptionList: DropdownFormFieldOption[] = [ - { id: 'opt_1', name: 'option_1', isDefault: false }, - { id: 'opt_2', name: 'option_2', isDefault: false }, - { id: 'opt_3', name: 'option_3', isDefault: false } +export const fakeOptionList: FormFieldOption[] = [ + { id: 'opt_1', name: 'option_1' }, + { id: 'opt_2', name: 'option_2' }, + { id: 'opt_3', name: 'option_3' } ]; -export const filterOptionList: DropdownFormFieldOption[] = [ - { id: 'opt_1', name: 'option_1', isDefault: false }, - { id: 'opt_2', name: 'option_2', isDefault: false }, - { id: 'opt_3', name: 'option_3', isDefault: false }, - { id: 'opt_4', name: 'option_4', isDefault: false }, - { id: 'opt_5', name: 'option_5', isDefault: false }, - { id: 'opt_6', name: 'option_6', isDefault: false } +export const filterOptionList = [ + { id: 'opt_1', name: 'option_1' }, + { id: 'opt_2', name: 'option_2' }, + { id: 'opt_3', name: 'option_3' }, + { id: 'opt_4', name: 'option_4' }, + { id: 'opt_5', name: 'option_5' }, + { id: 'opt_6', name: 'option_6' } ]; export const mockPlayersResponse = { @@ -149,18 +138,15 @@ export const mockDefaultResponse = { [ { id: 'default-pet-1', - name: 'Dog', - isDefault: false + name: 'Dog' }, { id: 'default-pet-2', - name: 'Cat', - isDefault: false + name: 'Cat' }, { id: 'default-pet-3', - name: 'Parrot', - isDefault: false + name: 'Parrot' } ] }; @@ -169,18 +155,15 @@ export const mockCountriesResponse = { countries: [ { id: 'PL', - name: 'Poland', - isDefault: false + name: 'Poland' }, { id: 'UK', - name: 'United Kingdom', - isDefault: false + name: 'United Kingdom' }, { id: 'GR', - name: 'Greece', - isDefault: false + name: 'Greece' } ] };