diff --git a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.spec.ts b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.spec.ts index 24af7916d..930164dce 100644 --- a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.spec.ts +++ b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.spec.ts @@ -2,7 +2,12 @@ import { CdkDragDrop } from '@angular/cdk/drag-drop' import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http' import { provideHttpClientTesting } from '@angular/common/http/testing' import { ComponentFixture, TestBed } from '@angular/core/testing' -import { FormsModule, ReactiveFormsModule } from '@angular/forms' +import { + FormControl, + FormGroup, + FormsModule, + ReactiveFormsModule, +} from '@angular/forms' import { NgbActiveModal, NgbModule } from '@ng-bootstrap/ng-bootstrap' import { NgSelectModule } from '@ng-select/ng-select' import { of } from 'rxjs' @@ -369,4 +374,19 @@ describe('WorkflowEditDialogComponent', () => { expect(component.objectForm.get('actions').value[0].email).toBeNull() expect(component.objectForm.get('actions').value[0].webhook).toBeNull() }) + + it('should remove selected custom field from the form group', () => { + const formGroup = new FormGroup({ + assign_custom_fields: new FormControl([1, 2, 3]), + }) + + component.removeSelectedCustomField(2, formGroup) + expect(formGroup.get('assign_custom_fields').value).toEqual([1, 3]) + + component.removeSelectedCustomField(1, formGroup) + expect(formGroup.get('assign_custom_fields').value).toEqual([3]) + + component.removeSelectedCustomField(3, formGroup) + expect(formGroup.get('assign_custom_fields').value).toEqual([]) + }) }) diff --git a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts index 6b5920bb4..66caf0148 100644 --- a/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts +++ b/src-ui/src/app/components/common/edit-dialog/workflow-edit-dialog/workflow-edit-dialog.component.ts @@ -651,10 +651,6 @@ export class WorkflowEditDialogComponent super.save() } - public getCustomField(id: number): CustomField { - return this.customFields.find((field) => field.id === id) - } - public removeSelectedCustomField(fieldId: number, group: FormGroup) { group .get('assign_custom_fields') diff --git a/src-ui/src/app/components/common/input/custom-fields-values/custom-fields-values.component.spec.ts b/src-ui/src/app/components/common/input/custom-fields-values/custom-fields-values.component.spec.ts new file mode 100644 index 000000000..82a065452 --- /dev/null +++ b/src-ui/src/app/components/common/input/custom-fields-values/custom-fields-values.component.spec.ts @@ -0,0 +1,69 @@ +import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http' +import { provideHttpClientTesting } from '@angular/common/http/testing' +import { ComponentFixture, TestBed } from '@angular/core/testing' +import { + FormsModule, + NG_VALUE_ACCESSOR, + ReactiveFormsModule, +} from '@angular/forms' +import { of } from 'rxjs' +import { CustomField, CustomFieldDataType } from 'src/app/data/custom-field' +import { CustomFieldsService } from 'src/app/services/rest/custom-fields.service' +import { CustomFieldsValuesComponent } from './custom-fields-values.component' + +describe('CustomFieldsValuesComponent', () => { + let component: CustomFieldsValuesComponent + let fixture: ComponentFixture + let customFieldsService: CustomFieldsService + + beforeEach(async () => { + TestBed.configureTestingModule({ + imports: [FormsModule, ReactiveFormsModule, CustomFieldsValuesComponent], + providers: [ + provideHttpClient(withInterceptorsFromDi()), + provideHttpClientTesting(), + ], + }).compileComponents() + + fixture = TestBed.createComponent(CustomFieldsValuesComponent) + fixture.debugElement.injector.get(NG_VALUE_ACCESSOR) + component = fixture.componentInstance + customFieldsService = TestBed.inject(CustomFieldsService) + jest.spyOn(customFieldsService, 'listAll').mockReturnValue( + of({ + all: [1], + count: 1, + results: [ + { + id: 1, + name: 'Field 1', + data_type: CustomFieldDataType.String, + } as CustomField, + ], + }) + ) + fixture.detectChanges() + }) + + beforeEach(() => { + fixture = TestBed.createComponent(CustomFieldsValuesComponent) + component = fixture.componentInstance + fixture.detectChanges() + }) + + it('should set selectedFields and map values correctly', () => { + component.value = { 1: 'value1' } + component.selectedFields = [1, 2] + expect(component.selectedFields).toEqual([1, 2]) + expect(component.value).toEqual({ 1: 'value1', 2: null }) + }) + + it('should return the correct custom field by id', () => { + const field = component.getCustomField(1) + expect(field).toEqual({ + id: 1, + name: 'Field 1', + data_type: CustomFieldDataType.String, + } as CustomField) + }) +})