From 94dae984a7c8bcab0546a6d19d2dff678d6b7ee4 Mon Sep 17 00:00:00 2001 From: Diogo Bastos Date: Mon, 24 Jul 2023 16:42:51 +0100 Subject: [PATCH] [AAE-12065] add tests --- .../dropdown/dropdown-cloud.widget.spec.ts | 29 ++++++++- .../widgets/dropdown/dropdown-cloud.widget.ts | 2 +- .../src/lib/form/mocks/dropdown.mock.ts | 65 +++++++++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) 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 ed96719b4c..5f46e31ee6 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 @@ -39,7 +39,8 @@ import { mockRestDropdownOptions, mockSecondRestDropdownOptions, mockVariablesWithDefaultJson, - mockProcessVariablesWithJson + mockProcessVariablesWithJson, + fakeFormOptionEntries } from '../../../mocks/dropdown.mock'; import { OverlayContainer } from '@angular/cdk/overlay'; import { TaskVariableCloud } from '../../../models/task-variable-cloud.model'; @@ -778,6 +779,32 @@ describe('DropdownCloudWidgetComponent', () => { }); }); + describe('Default option', () => { + it('should not get default option from list of options which do not have any id matching the DEFAULT_OPTION id', () => { + const call = widget.getDefaultOption(fakeFormOptionEntries[0]); + + expect(call).toEqual(undefined); + }); + + it('should get default option from list of options which have any id matching the DEFAULT_OPTION id, while not having an isDefault flag set to true', () => { + const call = widget.getDefaultOption(fakeFormOptionEntries[1]); + + expect(call).toEqual(fakeFormOptionEntries[1][0]); + }); + + it('should get default option from list of options which have any id matching the DEFAULT_OPTION id while also having an isDefault flag set to true', () => { + const call = widget.getDefaultOption(fakeFormOptionEntries[2]); + + expect(call).toEqual(fakeFormOptionEntries[2][0]); + }); + + it('should get default option from list of options which has an isDefault flag set to true, despite the option id', () => { + const call = widget.getDefaultOption(fakeFormOptionEntries[3]); + + expect(call).toEqual(fakeFormOptionEntries[3][0]); + }); + }); + describe('Load selection for linked dropdown (i.e. saved, completed forms)', () => { it('should load the selection of a manual type linked dropdown', () => { 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 23404e5c0c..e72b584b3f 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 @@ -435,6 +435,6 @@ export class DropdownCloudWidgetComponent extends WidgetComponent implements OnI } getDefaultOption(options: FormFieldOption[]): FormFieldOption { - return options.find((option: FormFieldOption) => option.isDefault === undefined ? option.id === DEFAULT_OPTION.id : option.isDefault); + return options.find((option: FormFieldOption) => option.isDefault ?? option.id === DEFAULT_OPTION.id); } } 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 20262f4297..bbf781d621 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 @@ -168,6 +168,71 @@ export const mockCountriesResponse = { ] }; +export const fakeFormOptionEntries = [ + [ + { + id: 'id_1', + name: 'name_1' + }, + { + id: 'id_1', + name: 'name_2' + }, + { + id: 'id_3', + name: 'name_3' + } + ], + [ + { + id: 'empty', + name: 'name_1' + }, + { + id: 'id_1', + name: 'name_2' + }, + { + id: 'id_3', + name: 'name_3' + } + ], + [ + { + id: 'empty', + name: 'name_1', + isDefault: true + }, + { + id: 'id_1', + name: 'name_2', + isDefault: false + }, + { + id: 'id_3', + name: 'name_3', + isDefault: false + } + ], + [ + { + id: 'id_1', + name: 'name_1', + isDefault: true + }, + { + id: 'id_1', + name: 'name_2', + isDefault: false + }, + { + id: 'id_3', + name: 'name_3', + isDefault: false + } + ] +]; + export const mockFormVariableWithJson = [ new TaskVariableCloud({ name: 'json-form-variable', value: mockCountriesResponse, type: 'json', id: 'fake-id-1' }) ];