[ACS-10514] fix leading and trailing operators (#4944)

* [ACS-10514] add search input operators validator, search input control test coverage, search utils multiple whitespaces handling in split

* [ACS-10514] review fixes

* [ACS-10514] cr fixes
This commit is contained in:
Grzegorz Jaśkowski
2026-01-13 09:43:51 +01:00
committed by GitHub
parent 83752656ac
commit 0f5940a33c
8 changed files with 201 additions and 11 deletions
+2 -1
View File
@@ -595,7 +595,8 @@
"LIBRARIES": "Libraries",
"MIN_LENGTH": "Search input must have at least 2 alphanumeric characters.",
"REQUIRED": "Search input is required.",
"WHITESPACE": "Search input cannot be only whitespace."
"WHITESPACE": "Search input cannot be only whitespace.",
"OPERATORS": "Search input cannot begin with, end with or contain only operators."
},
"SORT": {
"SORTING_OPTION": "Sort by",
@@ -25,13 +25,18 @@
import { SearchInputControlComponent } from './search-input-control.component';
import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { AppTestingModule } from '../../../testing/app-testing.module';
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { NO_ERRORS_SCHEMA, SimpleChange } from '@angular/core';
import { ActivatedRoute, NavigationStart, Router } from '@angular/router';
import { of } from 'rxjs';
import { UnitTestingUtils } from '@alfresco/adf-core';
import { HarnessLoader } from '@angular/cdk/testing';
import { TestbedHarnessEnvironment } from '@angular/cdk/testing/testbed';
describe('SearchInputControlComponent', () => {
let fixture: ComponentFixture<SearchInputControlComponent>;
let component: SearchInputControlComponent;
let unitTestingUtils: UnitTestingUtils;
let loader: HarnessLoader;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [AppTestingModule, SearchInputControlComponent],
@@ -39,9 +44,19 @@ describe('SearchInputControlComponent', () => {
});
fixture = TestBed.createComponent(SearchInputControlComponent);
component = fixture.componentInstance;
loader = TestbedHarnessEnvironment.loader(fixture);
unitTestingUtils = new UnitTestingUtils(fixture.debugElement, loader);
fixture.detectChanges();
});
/**
* Sets the input value of the search input control component.
*/
function setInputValue(value: string) {
component.searchTerm = value;
fixture.detectChanges();
}
it('should emit submit event if form is valid', () => {
component.searchTerm = 'valid';
spyOn(component.submit, 'emit');
@@ -75,21 +90,29 @@ describe('SearchInputControlComponent', () => {
});
it('should clear searchTerm', () => {
component.searchTerm = 'c';
fixture.detectChanges();
setInputValue('c');
component.clear();
expect(component.searchTerm).toBe('');
});
it('should check if searchTerm has a length less than 2', () => {
component.searchTerm = 'd';
fixture.detectChanges();
setInputValue('d');
expect(component.isTermTooShort()).toBe(true);
component.searchTerm = 'dd';
fixture.detectChanges();
setInputValue('dd');
expect(component.isTermTooShort()).toBe(false);
});
it('should mark searchFieldFormControl as untouched on blur', async () => {
spyOn(component.searchFieldFormControl, 'markAsUntouched').and.callThrough();
const input = await unitTestingUtils.getMatInput();
await input.setValue('test');
expect(component.searchFieldFormControl.touched).toBeTrue();
await input.blur();
expect(component.searchFieldFormControl.markAsUntouched).toHaveBeenCalled();
expect(component.searchFieldFormControl.touched).toBeFalse();
});
describe('ngOnInit', () => {
let route: ActivatedRoute;
let router: Router;
@@ -128,4 +151,48 @@ describe('SearchInputControlComponent', () => {
expect(component.searchFieldFormControl.setValue).not.toHaveBeenCalled();
}));
});
describe('validation error messages', () => {
beforeEach(() => {
spyOn(component.validationError, 'emit');
});
it('should emit correct validation error message for whitespace validator', () => {
setInputValue(' ');
expect(component.validationError.emit).toHaveBeenCalledWith('SEARCH.INPUT.WHITESPACE');
});
it('should emit correct validation error message for operators validator', () => {
setInputValue('AND word');
expect(component.validationError.emit).toHaveBeenCalledWith('SEARCH.INPUT.OPERATORS');
});
it('should emit correct validation error message for required validator', () => {
setInputValue('');
expect(component.validationError.emit).toHaveBeenCalledWith('SEARCH.INPUT.REQUIRED');
});
it('should update validation error when hasLibrariesConstraint changes from false to true with short search term', () => {
setInputValue('a');
expect(component.validationError.emit).toHaveBeenCalledWith('');
component.hasLibrariesConstraint = true;
component.ngOnChanges({
hasLibrariesConstraint: new SimpleChange(false, true, false)
});
expect(component.validationError.emit).toHaveBeenCalledWith('SEARCH.INPUT.MIN_LENGTH');
});
it('should clear validation error when hasLibrariesConstraint changes from true to false with short search term', () => {
component.hasLibrariesConstraint = true;
setInputValue('a');
expect(component.validationError.emit).toHaveBeenCalledWith('SEARCH.INPUT.MIN_LENGTH');
component.hasLibrariesConstraint = false;
component.ngOnChanges({
hasLibrariesConstraint: new SimpleChange(true, false, false)
});
expect(component.validationError.emit).toHaveBeenCalledWith('');
});
});
});
@@ -44,7 +44,7 @@ import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { FormControl, FormsModule, ReactiveFormsModule, StatusChangeEvent, TouchedChangeEvent, Validators } from '@angular/forms';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { noWhitespaceValidator } from '@alfresco/aca-shared';
import { noWhitespaceValidator, noLeadingTrailingOperatorsValidator } from '@alfresco/aca-shared';
import { combineLatest } from 'rxjs';
import { filter, startWith } from 'rxjs/operators';
import { ActivatedRoute, NavigationStart, Router } from '@angular/router';
@@ -95,7 +95,7 @@ export class SearchInputControlComponent implements OnInit, OnChanges {
@ViewChild('searchInput', { static: true })
searchInput: ElementRef;
searchFieldFormControl = new FormControl('', [Validators.required, noWhitespaceValidator()]);
searchFieldFormControl = new FormControl('', [Validators.required, noWhitespaceValidator(), noLeadingTrailingOperatorsValidator()]);
get searchTerm(): string {
return this.searchFieldFormControl.value.replace('text:', 'TEXT:');
@@ -167,6 +167,8 @@ export class SearchInputControlComponent implements OnInit, OnChanges {
const errors = this.searchFieldFormControl.errors;
if (errors?.whitespace) {
this.validationError.emit('SEARCH.INPUT.WHITESPACE');
} else if (errors?.operators) {
this.validationError.emit('SEARCH.INPUT.OPERATORS');
} else if (errors?.required) {
this.validationError.emit('SEARCH.INPUT.REQUIRED');
} else if (this.hasLibrariesConstraint && this.isTermTooShort()) {
@@ -118,6 +118,12 @@ describe('SearchUtils', () => {
`(=cm:name:"test1.pdf" OR =cm:title:"test1.pdf") OR (=cm:name:"test2.pdf" OR =cm:title:"test2.pdf")`
);
});
it('should split words correctly when multiple whitespaces are present', () => {
expect(formatSearchTerm(' big yellow ', ['cm:name', 'cm:title'])).toBe(
`(cm:name:"big*" OR cm:title:"big*") AND (cm:name:"yellow*" OR cm:title:"yellow*")`
);
});
});
describe('extractUserQueryFromEncodedQuery', () => {
@@ -78,7 +78,7 @@ export function formatSearchTerm(userInput: string, fields = ['cm:name']): strin
return userInput;
}
const words = userInput.split(' ');
const words = userInput.split(/\s+/);
if (words.length > 1) {
const separator = words.some(isOperator) ? ' ' : ' AND ';