diff --git a/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.html b/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.html index acde13ebd..dc1e735c9 100644 --- a/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.html +++ b/projects/aca-content/src/lib/components/search/search-input-control/search-input-control.component.html @@ -1,12 +1,11 @@
- @@ -20,7 +22,12 @@
arrow_drop_down -
@@ -36,14 +43,13 @@ (submit)="onSearchSubmit($event)" (searchChange)="onSearchChange($event)" /> -
- - {{ 'SEARCH.INPUT.HINT' | translate }} - - - {{ 'SEARCH.INPUT.REQUIRED' | translate }} - -
+ + {{ 'SEARCH.INPUT.HINT' | translate }} + + + {{ 'SEARCH.INPUT.REQUIRED' | translate }} +
{ + let fixture: ComponentFixture; + let component: SearchInputComponent; + + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [AppTestingModule, ReactiveFormsModule, SearchInputComponent, SearchInputControlComponent], + schemas: [NO_ERRORS_SCHEMA] + }).compileComponents(); + + fixture = TestBed.createComponent(SearchInputComponent); + component = fixture.componentInstance; + + // (component as any).searchInputControl = mockControl; + fixture.detectChanges(); + }); + + describe('Validation Behavior', () => { + function getFirstError(): string { + const error = fixture.debugElement.query(By.directive(MatError)); + return error?.nativeElement.textContent.trim(); + } + + it('should show required error when field is empty and touched', () => { + component.searchInputControl.searchFieldFormControl.setValue(''); + component.searchInputControl.searchFieldFormControl.markAsTouched(); + fixture.detectChanges(); + + const error = getFirstError(); + fixture.detectChanges(); + expect(error).toBe('SEARCH.INPUT.REQUIRED'); + }); + + // it('should not show error when field has value', () => { + // mockControl.searchFieldFormControl.setValue('test'); + // mockControl.searchFieldFormControl.markAsTouched(); + // fixture.detectChanges(); + + // const error = fixture.debugElement.query(By.directive(MatError)); + // expect(error).toBeNull(); + // }); + + // it('should not show error when field is untouched', () => { + // mockControl.searchFieldFormControl.setValue(''); + // mockControl.searchFieldFormControl.markAsUntouched(); + // fixture.detectChanges(); + + // const error = fixture.debugElement.query(By.directive(MatError)); + // expect(error).toBeNull(); + // }); + }); +}); diff --git a/projects/aca-content/src/lib/components/search/search-input/search-input.component.ts b/projects/aca-content/src/lib/components/search/search-input/search-input.component.ts index a1cd802b8..272c569fa 100644 --- a/projects/aca-content/src/lib/components/search/search-input/search-input.component.ts +++ b/projects/aca-content/src/lib/components/search/search-input/search-input.component.ts @@ -192,6 +192,12 @@ export class SearchInputComponent implements OnInit, OnDestroy { searchByOption() { this.syncInputValues(); this.has400LibraryError = false; + + const searchTerm = this.searchedWord?.trim(); + if (!searchTerm) { + return; + } + if (this.isLibrariesChecked()) { this.hasLibrariesConstraint = this.evaluateLibrariesConstraint(); if (this.onLibrariesSearchResults && this.isSameSearchTerm()) {