From 225331b6889b34449fe87f028501f41a5f2ea1c7 Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Sun, 15 Sep 2024 17:13:03 -0700 Subject: [PATCH] Refactor remove element, dropdown coverage --- ...om-fields-query-dropdown.component.spec.ts | 44 ++++++++++++++++++- .../custom-fields-query-dropdown.component.ts | 31 +++++++------ 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.spec.ts b/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.spec.ts index 55f566c89..628a152c5 100644 --- a/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.spec.ts +++ b/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.spec.ts @@ -1,10 +1,14 @@ import { ComponentFixture, TestBed } from '@angular/core/testing' -import { CustomFieldsQueryDropdownComponent } from './custom-fields-query-dropdown.component' +import { + CustomFieldQueriesModel, + CustomFieldsQueryDropdownComponent, +} from './custom-fields-query-dropdown.component' import { CustomFieldsService } from 'src/app/services/rest/custom-fields.service' import { of } from 'rxjs' import { CustomField, CustomFieldDataType } from 'src/app/data/custom-field' import { CUSTOM_FIELD_QUERY_OPERATORS_BY_GROUP, + CustomFieldQueryLogicalOperator, CustomFieldQueryOperatorGroups, } from 'src/app/data/custom-field-query' import { provideHttpClientTesting } from '@angular/common/http/testing' @@ -129,6 +133,16 @@ describe('CustomFieldsQueryDropdownComponent', () => { component.selectionModel.addExpression(expression) component.removeElement(atom) expect(component.selectionModel.isEmpty()).toBeTruthy() + const expression2 = new CustomFieldQueryExpression([ + CustomFieldQueryLogicalOperator.And, + [ + [1, 'icontains', 'test'], + [2, 'icontains', 'test'], + ], + ]) + component.selectionModel.addExpression(expression2) + component.removeElement(expression2) + expect(component.selectionModel.isEmpty()).toBeTruthy() }) it('should emit selectionModelChange when model changes', () => { @@ -138,4 +152,32 @@ describe('CustomFieldsQueryDropdownComponent', () => { atom.changed.next(atom) expect(nextSpy).toHaveBeenCalled() }) + + it('should complete selection model subscription when new selection model is set', () => { + const completeSpy = jest.spyOn(component.selectionModel.changed, 'complete') + const selectionModel = new CustomFieldQueriesModel() + component.selectionModel = selectionModel + expect(completeSpy).toHaveBeenCalled() + }) + + it('should support adding an atom', () => { + const expression = new CustomFieldQueryExpression() + component.addAtom(expression) + expect(expression.value.length).toBe(1) + }) + + it('should support adding an expression', () => { + const expression = new CustomFieldQueryExpression() + component.addExpression(expression) + expect(expression.value.length).toBe(1) + }) + + it('should support getting a custom field by ID', () => { + expect(component.getCustomFieldByID(1)).toEqual(customFields[0]) + }) + + it('should sanitize name from title', () => { + component.title = 'Test Title' + expect(component.name).toBe('test_title') + }) }) diff --git a/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.ts b/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.ts index 20e231818..8884bf74c 100644 --- a/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.ts +++ b/src-ui/src/app/components/common/custom-fields-query-dropdown/custom-fields-query-dropdown.component.ts @@ -96,15 +96,18 @@ export class CustomFieldQueriesModel { }) } - private findElement(queryElement: CustomFieldQueryElement, elements: any[]) { + private findElement( + queryElement: CustomFieldQueryElement, + elements: any[] + ): CustomFieldQueryElement { for (let i = 0; i < elements.length; i++) { if (elements[i] === queryElement) { return elements.splice(i, 1)[0] } else if (elements[i].type === CustomFieldQueryElementType.Expression) { - let found = this.findElement(queryElement, elements[i].value as any[]) - if (found !== undefined) { - return found - } + return this.findElement( + queryElement, + elements[i].value as CustomFieldQueryElement[] + ) } } return undefined @@ -118,20 +121,16 @@ export class CustomFieldQueriesModel { foundComponent = this.queries.splice(i, 1)[0] break } else if (query.type === CustomFieldQueryElementType.Expression) { - let found = this.findElement(queryElement, query.value as any[]) - if (found !== undefined) { - foundComponent = found - } + foundComponent = this.findElement(queryElement, query.value as any[]) } } - if (foundComponent === undefined) { - return + if (foundComponent) { + foundComponent.changed.complete() + if (this.isEmpty()) { + this.clear() + } + this.changed.next(this) } - foundComponent.changed.complete() - if (this.isEmpty()) { - this.clear() - } - this.changed.next(this) } }