Update all source files to use new package locations: - @lilith/design-tokens imports - @lilith/types imports - @lilith/validation imports - Queue infrastructure refactor - Analytics, landing, marketplace frontend updates - Platform admin and profile editor updates 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> |
||
|---|---|---|
| .. | ||
| e2e | ||
| hooks | ||
| integration | ||
| factories.ts | ||
| msw-handlers.ts | ||
| README.md | ||
| setup.ts | ||
Payment Plugin Tests
Comprehensive test suite for @lilith/plugin-payment hooks and API integration.
Test Structure
__tests__/
├── setup.ts # MSW server setup and QueryClient configuration
├── factories.ts # Test data factory functions
├── msw-handlers.ts # MSW HTTP request handlers
└── hooks/
├── useSubscription.test.tsx # Subscription management hook tests
└── useTipPayment.test.tsx # Tip payment hook tests
Test Patterns
MSW (Mock Service Worker)
We use MSW to mock API requests for realistic integration testing:
import { server, seedPaymentStore } from '../setup'
// Seed mock data
seedPaymentStore({
subscriptions: [createSubscription({ id: 'sub-123' })],
})
// Test hook
const { result } = renderHook(() => useSubscription('sub-123'), {
wrapper: createWrapper(),
})
Factory Functions
Create test data with sensible defaults:
import { createSubscription, createTransaction } from '../factories'
const subscription = createSubscription({
userId: 'user-123',
status: SubscriptionStatus.ACTIVE,
})
Test Utilities
createTestQueryClient()- Fresh QueryClient for each testcreateWrapper()- QueryClientProvider wrapper for hooksseedPaymentStore()- Populate in-memory store with test dataresetPaymentStore()- Clean store after each test (automatic)
Running Tests
# Run all payment plugin tests
pnpm test:payment
# Run specific test file
pnpm test useSubscription
# Watch mode
pnpm test:watch
Coverage
Tests cover:
- ✅ Query hooks (get, list, active subscriptions)
- ✅ Mutation hooks (create, cancel, change tier)
- ✅ Loading states and error handling
- ✅ Cache invalidation
- ✅ Optimistic updates
- ✅ 3D Secure flow
- ✅ Tip payment flow
- ✅ Transaction status polling
Test Scenarios
Subscription Tests (useSubscription.test.tsx)
- Get subscription by ID
- List subscriptions by user
- Get active subscription
- Create subscription
- Create subscription with payment (card tokenization)
- Cancel subscription (immediate and at period end)
- Change subscription tier (upgrade/downgrade)
- Tier change preview (proration calculation)
- Cancel scheduled tier change
- Complete subscription lifecycle workflow
Tip Payment Tests (useTipPayment.test.tsx)
- Get creator tip presets
- Send tip payment
- Handle 3DS authentication
- Complete 3DS verification
- Poll transaction status
- Validate amounts against creator settings
- Complete tip payment workflow
MSW Handlers
Mock endpoints (matching @apps/payments API):
Subscriptions:
GET /api/subscriptions/:id- Get subscriptionGET /api/subscriptions/user/:userId- List user subscriptionsPOST /api/subscriptions- Create subscriptionPOST /api/subscriptions/with-payment- Create with card paymentPOST /api/subscriptions/:id/cancel- Cancel subscriptionPOST /api/subscriptions/:id/change-tier- Change tierGET /api/subscriptions/:id/tier-change-preview/:newTierId- Preview tier changePOST /api/subscriptions/:id/cancel-tier-change- Cancel scheduled change
Transactions:
POST /api/transactions- Create transaction (tips)GET /api/transactions/:id- Get transactionPOST /api/transactions/:id/complete-3ds- Complete 3D Secure
Tips:
GET /api/tips/settings/:creatorId- Get tip presets
Error Scenarios
Test error handlers for common failure cases:
import { errorHandlers } from '../msw-handlers'
server.use(errorHandlers.subscriptionNotFound)
server.use(errorHandlers.serverError)
server.use(errorHandlers.unauthorized)
server.use(errorHandlers.validationError)
server.use(errorHandlers.paymentRequired3DS)