[ACS-4103] clear empty parameters for folder rules actions ()

This commit is contained in:
Mykyta Maliarchuk 2024-02-14 16:40:38 +01:00 committed by GitHub
parent ade5c67256
commit 7024bfc926
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 3 deletions
projects/aca-content/folder-rules/src/rule-details/actions

@ -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();

@ -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;
}
}