feat(store-header, store-layout): add user click event handling and navigation to login
This commit is contained in:
@@ -44,6 +44,7 @@
|
||||
variant="user"
|
||||
ariaLabel="Mi cuenta"
|
||||
title="Mi cuenta"
|
||||
(click)="userClick.emit()"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -12,4 +12,5 @@ export class StoreHeaderComponent {
|
||||
readonly logoUrl = input<string | null>(null);
|
||||
readonly cartQuantity = input<number>(0);
|
||||
readonly cartClick = output<void>();
|
||||
readonly userClick = output<void>();
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
[cartQuantity]="cartQuantity()"
|
||||
[logoUrl]="tenant()?.header_logo ?? null"
|
||||
(cartClick)="isCartOpen.set(!isCartOpen())"
|
||||
(userClick)="onUserClick()"
|
||||
/>
|
||||
|
||||
@if (isCartOpen()) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
||||
import { signal } from '@angular/core';
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { provideRouter } from '@angular/router';
|
||||
import { provideRouter, Router } from '@angular/router';
|
||||
|
||||
import { of } from 'rxjs';
|
||||
|
||||
@@ -8,6 +9,7 @@ import { Tenant } from '../../services/tenant.interface';
|
||||
import { TenantService } from '../../services/tenant.service';
|
||||
import { CartService } from '../../services/cart/cart.service';
|
||||
import { ToastService } from '../../services/toast.service';
|
||||
import { AuthService } from '../../services/auth/auth.service';
|
||||
import { StoreLayoutComponent } from './store-layout.component';
|
||||
|
||||
const tenant: Tenant = {
|
||||
@@ -60,6 +62,12 @@ describe('StoreLayoutComponent', () => {
|
||||
success: vi.fn(),
|
||||
dismiss: vi.fn()
|
||||
}
|
||||
},
|
||||
{
|
||||
provide: AuthService,
|
||||
useValue: {
|
||||
isAuthenticated: signal(false)
|
||||
}
|
||||
}
|
||||
]
|
||||
}).compileComponents();
|
||||
@@ -98,4 +106,44 @@ describe('StoreLayoutComponent', () => {
|
||||
expect(compiled.querySelector('.fa-facebook')).not.toBeNull();
|
||||
expect(compiled.querySelector('.fa-linkedin-in')).not.toBeNull();
|
||||
});
|
||||
|
||||
it('redirects to /login when user icon is clicked and user is not logged in', () => {
|
||||
const authService = TestBed.inject(AuthService);
|
||||
const router = TestBed.inject(Router);
|
||||
vi.spyOn(router, 'navigate');
|
||||
|
||||
// Set isAuthenticated to false
|
||||
(authService.isAuthenticated as any).set(false);
|
||||
|
||||
const fixture = TestBed.createComponent(StoreLayoutComponent);
|
||||
fixture.detectChanges();
|
||||
|
||||
const compiled = fixture.nativeElement as HTMLElement;
|
||||
const userButton = compiled.querySelector('app-icon-button[variant="user"] button') as HTMLButtonElement;
|
||||
expect(userButton).not.toBeNull();
|
||||
userButton.click();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(router.navigate).toHaveBeenCalledWith(['/login']);
|
||||
});
|
||||
|
||||
it('does not redirect to /login when user icon is clicked and user is logged in', () => {
|
||||
const authService = TestBed.inject(AuthService);
|
||||
const router = TestBed.inject(Router);
|
||||
vi.spyOn(router, 'navigate');
|
||||
|
||||
// Set isAuthenticated to true
|
||||
(authService.isAuthenticated as any).set(true);
|
||||
|
||||
const fixture = TestBed.createComponent(StoreLayoutComponent);
|
||||
fixture.detectChanges();
|
||||
|
||||
const compiled = fixture.nativeElement as HTMLElement;
|
||||
const userButton = compiled.querySelector('app-icon-button[variant="user"] button') as HTMLButtonElement;
|
||||
expect(userButton).not.toBeNull();
|
||||
userButton.click();
|
||||
fixture.detectChanges();
|
||||
|
||||
expect(router.navigate).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Component, computed, inject, OnInit, signal } from '@angular/core';
|
||||
import { RouterOutlet } from '@angular/router';
|
||||
import { Router, RouterOutlet } from '@angular/router';
|
||||
import { TenantService } from '../../services/tenant.service';
|
||||
import { CartService } from '../../services/cart/cart.service';
|
||||
import { StoreFooterComponent, StoreFooterSection, StoreSocialLink } from './store-footer/store-footer.component';
|
||||
@@ -7,6 +7,7 @@ import { StoreHeaderComponent } from './store-header/store-header.component';
|
||||
import { CartComponent, CartItemMock } from '../../../shared/components/cart/cart.component';
|
||||
import { ButtonComponent } from '../../../shared/components/button/button.component';
|
||||
import { CartItem } from '../../services/cart/cart.interface';
|
||||
import { AuthService } from '../../services/auth/auth.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-store-layout',
|
||||
@@ -17,6 +18,8 @@ import { CartItem } from '../../services/cart/cart.interface';
|
||||
export class StoreLayoutComponent implements OnInit {
|
||||
private readonly tenantService = inject(TenantService);
|
||||
private readonly cartService = inject(CartService);
|
||||
private readonly authService = inject(AuthService);
|
||||
private readonly router = inject(Router);
|
||||
|
||||
protected readonly isCartOpen = signal(false);
|
||||
|
||||
@@ -80,6 +83,12 @@ export class StoreLayoutComponent implements OnInit {
|
||||
});
|
||||
}
|
||||
|
||||
protected onUserClick(): void {
|
||||
if (!this.authService.isAuthenticated()) {
|
||||
this.router.navigate(['/login']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected readonly footerSections: StoreFooterSection[] = [
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user