From bd330b83fbf212c2288291225799a17a5dede705 Mon Sep 17 00:00:00 2001 From: Shivangi917 Date: Mon, 21 Jul 2025 11:00:16 -0400 Subject: [PATCH] feat: WIP - fixing search input errors for draft PR --- .../search-input-control.component.html | 7 +- .../search-input-control.component.spec.ts | 69 ++----------------- .../search-input-control.component.ts | 28 +------- .../search-input/search-input.component.html | 36 ++++++---- .../search-input/search-input.component.scss | 7 +- .../search-input.component.spec.ts | 62 +++++++++++++++++ .../search-input/search-input.component.ts | 6 ++ 7 files changed, 104 insertions(+), 111 deletions(-) create mode 100644 projects/aca-content/src/lib/components/search/search-input/search-input.component.spec.ts 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 204998302..ebec77c0a 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 @@ -203,6 +203,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()) {