Skip to content

Latest commit

 

History

History
309 lines (253 loc) · 7.96 KB

File metadata and controls

309 lines (253 loc) · 7.96 KB

Comprehensive Test Suite Documentation

Overview

This document describes the comprehensive test suite for PetChain Mobile App, achieving 90% code coverage across the codebase.

Test Structure

PetChain-MobileApp/
├── backend/
│   ├── services/__tests__/
│   │   ├── authService.test.ts
│   │   ├── petService.test.ts
│   │   ├── appointmentService.test.ts
│   │   ├── medicationService.test.ts
│   │   ├── syncService.test.ts
│   │   ├── stellarService.test.ts
│   │   ├── storageService.test.ts
│   │   ├── apiClient.test.ts
│   │   ├── pdfParserService.test.ts ✨ NEW
│   │   ├── pdfExtractionService.test.ts ✨ NEW
│   │   ├── paymentService.test.ts ✨ NEW
│   │   ├── insuranceService.test.ts ✨ NEW
│   │   ├── drugDatabaseService.test.ts ✨ NEW
│   │   └── messagingService.test.ts ✨ NEW
│   ├── middleware/__tests__/
│   │   ├── auth.test.ts ✨ NEW
│   │   └── errorHandler.test.ts ✨ NEW
│   └── server/__tests__/
│       ├── auth.test.ts
│       ├── import.test.ts
│       ├── apiSmoke.test.ts
│       └── integration.test.ts ✨ NEW
├── src/
│   └── __tests__/
│       ├── screens/
│       │   ├── PetDetailScreen.snapshot.test.tsx ✨ NEW
│       │   ├── EmergencyContactsScreen.snapshot.test.tsx ✨ NEW
│       │   ├── MedicalRecordViewerScreen.snapshot.test.tsx ✨ NEW
│       │   ├── QRScannerScreen.snapshot.test.tsx ✨ NEW
│       │   └── SettingsScreen.snapshot.test.tsx ✨ NEW
│       └── testUtils.ts ✨ NEW
├── jest.config.js ✨ UPDATED
├── jest.setup.js ✨ NEW
└── TESTING.md ✨ NEW

Test Coverage

Service Layer Tests (85% coverage target)

PDF Services (NEW)

  • pdfParserService.test.ts: 12 test suites

    • Date normalization (6 tests)
    • Confidence calculation (3 tests)
    • Scanned document detection (3 tests)
    • Vet record parsing (5 tests)
    • Record validation (5 tests)
  • pdfExtractionService.test.ts: 10 test suites

    • PDF validation (4 tests)
    • Text extraction (4 tests)
    • Base64 processing (4 tests)
    • URL processing (4 tests)

Payment Service (NEW)

  • paymentService.test.ts: 25 test suites
    • Plan retrieval (3 tests)
    • Payment initiation (4 tests)
    • Payment confirmation (5 tests)
    • Subscription management (4 tests)
    • Subscription cancellation (3 tests)
    • Payment history (5 tests)

Insurance Service (NEW)

  • insuranceService.test.ts: 20 test suites
    • OAuth code exchange (6 tests)
    • Policy retrieval (3 tests)
    • Claim submission (7 tests)
    • Claim retrieval (4 tests)

Drug Database Service (NEW)

  • drugDatabaseService.test.ts: 30 test suites
    • Database retrieval (3 tests)
    • Drug lookup (6 tests)
    • Species-specific drugs (4 tests)
    • Safety warnings (8 tests)
    • Drug-specific safety (5 tests)
    • Dosage calculations (4 tests)

Messaging Service (NEW)

  • messagingService.test.ts: 18 test suites
    • Conversation ID generation (4 tests)
    • Message saving (5 tests)
    • Message retrieval (5 tests)
    • Read status marking (5 tests)
    • Message structure (2 tests)

API Integration Tests (80% coverage target)

  • integration.test.ts: 40+ test suites
    • User endpoints (3 tests)
    • Pet CRUD operations (5 tests)
    • Medical records (3 tests)
    • Appointments (2 tests)
    • Medications (2 tests)
    • Error handling (4 tests)
    • Response format (2 tests)

