import { describe, it, expect, vi, afterEach } from 'vitest' import { render, screen } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { GroupedProductCard } from './GroupedProductCard' import type { GroupedProduct, ProductWithDistance } from '../types/product' const offerBase: ProductWithDistance = { id: 'offer-1', seller_id: 'seller-1', name: 'Dipirona 500mg', description: 'Caixa com 10 comprimidos', batch: 'B123', expires_at: new Date().toISOString(), price_cents: 500, stock: 5, created_at: new Date().toISOString(), updated_at: new Date().toISOString(), distance_km: 5, tenant_city: 'Rio de Janeiro', tenant_state: 'RJ' } const buildGroup = (offers: ProductWithDistance[], minPriceCents: number): GroupedProduct => ({ name: 'Dipirona', description: 'Analgésico', offers, minPriceCents, offerCount: offers.length }) describe('GroupedProductCard', () => { afterEach(() => { vi.useRealTimers() }) it('shows nearest offer and pluralized count', () => { const offers = [ { ...offerBase, id: 'offer-1', distance_km: 8, expires_at: new Date(Date.now() + 40 * 24 * 60 * 60 * 1000).toISOString() }, { ...offerBase, id: 'offer-2', distance_km: 2, expires_at: new Date(Date.now() + 50 * 24 * 60 * 60 * 1000).toISOString() } ] render() expect(screen.getByText('2 ofertas')).toBeInTheDocument() expect(screen.getByText('Mais próximo: 2 km')).toBeInTheDocument() expect(screen.getByText('R$ 5,00')).toBeInTheDocument() }) it('shows expiring soon badge based on soonest expiry and triggers click', async () => { vi.useFakeTimers() vi.setSystemTime(new Date('2024-02-01T00:00:00.000Z')) const offers = [ { ...offerBase, id: 'offer-3', distance_km: 3, expires_at: new Date('2024-02-10T00:00:00.000Z').toISOString() }, { ...offerBase, id: 'offer-4', distance_km: 6, expires_at: new Date('2024-04-10T00:00:00.000Z').toISOString() } ] const onClick = vi.fn() const user = userEvent.setup() render() expect(screen.getByText('Vence em breve')).toBeInTheDocument() await user.click(screen.getByText('Ver ofertas')) expect(onClick).toHaveBeenCalledTimes(1) }) })