Refactor remove element, dropdown coverage

This commit is contained in:
shamoon 2024-09-15 17:13:03 -07:00
parent 2a8e07dd97
commit 225331b688
2 changed files with 58 additions and 17 deletions

View File

@ -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')
})
})

View File

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