This directory contains unit tests for script.ts and related UI/state behaviour.
The test suite validates critical functionality including:
- Constants validation: SWEDEN_BOUNDS, ACCURACY_THRESHOLD_METERS, SPEED_THRESHOLD_MS
- PROJ definition verification: SWEREF 99 TM (EPSG:3006) coordinate system definition
- Coordinate transformation: WGS84 to SWEREF 99 TM conversion
- Input validation: Rejects invalid coordinates before projection attempts
- ITRF to ETRS89 correction: Continental drift calculations
- Boundary validation: Checks if coordinates are within Swedish territory
- Integration scenarios: Complete workflows combining multiple functions
- Button state handling: Share/start/stop button behaviour
- Details state persistence: Saving and restoring expanded help sections
- Coordinate formatting: UI alignment and share text formatting
- Speed units: m/s, km/h, and mph conversion and cycling
npm testnpm run test:watchnpm run test:coverageThe coverage report will be generated in the coverage/ directory.
script.test.ts: Core coordinate logic, bounds, thresholds, and integration coveragebutton-state.test.ts: Button enable/disable state transitionsdetails-state.test.ts: Details element persistence with localStoragecoordinate-formatting.test.ts: Coordinate display and share text formattingspeed-units.test.ts: Speed unit conversion and cycling behaviour
Validates that the geographic bounds for Sweden are correctly defined:
- Latitude range: 55° to 69°
- Longitude range: 10° to 24°
- Internal consistency checks
Tests boundary validation for Swedish territory:
- Typical locations: Stockholm, Gothenburg, Malmö, Kiruna
- Boundary cases: Exact min/max coordinates and edge cases
- Outside Sweden: Non-Swedish locations (Berlin, London, New York)
Validates GPS accuracy threshold (5 meters):
- Appropriate for smartphone GPS accuracy (3-5m optimal)
- Usage scenarios from good (3m) to poor (20m) accuracy
Validates speed threshold (1.4 m/s for walking):
- Represents upper end of walking speed (4-5 km/h)
- Usage scenarios from stationary to driving speeds
Validates the PROJ definition string for EPSG:3006:
- Core parameters: UTM projection, zone 33, GRS80 ellipsoid
- Datum transformation: Zero transformation (ETRS89 ≈ WGS84)
- Technical parameters: Units, no_defs, type=crs
- Format validation: PROJ string structure and syntax
- Official compliance: Matches EPSG:3006 specification
Tests continental drift correction calculations:
- Returns valid correction objects with
dnanddeproperties - Positive corrections (drift since 1989)
- North component larger than east (25° azimuth)
- Values within expected ranges based on 2.5 cm/year drift rate
- Consistency across multiple calls
Tests coordinate transformation from WGS84 to SWEREF 99 TM:
- Coordinate transformation: Valid transformations for Swedish locations
- Edge cases: Boundaries of Swedish territory
- Coordinate system properties: Increasing northing/easting with lat/lon
- Consistency: Same inputs produce same outputs, different inputs differ
- Precision: Small coordinate differences produce measurable results
Complete workflows combining multiple functions:
- Sweden boundary validation with coordinate transformation
- Accuracy threshold validation
- Speed threshold validation
- Full coordinate processing workflow
The test suite achieves comprehensive coverage of:
- ✅ All exported constants
- ✅ All coordinate transformation logic
- ✅ Boundary validation functions
- ✅ Continental drift correction calculations
- ✅ Edge cases and error handling
- ✅ Integration scenarios
- ✅ UI state, persistence, and formatting helpers
The tests use a mocked version of the proj4 library since it's loaded from CDN in production. The mock provides:
- Basic coordinate transformation approximation
- Coordinate system definition registration
- Sufficient accuracy for testing logic correctness
Current Approach:
The test suite contains copies of constants and functions from src/script.ts rather than importing them directly. This creates some duplication but is necessary because:
- Top-level code:
script.tsexecutes DOM-dependent code at the module level (event listeners, DOM queries) - Browser-only design: The file is designed as a single-file browser application, not a modular library
- Minimal modifications: Following the principle of minimal changes to existing working code
Advantages:
- Tests can run in isolation without DOM dependencies
- No changes needed to the production code structure
- Tests validate the expected behavior independent of implementation details
Trade-offs:
- Constants and function implementations must be kept in sync manually
- Higher maintenance burden when source code changes
- Cannot verify test code matches source code exactly
Future Improvements: If the codebase evolves to support modular architecture:
- Refactor
script.tsto export testable functions - Separate DOM initialization from business logic
- Use ES modules to import actual functions in tests
- This would eliminate duplication and improve maintainability
For now, the duplication is documented and acceptable given the constraints.
Tests are automatically run in the GitHub Actions workflow:
- Dependencies are installed via
npm ci - Tests run via
npm test - Build only proceeds if all tests pass
When modifying src/script.ts:
- Ensure constants remain in sync with test definitions
- Update tests if function signatures or behavior change
- Add new tests for new functionality
- Run tests locally before committing
- Verify CI passes after pushing changes
- Framework: Jest 30.3.0
- TypeScript Support: ts-jest 29.4.6
- Environment: jsdom (simulates browser DOM)
- Assertion Library: Jest's built-in expect
Potential enhancements to the test suite:
- End-to-end tests for UI interactions
- Performance benchmarks for coordinate transformations
- Property-based testing for coordinate edge cases
- Visual regression testing for UI components
- Integration with real proj4 library for accuracy validation