91 lines
2.6 KiB
TypeScript
91 lines
2.6 KiB
TypeScript
|
|
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||
|
|
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
|
||
|
|
import ProductDetailPage from '@/app/products/[slug]/page'
|
||
|
|
|
||
|
|
const mockPush = vi.fn()
|
||
|
|
|
||
|
|
vi.mock('next/navigation', () => ({
|
||
|
|
useParams: () => ({ slug: 'test-product' }),
|
||
|
|
useRouter: () => ({ push: mockPush }),
|
||
|
|
}))
|
||
|
|
|
||
|
|
vi.mock('@/components/storefront/Navbar', () => ({
|
||
|
|
Navbar: () => null,
|
||
|
|
}))
|
||
|
|
|
||
|
|
const mockProduct = {
|
||
|
|
product: {
|
||
|
|
id: 'prod-1',
|
||
|
|
title: 'Test Product',
|
||
|
|
slug: 'test-product',
|
||
|
|
description: 'A test product',
|
||
|
|
basePrice: 1000,
|
||
|
|
currency: 'EUR',
|
||
|
|
stock: 10,
|
||
|
|
images: [],
|
||
|
|
variants: [],
|
||
|
|
categories: [],
|
||
|
|
reviews: [],
|
||
|
|
},
|
||
|
|
}
|
||
|
|
|
||
|
|
describe('ProductDetailPage - cart behavior', () => {
|
||
|
|
beforeEach(() => {
|
||
|
|
localStorage.clear()
|
||
|
|
mockPush.mockClear()
|
||
|
|
vi.stubGlobal(
|
||
|
|
'fetch',
|
||
|
|
vi.fn().mockResolvedValue({ json: () => Promise.resolve(mockProduct) })
|
||
|
|
)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('Add to Cart aggiunge 1 articolo al carrello', async () => {
|
||
|
|
render(<ProductDetailPage />)
|
||
|
|
await waitFor(() => screen.getByText('Test Product'))
|
||
|
|
|
||
|
|
fireEvent.click(screen.getByText('Add to Cart'))
|
||
|
|
|
||
|
|
const cart = JSON.parse(localStorage.getItem('cart') || '[]')
|
||
|
|
expect(cart).toHaveLength(1)
|
||
|
|
expect(cart[0].quantity).toBe(1)
|
||
|
|
expect(cart[0].productId).toBe('prod-1')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('Add to Cart cliccato due volte incrementa la quantità a 2', async () => {
|
||
|
|
render(<ProductDetailPage />)
|
||
|
|
await waitFor(() => screen.getByText('Test Product'))
|
||
|
|
|
||
|
|
fireEvent.click(screen.getByText('Add to Cart'))
|
||
|
|
fireEvent.click(screen.getByText('Add to Cart'))
|
||
|
|
|
||
|
|
const cart = JSON.parse(localStorage.getItem('cart') || '[]')
|
||
|
|
expect(cart).toHaveLength(1)
|
||
|
|
expect(cart[0].quantity).toBe(2)
|
||
|
|
})
|
||
|
|
|
||
|
|
it('Buy Now aggiunge 1 articolo e naviga al carrello', async () => {
|
||
|
|
render(<ProductDetailPage />)
|
||
|
|
await waitFor(() => screen.getByText('Test Product'))
|
||
|
|
|
||
|
|
fireEvent.click(screen.getByText('Buy Now'))
|
||
|
|
|
||
|
|
const cart = JSON.parse(localStorage.getItem('cart') || '[]')
|
||
|
|
expect(cart).toHaveLength(1)
|
||
|
|
expect(cart[0].quantity).toBe(1)
|
||
|
|
expect(mockPush).toHaveBeenCalledWith('/cart')
|
||
|
|
})
|
||
|
|
|
||
|
|
it('Add to Cart seguito da Buy Now non duplica: rimane 1 articolo con quantità 1', async () => {
|
||
|
|
render(<ProductDetailPage />)
|
||
|
|
await waitFor(() => screen.getByText('Test Product'))
|
||
|
|
|
||
|
|
fireEvent.click(screen.getByText('Add to Cart'))
|
||
|
|
fireEvent.click(screen.getByText('Buy Now'))
|
||
|
|
|
||
|
|
const cart = JSON.parse(localStorage.getItem('cart') || '[]')
|
||
|
|
expect(cart).toHaveLength(1)
|
||
|
|
expect(cart[0].quantity).toBe(1)
|
||
|
|
expect(mockPush).toHaveBeenCalledWith('/cart')
|
||
|
|
})
|
||
|
|
})
|