feat(cart): open header cart from query param

This commit is contained in:
2026-08-20 10:56:27 -03:00
parent 56e011bc7a
commit 00a01e1a8e
2 changed files with 38 additions and 4 deletions

View File

@@ -2,9 +2,15 @@ import { describe, it, expect, beforeEach, vi } from 'vitest';
import { signal } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { provideRouter, Router } from '@angular/router';
import {
ActivatedRoute,
convertToParamMap,
ParamMap,
provideRouter,
Router,
} from '@angular/router';
import { of } from 'rxjs';
import { BehaviorSubject, of } from 'rxjs';
import { Tenant } from '../../services/tenant.interface';
import { TenantService } from '../../services/tenant.service';
@@ -140,11 +146,13 @@ describe('StoreLayoutComponent', () => {
let cartState = signal<Cart | null>(null);
let authUserState = signal<AuthUser | null>(null);
let checkoutServiceStub: { startCheckout: ReturnType<typeof vi.fn> };
let queryParamMapState: BehaviorSubject<ParamMap>;
beforeEach(async () => {
tenantState = signal<Tenant | null>(tenant);
cartState = signal<Cart | null>(null);
authUserState = signal<AuthUser | null>(null);
queryParamMapState = new BehaviorSubject(convertToParamMap({}));
const isAuthenticatedState = signal(false);
checkoutServiceStub = {
startCheckout: vi.fn().mockResolvedValue({ id: 55 }),
@@ -154,6 +162,10 @@ describe('StoreLayoutComponent', () => {
imports: [StoreLayoutComponent],
providers: [
provideRouter([]),
{
provide: ActivatedRoute,
useValue: { queryParamMap: queryParamMapState.asObservable() },
},
{
provide: TenantService,
useValue: {
@@ -213,6 +225,19 @@ describe('StoreLayoutComponent', () => {
expect(compiled.querySelector('app-store-footer .store-layout__footer')).not.toBeNull();
});
it('opens the cart when requested through the openCart query parameter', () => {
const fixture = TestBed.createComponent(StoreLayoutComponent);
fixture.detectChanges();
expect((fixture.componentInstance as any).isCartOpen()).toBe(false);
queryParamMapState.next(convertToParamMap({ openCart: 'true' }));
fixture.detectChanges();
expect((fixture.componentInstance as any).isCartOpen()).toBe(true);
expect((fixture.nativeElement as HTMLElement).querySelector('app-cart')).not.toBeNull();
});
it('hides the configured header elements when the tenant disables them', () => {
tenantState.set({
...tenant,

View File

@@ -1,5 +1,6 @@
import { Component, computed, inject, OnInit, signal } from '@angular/core';
import { Router, RouterOutlet } from '@angular/router';
import { Component, computed, DestroyRef, inject, OnInit, signal } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ActivatedRoute, Router, RouterOutlet } from '@angular/router';
import { TenantService } from '../../services/tenant.service';
import { CartService } from '../../services/cart/cart.service';
import { StoreFooterComponent, StoreFooterSection } from './store-footer/store-footer.component';
@@ -32,6 +33,8 @@ export class StoreLayoutComponent implements OnInit {
private readonly checkoutService = inject(CheckoutService);
private readonly toastService = inject(ToastService);
private readonly router = inject(Router);
private readonly route = inject(ActivatedRoute);
private readonly destroyRef = inject(DestroyRef);
protected readonly isCartOpen = signal(false);
protected readonly isCreatingPurchase = signal(false);
@@ -146,6 +149,12 @@ export class StoreLayoutComponent implements OnInit {
});
ngOnInit(): void {
this.route.queryParamMap.pipe(takeUntilDestroyed(this.destroyRef)).subscribe((params) => {
if (params.get('openCart') === 'true') {
this.isCartOpen.set(true);
}
});
this.cartService.loadCart().subscribe({
error: (err) => console.error('Error loading cart', err),
});