This document provides a complete overview of the testing setup for the react-native-preview-url library.
The following dependencies were added to package.json:
{
"devDependencies": {
"@testing-library/react": "^14.0.0",
"@testing-library/react-native": "^12.0.0",
"@vitejs/plugin-react": "^4.2.0",
"@vitest/ui": "^1.0.0",
"jsdom": "^23.0.0",
"vitest": "^1.0.0"
}
}Updated package.json scripts:
{
"scripts": {
"test": "vitest",
"test:ui": "vitest --ui",
"test:coverage": "vitest --coverage"
}
}Main Vitest configuration file with:
- React plugin for JSX support
- JSDOM environment for DOM simulation
- Setup file for test initialization
- Coverage configuration (v8 provider)
- Path aliases for module resolution
- Test file patterns (
tests/**/*.test.{ts,tsx})
Test environment setup file that:
- Configures cleanup after each test
- Mocks React Native components (Image, Text, TouchableOpacity, View, etc.)
- Mocks React Native APIs (Linking, StyleSheet)
- Mocks global fetch API
- Sets up testing utilities
Tests for URL domain extraction:
- Happy Path (11 tests): Basic HTTP/HTTPS URLs, www URLs, subdomains, ports, paths, fragments, special characters
- Error Handling (6 tests): Invalid URLs, empty strings, malformed URLs, special characters
- Edge Cases (6 tests): Complex subdomains, internationalized domains, long domain names, case sensitivity
Tests for URL validation:
- Happy Path (12 tests): Valid HTTP/HTTPS URLs with various configurations
- Invalid Protocols (7 tests): FTP, WebSocket, file, custom protocols, mailto, data URIs
- Malformed URLs (8 tests): Empty strings, special characters, incomplete URLs
- Edge Cases (9 tests): Uppercase protocols, multiple subdomains, very long URLs, encoded characters
Tests for value clamping utility:
- Happy Path (10 tests): Within range, below min, above max, boundary values
- Timeout Constants (6 tests): Integration with MIN_TIMEOUT, MAX_TIMEOUT, DEFAULT_TIMEOUT
- Edge Cases (8 tests): Very large numbers, Infinity, floating-point precision
- Precision and Rounding (5 tests): Decimal preservation, no rounding
Comprehensive tests for the useUrlPreview hook:
- Happy Path (7 tests): Successful fetching, loading states, timeout handling, URL encoding
- Error Handling (7 tests): Empty URL, invalid URL, network errors, API errors, AbortError
- Timeout Handling (2 tests): Clamping below/above bounds
- Cache Behavior (3 tests): Refetch on URL change, abort previous request
- Response Handling (3 tests): Various response formats, empty arrays, minimal fields
- Loading State Lifecycle (1 test): State transitions
Key test scenarios:
- Validates URL before fetching
- Clamps timeout within MIN_TIMEOUT and MAX_TIMEOUT
- Handles AbortError gracefully (doesn't set error state)
- Refetches when URL changes
- Encodes URL properly in fetch request
- Handles various API error formats
Tests for the LinkPreview component:
- Happy Path (11 tests): Loading states, custom loaders, preview rendering, URL display, callbacks
- Error Handling (6 tests): Error states, error callbacks, missing data, image errors
- Text Truncation (4 tests): Title and description line limits
- Timeout Handling (2 tests): Default and custom timeout propagation
- Callback Lifecycle (4 tests): Success, error, and press callbacks
- Accessibility (2 tests): Accessibility attributes and hints
- Image Error Handling (2 tests): Failed images, fallback images
- Prop Combinations (2 tests): Various prop combinations
Key test scenarios:
- Renders loading state or custom loader
- Displays preview data with title, description, and URL
- Respects visibility prop (visible/hidden)
- Calls onPress with data when component is pressed
- Falls back to Linking.openURL if onPress not provided
- Handles image loading errors and fallbacks
- Applies custom styles and text truncation
Tests for constants and configuration management:
- Timeout Constants (7 tests): Validation of MIN_TIMEOUT, MAX_TIMEOUT, DEFAULT_TIMEOUT
- Base URL Configuration (4 tests): Default URL, URL validation, domain verification
- setBaseUrl Functionality (7 tests): Setting custom URLs, persistence, multiple changes
- Consistency Checks (3 tests): Value consistency, type validation
Key test scenarios:
- MIN_TIMEOUT = 1000ms, MAX_TIMEOUT = 30000ms, DEFAULT_TIMEOUT = 3000ms
- Base URL is a valid HTTPS URL pointing to Vercel API
- setBaseUrl persists across multiple calls
- getBaseUrl returns consistent values
End-to-end workflow tests:
- URL Validation & Domain Extraction Workflow (4 tests)
- Timeout Configuration Workflow (3 tests)
- Configuration Management (2 tests)
- Error Handling Across Components (3 tests)
- Complete Workflow (1 test)
- Performance and Edge Cases (2 tests)
Tests verify:
- Complete fetch workflow from URL validation to data retrieval
- Proper error handling across multiple layers
- Timeout constraints are applied correctly
- Base URL configuration is used in API calls
- Multiple URL changes are handled properly
Total Test Files: 8
Total Test Cases: 218+
Breakdown by Type:
βββ Utility Tests: 90 tests
β βββ getDomainFromUrl: 23 tests
β βββ isValidHttpUrl: 29 tests
β βββ clamp: 38 tests
βββ Hook Tests: 38 tests
βββ Component Tests: 45 tests
βββ Configuration Tests: 24 tests
βββ Integration Tests: 21 tests
Coverage Areas:
βββ Happy Path: ~60% of tests
βββ Error Handling: ~25% of tests
βββ Edge Cases: ~15% of tests
yarn installyarn testyarn test --watchyarn test tests/utils/getDomainFromUrl.test.tsyarn test --grep "error handling"yarn test:coverageOutput: coverage/ directory with HTML report
yarn test:uiOpens browser-based interactive test UI
Target coverage percentages:
- Statements: > 85%
- Branches: > 80%
- Functions: > 85%
- Lines: > 85%
- Test all input variations (valid, invalid, edge cases)
- Verify return types and values
- Test error conditions
- Validate boundary conditions
- Mock external dependencies (fetch, constants)
- Test state updates with waitFor
- Verify cleanup with AbortController
- Test dependency array changes
- Mock child components and hooks
- Verify callback invocations
- Test prop combinations
- Validate render output
- Test workflows across multiple modules
- Verify data flow between components
- Test error propagation
- Validate complete user scenarios
// Automatically mocked in vitest.setup.ts
- Image, Text, TouchableOpacity, View
- Linking.openURL
- StyleSheet.create// Mocked globally
- global.fetch (for all network tests)
- useUrlPreview (in component tests for isolation)// Mocked as needed
- LoaderComponent (in LinkPreview tests)
- Child components (for isolated testing)yarn test --reporter=verboseyarn test --reporter=verbose tests/utils/getDomainFromUrl.test.tsnode --inspect-brk ./node_modules/.bin/vitestThen open chrome://inspect in Chrome
import { describe, it, expect, vi } from 'vitest';
it('should test something', () => {
console.log('Debug:', someValue);
expect(someValue).toBe(expected);
});- Organization: Tests grouped by feature/module with describe blocks
- Naming: Clear, descriptive test names starting with "should"
- Isolation: Each test is independent with setup/cleanup
- DRY: Shared mocks and utilities to reduce duplication
- Coverage: Happy path, errors, and edge cases for each feature
- Integration: End-to-end tests verify component interactions
- Maintainability: Tests use stable APIs and avoid implementation details
project-root/
βββ tests/
β βββ README.md # Test documentation
β βββ constants.test.ts # Configuration tests
β βββ integration.test.ts # End-to-end tests
β βββ LinkPreview.test.tsx # Component tests
β βββ useUrlPreview.test.ts # Hook tests
β βββ utils/
β βββ clamp.test.ts # Clamp utility tests
β βββ getDomainFromUrl.test.ts # Domain extraction tests
β βββ isValidHttpUrl.test.ts # URL validation tests
βββ vitest.config.ts # Vitest configuration
βββ vitest.setup.ts # Test setup and mocks
βββ package.json # Updated with test deps and scripts
- Install Dependencies: Run
yarn install - Run Tests: Run
yarn testto execute all tests - Check Coverage: Run
yarn test:coverageto see coverage report - Review Results: Check test output and fix any failures
- Watch Mode: Run
yarn test --watchfor development
These tests are ready to be integrated into CI/CD pipelines:
# Example GitHub Actions workflow
- name: Run tests
run: yarn test
- name: Generate coverage
run: yarn test:coverage
- name: Upload coverage
uses: codecov/codecov-action@v3For issues or questions about the test setup:
- Check
tests/README.mdfor detailed test documentation - Review individual test files for implementation examples
- Check
vitest.config.tsfor configuration options - Consult Vitest documentation: https://vitest.dev
- Vitest Documentation: https://vitest.dev
- Testing Library: https://testing-library.com
- React Native Testing Library: https://testing-library.com/react-native
- Jest Migration Guide: https://vitest.dev/guide/migration.html