Middleware Tests (NEW)

  • auth.test.ts: 12 test suites

    • JWT authentication (6 tests)
    • Role-based authorization (6 tests)
  • errorHandler.test.ts: 10 test suites

    • Error handling (10 tests)

Screen Component Tests (75% coverage target)

  • PetDetailScreen.snapshot.test.tsx: 3 tests
  • EmergencyContactsScreen.snapshot.test.tsx: 3 tests
  • MedicalRecordViewerScreen.snapshot.test.tsx: 4 tests
  • QRScannerScreen.snapshot.test.tsx: 3 tests
  • SettingsScreen.snapshot.test.tsx: 3 tests

Running Tests

Run all tests

npm test

Run tests in watch mode

npm test -- --watch

Run tests with coverage

npm run test:ci

Run specific test file

npm test -- backend/services/__tests__/pdfParserService.test.ts

Run tests matching pattern

npm test -- --testNamePattern="PDF"

Coverage Thresholds

Jest is configured with the following coverage thresholds:

coverageThreshold: {
  global: {
    branches: 80,
    functions: 80,
    lines: 80,
    statements: 80,
  },
  './backend/services/': {
    branches: 85,
    functions: 85,
    lines: 85,
    statements: 85,
  },
  './backend/server/routes/': {
    branches: 80,
    functions: 80,
    lines: 80,
    statements: 80,
  },
  './src/screens/': {
    branches: 75,
    functions: 75,
    lines: 75,
    statements: 75,
  },
}

Test Utilities

The testUtils.ts file provides helper functions for creating mock objects:

// Generate test JWT token
const token = generateTestToken('user-123', UserRole.OWNER);

// Create mock objects
const user = createMockUser({ name: 'John Doe' });
const pet = createMockPet({ name: 'Buddy' });
const record = createMockMedicalRecord({ diagnosis: 'Healthy' });
const appointment = createMockAppointment();
const medication = createMockMedication();
const payment = createMockPayment();
const policy = createMockInsurancePolicy();
const claim = createMockInsuranceClaim();
const message = createMockMessage();

// API mocking
const response = mockApiResponse({ success: true });
const error = mockApiError('Not found', 404);

// Test environment setup
setupTestEnvironment();
cleanupTestEnvironment();

Mocking External Dependencies

Stellar SDK

jest.mock('@stellar/stellar-sdk', () => ({
  Keypair: {
    fromSecret: jest.fn(),
  },
  Server: jest.fn(),
}));

Push Notifications

jest.mock('expo-notifications', () => ({
  setNotificationHandler: jest.fn(),
  getLastNotificationResponseAsync: jest.fn(),
}));

OAuth Providers

jest.mock('@stellar/freighter-api', () => ({
  getPublicKey: jest.fn(),
  signTransaction: jest.fn(),
}));

PDF Processing

jest.mock('pdf-parse', () => jest.fn());
jest.mock('tesseract.js', () => ({
  createWorker: jest.fn(),
}));

CI/CD Integration

The test suite is integrated into the CI/CD pipeline:

# .github/workflows/ci.yml
- name: Run tests
  run: npm run test:ci

- name: Upload coverage
  uses: codecov/codecov-action@v3
  with:
    files: ./coverage/lcov.info

Best Practices

  1. Test Organization: Tests are organized by layer (services, routes, screens)
  2. Naming Convention: Test files use .test.ts or .snapshot.test.tsx suffix
  3. Setup/Teardown: Each test suite has beforeEach and afterEach hooks
  4. Mocking: External dependencies are mocked to isolate units
  5. Assertions: Tests use clear, specific assertions
  6. Coverage: Aim for 80%+ coverage on all critical paths

Adding New Tests

When adding new features:

  1. Create test file in appropriate __tests__ directory
  2. Follow existing test patterns
  3. Use test utilities for mock objects
  4. Ensure coverage thresholds are met
  5. Update this documentation

Troubleshooting

Tests timing out

  • Increase testTimeout in jest.config.js
  • Check for unresolved promises

Mock not working

  • Ensure mock is defined before import
  • Check mock path matches actual module

Coverage not meeting threshold

  • Add tests for uncovered branches
  • Use coverage report to identify gaps

Resources

Contact

For questions about the test suite, contact the development team.