acupedia-store-ui/tests/unit/components/CartDrawer.test.tsx

143 lines
4.6 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { CartDrawer } from '../../../src/components/cart/CartDrawer';
const sampleItem = {
code: 'POINTS_PACK',
name: 'Points: Complete Pack',
category: 'Bundle',
desc: 'All points',
priceInr: 900,
priceUsd: 15,
priceEur: 15,
};
describe('CartDrawer', () => {
it('renders empty state when cart is empty', () => {
render(
<CartDrawer
cart={[]}
currency="INR"
toggleCartItem={vi.fn()}
couponCode=""
setCouponCode={vi.fn()}
appliedCoupon={null}
applyPromoCode={vi.fn()}
removePromoCode={vi.fn()}
pricingCalc={null}
loadingPricing={false}
loggedInUser={null}
onUpdateBillingPhone={vi.fn()}
verificationResult={null}
checkoutLoading={false}
handleCheckout={vi.fn()}
/>
);
expect(screen.getByText('0 Items')).toBeInTheDocument();
expect(screen.getByText(/Your checkout cart is empty/i)).toBeInTheDocument();
});
it('renders populated cart, discounts, invalid/valid coupons, billing profile, and checkout errors', async () => {
const user = userEvent.setup();
const onUpdateBillingPhone = vi.fn();
const removePromoCode = vi.fn();
const handleCheckout = vi.fn();
const { rerender } = render(
<CartDrawer
cart={[sampleItem]}
currency="INR"
toggleCartItem={vi.fn()}
couponCode="SAVE20"
setCouponCode={vi.fn()}
appliedCoupon="SAVE20"
applyPromoCode={vi.fn()}
removePromoCode={removePromoCode}
pricingCalc={{
currency: 'INR',
base_subtotal: 900,
renewal_discount_amount: 100,
bundle_discount_amount: 50,
coupon_discount_amount: 180,
total_discounts: 330,
convenience_fee_rate: 2,
convenience_fee: 10,
taxable_amount: 570,
gst_amount: 0,
grand_total: 580,
discount_policy: 'Staggered Buy: 10%',
coupon_applied: 'Code: SAVE20 - 20% Off',
coupon_error: null,
}}
loadingPricing={false}
loggedInUser={{
id: 1,
email: 'doc@acupedia.com',
country: 'India',
phone_number: '',
} as any}
onUpdateBillingPhone={onUpdateBillingPhone}
verificationResult={{ status: 'error', message: 'Signature mismatch' }}
checkoutLoading={false}
handleCheckout={handleCheckout}
/>
);
expect(screen.getByText('Complete Pack')).toBeInTheDocument();
expect(screen.getByText('Early Renewal Discount (20%)')).toBeInTheDocument();
expect(screen.getByText('Loyalty Discount: 10%')).toBeInTheDocument();
expect(screen.getByText('Coupon Discount (SAVE20)')).toBeInTheDocument();
expect(screen.getByText('Gateway fee (2%)')).toBeInTheDocument();
expect(screen.getByText('Required for checkout')).toBeInTheDocument();
expect(screen.getByText('Signature mismatch')).toBeInTheDocument();
await user.click(screen.getByRole('button', { name: 'Add Number' }));
expect(onUpdateBillingPhone).toHaveBeenCalledTimes(1);
await user.click(screen.getByRole('button', { name: /Secure Payment with Razorpay/i }));
expect(handleCheckout).toHaveBeenCalledTimes(1);
// Rerender with EUR, invalid coupon, and incomplete phone
rerender(
<CartDrawer
cart={[sampleItem]}
currency="EUR"
toggleCartItem={vi.fn()}
couponCode="BAD"
setCouponCode={vi.fn()}
appliedCoupon="BAD"
applyPromoCode={vi.fn()}
removePromoCode={removePromoCode}
pricingCalc={{
currency: 'EUR',
base_subtotal: 15,
coupon_discount_amount: 0,
total_discounts: 3,
convenience_fee_rate: 0,
convenience_fee: 0,
taxable_amount: 0,
gst_amount: 0,
grand_total: 12,
coupon_applied: 'None',
coupon_error: 'Invalid coupon',
}}
loadingPricing={true}
loggedInUser={{
id: 1,
email: 'doc@acupedia.com',
country: 'Germany',
phone_number: '123',
} as any}
onUpdateBillingPhone={onUpdateBillingPhone}
verificationResult={null}
checkoutLoading={true}
handleCheckout={handleCheckout}
/>
);
expect(screen.getByText(/Invalid coupon/i)).toBeInTheDocument();
expect(screen.getByText(/Incomplete \(123\)/i)).toBeInTheDocument();
expect(screen.getByText('Total Discounts')).toBeInTheDocument();
});
});