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

76 lines
2.8 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { SubscriptionsView } from '../../../src/components/subscriptions/SubscriptionsView';
describe('SubscriptionsView', () => {
it('renders unauthenticated prompt and opens auth modal on click', async () => {
const user = userEvent.setup();
const onOpenAuth = vi.fn();
render(
<SubscriptionsView
loggedInUser={null}
onOpenAuth={onOpenAuth}
onSwitchTab={vi.fn()}
curriculumModules={[]}
/>
);
await user.click(screen.getByRole('button', { name: 'Log In' }));
expect(onOpenAuth).toHaveBeenCalledTimes(1);
});
it('renders empty state and navigates to catalog', async () => {
const user = userEvent.setup();
const onSwitchTab = vi.fn();
render(
<SubscriptionsView
loggedInUser={{ active_subscriptions: [] } as any}
onOpenAuth={vi.fn()}
onSwitchTab={onSwitchTab}
curriculumModules={[]}
/>
);
await user.click(screen.getByRole('button', { name: 'Explore Modules Catalog' }));
expect(onSwitchTab).toHaveBeenCalledWith('storefront');
});
it('renders active, lifetime, expired, and paginated subscriptions with invoice download', async () => {
const user = userEvent.setup();
const onDownloadInvoice = vi.fn();
const future = new Date(Date.now() + 30 * 86400000).toISOString();
const past = new Date(Date.now() - 5 * 86400000).toISOString();
const subs = [
{ id: 1, plan_code: 'LIFETIME_ALL', status: 'ACTIVE', started_at: '2026-01-01', expires_at: '', payment_id: 'p1', invoice_number: 'INV-1' },
{ id: 2, plan_code: 'POINTS_ACU', status: 'ACTIVE', started_at: '2026-01-01', expires_at: past, payment_id: 'p2' },
...Array.from({ length: 5 }, (_, i) => ({
id: i + 3,
plan_code: `MOD_${i}`,
status: 'ACTIVE',
started_at: '2026-01-01',
expires_at: future,
})),
];
render(
<SubscriptionsView
loggedInUser={{ active_subscriptions: subs } as any}
onOpenAuth={vi.fn()}
onSwitchTab={vi.fn()}
curriculumModules={[
{ code: 'POINTS_ACU', name: 'Acu Points', category: 'Points', desc: 'Acu desc', priceInr: 300, priceUsd: 5, priceEur: 5 },
]}
onDownloadInvoice={onDownloadInvoice}
/>
);
expect(screen.getByText('Lifetime Access')).toBeInTheDocument();
expect(screen.getByText('Expired')).toBeInTheDocument();
await user.click(screen.getAllByRole('button', { name: /Download Invoice/i })[0]);
expect(onDownloadInvoice).toHaveBeenCalledWith('p1', undefined);
await user.click(screen.getByRole('button', { name: '2' }));
expect(screen.getByText('MOD 4')).toBeInTheDocument();
});
});