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

127 lines
4.3 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, screen, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { PaymentSuccessModal } from '../../../src/components/checkout/PaymentSuccessModal';
describe('PaymentSuccessModal', () => {
const baseResult = {
status: 'success' as const,
paymentId: 'pay_123',
orderId: 'order_123',
invoiceNumber: 'INV-001',
invoiceUrl: '/api/checkout/payment/pay_123/invoice/view',
amount: 900,
currency: 'INR' as const,
items: ['Points Pack'],
message: 'Unlocked!',
};
beforeEach(() => {
vi.useFakeTimers({ shouldAdvanceTime: true });
});
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
});
it('renders details, pauses auto-redirect, and triggers invoice download & close', async () => {
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
const onClose = vi.fn();
const onDownloadInvoice = vi.fn();
render(
<PaymentSuccessModal
verificationResult={baseResult}
onClose={onClose}
loggedInUser={{ email: 'doc@acupedia.com' } as any}
onDownloadInvoice={onDownloadInvoice}
downloadingInvoice={false}
/>
);
expect(screen.getByText('Thank You for Your Purchase!')).toBeInTheDocument();
expect(screen.getByText('INV-001')).toBeInTheDocument();
expect(screen.getByText('₹900')).toBeInTheDocument();
await user.click(screen.getByRole('button', { name: /Stay on Web/i }));
expect(screen.getByText(/Auto-redirect paused/i)).toBeInTheDocument();
await user.click(screen.getByRole('button', { name: /Open App Now/i }));
expect(screen.getByText(/Attempted to launch Acupedia App/i)).toBeInTheDocument();
await user.click(screen.getByText('Download Invoice PDF'));
expect(onDownloadInvoice).toHaveBeenCalledWith('pay_123', '/api/checkout/payment/pay_123/invoice/view');
await user.click(screen.getByRole('button', { name: 'Return to Storefront' }));
expect(onClose).toHaveBeenCalledTimes(1);
});
it('auto-redirects when countdown hits 0 and formats EUR/USD currencies', () => {
const { rerender } = render(
<PaymentSuccessModal
verificationResult={{ ...baseResult, currency: 'EUR', amount: 15 }}
onClose={vi.fn()}
loggedInUser={null}
onDownloadInvoice={vi.fn()}
downloadingInvoice={true}
/>
);
expect(screen.getByText('€15')).toBeInTheDocument();
expect(screen.getByText('Downloading...')).toBeInTheDocument();
act(() => {
vi.advanceTimersByTime(10000);
});
expect(screen.getByText(/Attempted to launch Acupedia App/i)).toBeInTheDocument();
rerender(
<PaymentSuccessModal
verificationResult={{ ...baseResult, currency: 'USD', amount: 20 }}
onClose={vi.fn()}
loggedInUser={null}
onDownloadInvoice={vi.fn()}
downloadingInvoice={false}
/>
);
expect(screen.getByText('$20')).toBeInTheDocument();
});
it('handles Android and iOS user agents on launchMobileApp', async () => {
const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime });
const originalUA = navigator.userAgent;
Object.defineProperty(navigator, 'userAgent', { value: 'Android', configurable: true });
const { rerender } = render(
<PaymentSuccessModal
verificationResult={baseResult}
onClose={vi.fn()}
loggedInUser={null}
onDownloadInvoice={vi.fn()}
downloadingInvoice={false}
/>
);
await user.click(screen.getByRole('button', { name: /Open in App \/ Library/i }));
act(() => {
vi.advanceTimersByTime(1100);
});
Object.defineProperty(navigator, 'userAgent', { value: 'iPhone', configurable: true });
rerender(
<PaymentSuccessModal
verificationResult={{ ...baseResult, currency: 'USD' }}
onClose={vi.fn()}
loggedInUser={null}
onDownloadInvoice={vi.fn()}
downloadingInvoice={false}
/>
);
await user.click(screen.getByRole('button', { name: /Open in App \/ Library/i }));
act(() => {
vi.advanceTimersByTime(1000);
});
Object.defineProperty(navigator, 'userAgent', { value: originalUA, configurable: true });
});
});