feat: enhance cart functionality with quantity update and removal features, integrating toast notifications for user feedback

This commit is contained in:
2026-07-02 10:38:26 -03:00
parent f2cc42e0cc
commit c9d8265790
5 changed files with 91 additions and 13 deletions

View File

@@ -7,6 +7,7 @@ import { of } from 'rxjs';
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 { StoreLayoutComponent } from './store-layout.component';
const tenant: Tenant = {
@@ -45,7 +46,19 @@ describe('StoreLayoutComponent', () => {
provide: CartService,
useValue: {
cart: signal(null).asReadonly(),
loadCart: () => of({ id: 1, items: [], subtotal: '0' })
loadCart: () => of({ id: 1, items: [], subtotal: '0' }),
updateItemQuantity: vi.fn().mockReturnValue(of({ data: { id: 1, items: [], subtotal: '0' } })),
removeItem: vi.fn().mockReturnValue(of({ data: { id: 1, items: [], subtotal: '0' } }))
}
},
{
provide: ToastService,
useValue: {
show: vi.fn(),
info: vi.fn(),
danger: vi.fn(),
success: vi.fn(),
dismiss: vi.fn()
}
}
]

View File

@@ -91,8 +91,8 @@ describe('CartService', () => {
const updatedCart = { ...mockCart, subtotal: '30.00' };
updatedCart.items[0].cantidad = 3;
service.updateItemQuantity(10, 3).subscribe((cart) => {
expect(cart).toEqual(updatedCart);
service.updateItemQuantity(10, 3).subscribe((res) => {
expect(res.data).toEqual(updatedCart);
expect(service.cart()).toEqual(updatedCart);
});
@@ -112,8 +112,8 @@ describe('CartService', () => {
subtotal: '0.00'
};
service.removeItem(10).subscribe((cart) => {
expect(cart).toEqual(emptyCart);
service.removeItem(10).subscribe((res) => {
expect(res.data).toEqual(emptyCart);
expect(service.cart()).toEqual(emptyCart);
});

View File

@@ -44,7 +44,7 @@ export class CartService {
);
}
updateItemQuantity(productVariantId: number, cantidad: number): Observable<Cart> {
updateItemQuantity(productVariantId: number, cantidad: number): Observable<ApiResponse<Cart>> {
return this.http
.patch<ApiResponse<Cart>>(
`${this.tenantApiUrl}/cart/items/${productVariantId}`,
@@ -52,20 +52,18 @@ export class CartService {
{ withCredentials: true }
)
.pipe(
map((response) => response.data),
tap((cart) => this.cartState.set(cart))
tap((response) => this.cartState.set(response.data))
);
}
removeItem(productVariantId: number): Observable<Cart> {
removeItem(productVariantId: number): Observable<ApiResponse<Cart>> {
return this.http
.delete<ApiResponse<Cart>>(
`${this.tenantApiUrl}/cart/items/${productVariantId}`,
{ withCredentials: true }
)
.pipe(
map((response) => response.data),
tap((cart) => this.cartState.set(cart))
tap((response) => this.cartState.set(response.data))
);
}
}