From 7024bfc9267c47ec68f024e56db9355cacb5ad47 Mon Sep 17 00:00:00 2001 From: Mykyta Maliarchuk <84377976+nikita-web-ua@users.noreply.github.com> Date: Wed, 14 Feb 2024 16:40:38 +0100 Subject: [PATCH] [ACS-4103] clear empty parameters for folder rules actions (#3641) --- .../actions/rule-action.ui-component.spec.ts | 25 +++++++++++++++++++ .../actions/rule-action.ui-component.ts | 13 +++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/projects/aca-content/folder-rules/src/rule-details/actions/rule-action.ui-component.spec.ts b/projects/aca-content/folder-rules/src/rule-details/actions/rule-action.ui-component.spec.ts index deedd1050..623237606 100644 --- a/projects/aca-content/folder-rules/src/rule-details/actions/rule-action.ui-component.spec.ts +++ b/projects/aca-content/folder-rules/src/rule-details/actions/rule-action.ui-component.spec.ts @@ -47,6 +47,13 @@ describe('RuleActionUiComponent', () => { const getPropertiesCardView = (): CardViewComponent => fixture.debugElement.query(By.directive(CardViewComponent)).componentInstance; + function setInputValue(value: string) { + const input = fixture.debugElement.query(By.css('input')).nativeElement; + input.value = value; + input.dispatchEvent(new Event('input')); + fixture.detectChanges(); + } + beforeEach(() => { TestBed.configureTestingModule({ imports: [CoreTestingModule, RuleActionUiComponent] @@ -56,6 +63,24 @@ describe('RuleActionUiComponent', () => { component = fixture.componentInstance; }); + it('should clear empty parameters', async () => { + component.actionDefinitions = actionsTransformedListMock; + component.parameterConstraints = dummyConstraints; + fixture.detectChanges(); + + changeMatSelectValue('mock-action-1-definition'); + + setInputValue('test'); + await fixture.whenStable(); + + expect(component.parameters).toEqual({ 'mock-action-parameter-boolean': false, 'mock-action-parameter-text': 'test' }); + + setInputValue(''); + await fixture.whenStable(); + + expect(component.parameters).toEqual({ 'mock-action-parameter-boolean': false }); + }); + it('should populate the dropdown selector with the action definitions', () => { component.actionDefinitions = actionsTransformedListMock; fixture.detectChanges(); diff --git a/projects/aca-content/folder-rules/src/rule-details/actions/rule-action.ui-component.ts b/projects/aca-content/folder-rules/src/rule-details/actions/rule-action.ui-component.ts index 92ed59689..d490c6e6a 100644 --- a/projects/aca-content/folder-rules/src/rule-details/actions/rule-action.ui-component.ts +++ b/projects/aca-content/folder-rules/src/rule-details/actions/rule-action.ui-component.ts @@ -159,10 +159,10 @@ export class RuleActionUiComponent implements ControlValueAccessor, OnInit, OnCh }); this.cardViewUpdateService.itemUpdated$.pipe(takeUntil(this.onDestroy$)).subscribe((updateNotification: UpdateNotification) => { - this.parameters = { + this.parameters = this.clearEmptyParameters({ ...this.parameters, ...updateNotification.changed - }; + }); this.onChange({ actionDefinitionId: this.selectedActionDefinitionId, params: this.parameters @@ -291,7 +291,9 @@ export class RuleActionUiComponent implements ControlValueAccessor, OnInit, OnCh setDefaultParameters() { this.parameters = {}; (this.selectedActionDefinition?.parameterDefinitions ?? []).forEach((paramDef: ActionParameterDefinition) => { - this.parameters[paramDef.name] = paramDef.type === 'd:boolean' ? false : ''; + if (paramDef.type === 'd:boolean') { + this.parameters[paramDef.name] = false; + } }); } @@ -321,4 +323,9 @@ export class RuleActionUiComponent implements ControlValueAccessor, OnInit, OnCh label: constraint.label ? `${constraint.label} [${constraint.value}]` : constraint.value })); } + + private clearEmptyParameters(params: { [key: string]: unknown }): { [key: string]: unknown } { + Object.keys(params).forEach((key) => (params[key] === null || params[key] === undefined || params[key] === '') && delete params[key]); + return params; + } }