Workout Window Feature - Testing Guide
Overview
This document describes the test suite for the Workout Window feature, including backend API tests, frontend component tests, and database constraint tests.
Test Structure
apps/backend/src/routes/__tests__/
└── workout-window.test.ts # Backend API route tests
apps/frontend/src/app/dashboard/workout-window/__tests__/
└── page.test.tsx # Frontend component tests
supabase/migrations/
└── 025_add_workout_window.test.sql # Database constraint tests
Backend Tests
Setup
- Install dependencies:
cd apps/backend
npm install
- Run tests:
npm test
npm run test:watch # Watch mode
npm run test:coverage # With coverage report
Test Coverage
POST /api/workout-window/setup
- ✅ Creates workout window successfully
- ✅ Rejects invalid activity type
- ✅ Requires custom activity when type is "other"
- ✅ Rejects window longer than 2.5 hours
- ✅ Rejects window where end time is before start time
- ✅ Requires timezone
- ✅ Deactivates existing windows before creating new one
GET /api/workout-window/my-window
- ✅ Returns user window and location
- ✅ Returns null if no window exists
PUT /api/workout-window/update-location
- ✅ Updates user location successfully
GET /api/workout-window/chain/:date
- ✅ Returns chain data for valid date
- ✅ Rejects invalid date format
- ✅ Returns null chain if no chain exists
GET /api/workout-window/checkin/:date
- ✅ Returns check-in status
- ✅ Rejects invalid date format
Mocking Strategy
- Supabase Client: Mocked to return controlled responses
- Authentication Middleware: Mocked to inject test user
- Express App: Isolated test app instance
Frontend Tests
Setup
- Install dependencies (if not already installed):
cd apps/frontend
npm install
- Run tests:
npm test workout-window
npm run test:watch
Test Coverage
Component Rendering
- ✅ Renders setup form
- ✅ Displays all activity type options
- ✅ Shows custom activity input when "other" is selected
- ✅ Displays existing window if one exists
- ✅ Shows location information when available
Form Validation
- ✅ Validates time window on change
- ✅ Requires custom activity when "other" is selected
- ✅ Shows error message on API failure
Form Submission
- ✅ Submits form with valid data
- ✅ Calls API with correct payload
Mocking Strategy
- Next.js Router: Mocked
useRouterhook - API Client: Mocked
apiRequestfunction - Supabase Client: Mocked authentication
Database Constraint Tests
Running Tests
The SQL test file (025_add_workout_window.test.sql) contains test queries that verify database constraints. These can be run manually in a test database:
# Connect to your test Supabase database
psql -h your-db-host -U postgres -d postgres
# Run the test file
\i supabase/migrations/025_add_workout_window.test.sql
Test Coverage
Time Window Constraints
- ✅ Valid time window (should succeed)
- ✅ Invalid time window - end before start (should fail)
- ✅ Window too long > 2.5 hours (should fail)
- ✅ Valid 2.5 hour window (should succeed)
Activity Type Constraints
- ✅ Invalid activity type (should fail)
Unique Constraints
- ✅ Only one active window per user (should fail on duplicate)
- ✅ One check-in per user per date (should fail on duplicate)
Team Color Constraints
- ✅ Invalid team color (should fail)
- ✅ Valid team color (should succeed)
Check-in Status Constraints
- ✅ Invalid status (should fail)
Battle Chips Constraints
- ✅ Negative battle chips (should fail)
Mosaic Counts Constraints
- ✅ Counts exceeding total participants (should fail)
Running All Tests
Backend Tests
cd apps/backend
npm test
Frontend Tests
cd apps/frontend
npm test workout-window
Database Tests
# Run manually in test database
psql -h your-db-host -U postgres -d postgres < supabase/migrations/025_add_workout_window.test.sql
Continuous Integration
Tests should be run automatically on:
- Pull requests
- Pushes to main branch
- Before deployments
Test Coverage Goals
- Backend API Routes: > 80% coverage
- Frontend Components: > 75% coverage
- Database Constraints: 100% (all constraints tested)
Adding New Tests
Backend API Test
- Add test case to
apps/backend/src/routes/__tests__/workout-window.test.ts - Mock Supabase responses
- Test both success and error cases
- Verify request/response structure
Frontend Component Test
- Add test case to
apps/frontend/src/app/dashboard/workout-window/__tests__/page.test.tsx - Mock API calls
- Test user interactions
- Verify UI state changes
Database Constraint Test
- Add test query to
supabase/migrations/025_add_workout_window.test.sql - Test both valid and invalid cases
- Document expected behavior in comments
Troubleshooting
Backend Tests
Issue: Tests fail with module resolution errors
Solution: Ensure jest.config.js is properly configured for ESM modules
Issue: Supabase mocks not working
Solution: Verify mock setup in beforeEach blocks
Frontend Tests
Issue: Router mocks not working
Solution: Ensure useRouter is properly mocked before component render
Issue: API mocks not working
Solution: Verify apiRequest mock is set up correctly
Database Tests
Issue: Constraint violations not caught Solution: Ensure test database has constraints enabled and migration has been run
Best Practices
- Isolation: Each test should be independent
- Mocking: Mock external dependencies (Supabase, API calls)
- Coverage: Test both success and error paths
- Naming: Use descriptive test names
- Cleanup: Clean up test data after tests