feat(search): update search functionality to handle button click and Enter key press
This commit is contained in:
@@ -21,7 +21,7 @@
|
|||||||
<div
|
<div
|
||||||
class="d-flex flex-row align-items-center gap-3 ms-auto flex-nowrap flex-grow-1 justify-content-between justify-content-md-end"
|
class="d-flex flex-row align-items-center gap-3 ms-auto flex-nowrap flex-grow-1 justify-content-between justify-content-md-end"
|
||||||
>
|
>
|
||||||
<form class="input-group store-layout__search" role="search" (ngSubmit)="submitSearch()">
|
<div class="input-group store-layout__search" role="search">
|
||||||
<label class="visually-hidden" for="store-search-input">Buscar productos</label>
|
<label class="visually-hidden" for="store-search-input">Buscar productos</label>
|
||||||
<input
|
<input
|
||||||
id="store-search-input"
|
id="store-search-input"
|
||||||
@@ -30,15 +30,17 @@
|
|||||||
placeholder="Buscar productos"
|
placeholder="Buscar productos"
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
[formControl]="searchControl"
|
[formControl]="searchControl"
|
||||||
|
(keydown.enter)="submitSearch($event)"
|
||||||
/>
|
/>
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="button"
|
||||||
class="btn store-layout__search-button border-start-0 rounded-end px-3"
|
class="btn store-layout__search-button border-start-0 rounded-end px-3"
|
||||||
aria-label="Buscar"
|
aria-label="Buscar"
|
||||||
|
(click)="submitSearch()"
|
||||||
>
|
>
|
||||||
<i class="fa-solid fa-magnifying-glass" aria-hidden="true"></i>
|
<i class="fa-solid fa-magnifying-glass" aria-hidden="true"></i>
|
||||||
</button>
|
</button>
|
||||||
</form>
|
</div>
|
||||||
|
|
||||||
@if (ticketsMenu(); as menu) {
|
@if (ticketsMenu(); as menu) {
|
||||||
<app-button
|
<app-button
|
||||||
|
|||||||
@@ -75,7 +75,9 @@ export class StoreHeaderComponent {
|
|||||||
this.logoutClick.emit();
|
this.logoutClick.emit();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected submitSearch(): void {
|
protected submitSearch(event?: Event): void {
|
||||||
|
event?.preventDefault();
|
||||||
|
|
||||||
const term = this.searchControl.value.trim();
|
const term = this.searchControl.value.trim();
|
||||||
|
|
||||||
if (term.length >= 2) {
|
if (term.length >= 2) {
|
||||||
|
|||||||
@@ -218,17 +218,17 @@ describe('StoreLayoutComponent', () => {
|
|||||||
expect(compiled.querySelector('.fa-linkedin-in')).not.toBeNull();
|
expect(compiled.querySelector('.fa-linkedin-in')).not.toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('navigates to search results when the search form is submitted', () => {
|
it('navigates to search results when the search button is clicked', () => {
|
||||||
const router = TestBed.inject(Router);
|
const router = TestBed.inject(Router);
|
||||||
vi.spyOn(router, 'navigate');
|
vi.spyOn(router, 'navigate');
|
||||||
const fixture = TestBed.createComponent(StoreLayoutComponent);
|
const fixture = TestBed.createComponent(StoreLayoutComponent);
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
const header = fixture.debugElement.query(By.directive(StoreHeaderComponent));
|
const header = fixture.debugElement.query(By.directive(StoreHeaderComponent));
|
||||||
const form = header.query(By.css('form[role="search"]'));
|
const searchButton = header.query(By.css('.store-layout__search-button'));
|
||||||
|
|
||||||
(header.componentInstance as any).searchControl.setValue(' zapatillas ');
|
(header.componentInstance as any).searchControl.setValue(' zapatillas ');
|
||||||
form.triggerEventHandler('ngSubmit');
|
searchButton.triggerEventHandler('click');
|
||||||
fixture.detectChanges();
|
fixture.detectChanges();
|
||||||
|
|
||||||
expect(router.navigate).toHaveBeenCalledWith(['/buscar'], {
|
expect(router.navigate).toHaveBeenCalledWith(['/buscar'], {
|
||||||
@@ -236,6 +236,27 @@ describe('StoreLayoutComponent', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('prevents the native submit and searches when Enter is pressed', () => {
|
||||||
|
const router = TestBed.inject(Router);
|
||||||
|
vi.spyOn(router, 'navigate');
|
||||||
|
const fixture = TestBed.createComponent(StoreLayoutComponent);
|
||||||
|
fixture.detectChanges();
|
||||||
|
|
||||||
|
const header = fixture.debugElement.query(By.directive(StoreHeaderComponent));
|
||||||
|
const input = header.query(By.css('#store-search-input'));
|
||||||
|
const keyboardEvent = {
|
||||||
|
preventDefault: vi.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
(header.componentInstance as any).searchControl.setValue('remera');
|
||||||
|
input.triggerEventHandler('keydown.enter', keyboardEvent);
|
||||||
|
|
||||||
|
expect(keyboardEvent.preventDefault).toHaveBeenCalled();
|
||||||
|
expect(router.navigate).toHaveBeenCalledWith(['/buscar'], {
|
||||||
|
queryParams: { q: 'remera', page: 1 },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('renders the primary tickets action when the tenant has the tickets menu', () => {
|
it('renders the primary tickets action when the tenant has the tickets menu', () => {
|
||||||
const router = TestBed.inject(Router);
|
const router = TestBed.inject(Router);
|
||||||
vi.spyOn(router, 'navigate');
|
vi.spyOn(router, 'navigate');
|
||||||
|
|||||||
Reference in New Issue
Block a user