Orders API Test Coverage
Last Updated: November 28, 2025 Test Suite Version: 1.0.0 Coverage: 100%
Summary
Comprehensive test suite for Orders API routes covering all endpoints with 100% code coverage.
Coverage Metrics
-------------|---------|----------|---------|---------|
File | % Stmts | % Branch | % Funcs | % Lines |
-------------|---------|----------|---------|---------|
All files | 100 | 100 | 100 | 100 |
orders | 100 | 100 | 100 | 100 |
route.ts | 100 | 100 | 100 | 100 |
orders/[id] | 100 | 100 | 100 | 100 |
route.ts | 100 | 100 | 100 | 100 |
-------------|---------|----------|---------|---------|
Test Statistics
- Test Suites: 2 passed, 2 total
- Tests: 25 passed, 25 total
- Snapshots: 0 total
- Test Duration: ~2 seconds
Test Files
1. POST/GET /api/orders
File: src/app/api/orders/__tests__/route.test.ts
Tests: 17
POST /api/orders - Create Order (10 tests)
- Authentication (1)
- ✓ Requires authentication
- Order Creation (4)
- ✓ Creates order from cart items
- ✓ Calculates total correctly with multiple items
- ✓ Returns error when cart is empty
- ✓ Clears cart after order creation
- Validation (3)
- ✓ Rejects missing shipping address
- ✓ Rejects invalid shipping address fields
- ✓ Rejects missing billing address
- Rate Limiting (1)
- ✓ Returns 429 when rate limit exceeded
- Error Handling (1)
- ✓ Returns 500 on database error
GET /api/orders - List Orders (7 tests)
- Authentication (1)
- ✓ Requires authentication
- Order Retrieval (4)
- ✓ Returns user's orders
- ✓ Returns orders sorted by date (newest first)
- ✓ Returns empty array for user with no orders
- ✓ Only returns orders belonging to the authenticated user
- Rate Limiting (1)
- ✓ Returns 429 when rate limit exceeded
- Error Handling (1)
- ✓ Returns 500 on database error
2. GET /api/orders/[id]
File: src/app/api/orders/__tests__/[id].test.ts
Tests: 8
GET /api/orders/[id] - Get Single Order (8 tests)
- Authentication (1)
- ✓ Requires authentication
- Order Retrieval (5)
- ✓ Returns order with items for authenticated user
- ✓ Returns 404 for non-existent order
- ✓ Returns 403 when accessing another user's order
- ✓ Returns 400 for invalid order ID
- ✓ Includes product details in order items
- Rate Limiting (1)
- ✓ Returns 429 when rate limit exceeded
- Error Handling (1)
- ✓ Returns 500 on database error
Test Categories
| Category | Tests | Coverage |
|---|---|---|
| Authentication | 3 | 100% |
| Authorization | 1 | 100% |
| Validation | 4 | 100% |
| Business Logic | 5 | 100% |
| Rate Limiting | 3 | 100% |
| Error Handling | 4 | 100% |
| Data Retrieval | 5 | 100% |
| Total | 25 | 100% |
Running Tests
All Orders Tests
npm test -- "src/app/api/orders/__tests__/"
With Coverage
npm test -- "src/app/api/orders/__tests__/" --coverage \
--collectCoverageFrom='src/app/api/orders/**/*.ts' \
--collectCoverageFrom='!src/app/api/orders/**/*.test.ts'
Individual Files
# POST/GET tests
npm test -- src/app/api/orders/__tests__/route.test.ts
# [id] route tests
npm test -- "src/app/api/orders/__tests__/\[id\].test.ts"
Key Features Tested
Authentication & Authorization
- ✓ All endpoints require authentication (401)
- ✓ Order ownership verification (403)
- ✓ Session user ID validation
Input Validation
- ✓ Zod schema validation for POST requests
- ✓ Invalid order ID handling
- ✓ Missing required fields rejection
- ✓ Field-level validation (min lengths)
Business Logic
- ✓ Correct total calculation
- ✓ Empty cart prevention
- ✓ Order status initialization
- ✓ Cart clearing in transaction
Rate Limiting
- ✓ Standard rate limit preset (100 requests)
- ✓ Proper rate limit headers
- ✓ Different keys for different endpoints
Error Handling
- ✓ Graceful database error handling (500)
- ✓ Validation error responses with details
- ✓ Not found handling (404)
- ✓ Authorization failures (403)
Related Documentation
Last Updated: November 28, 2025