tests — location

Module: tests-location Cohesion: 0.80 Members: 0

tests — location

This document provides an overview of the tests/location/location.test.ts module, which is responsible for validating the functionality of the LocationService and its associated utility functions defined in src/location/index.ts.

Location Module Tests

The location.test.ts file contains a comprehensive suite of tests designed to ensure the correctness, reliability, and expected behavior of the LocationService and its core geographical utility functions. It covers various aspects, from basic distance calculations to complex service configurations, caching, and event emissions.

Purpose

The primary goals of this test module are:

Module Structure

The tests are organized into three main describe blocks, reflecting the logical separation of concerns within the src/location module:

  1. Location Utilities: Tests for standalone, pure functions that perform geographical calculations.
  2. LocationService: Comprehensive tests for the LocationService class instance, covering its lifecycle, configuration, data retrieval, and various features.
  3. Singleton: Tests specifically for the getLocationService factory function and resetLocationService to ensure correct singleton pattern implementation.
graph TD
    A[tests/location/location.test.ts] --> B{src/location/index.ts}
    B --> C[LocationService Class]
    B --> D[getLocationService()]
    B --> E[resetLocationService()]
    B --> F[calculateDistance()]
    B --> G[calculateBearing()]
    B --> H[bearingToCardinal()]
    B --> I[isWithinRadius()]
    B --> J[formatCoordinates()]

Location Utilities Tests

This section focuses on the pure utility functions that operate on GeoCoordinates objects (defined as { latitude: number; longitude: number; }).

calculateDistance(from: GeoCoordinates, to: GeoCoordinates)

calculateBearing(from: GeoCoordinates, to: GeoCoordinates)

bearingToCardinal(bearing: number)

isWithinRadius(point: GeoCoordinates, center: GeoCoordinates, radiusMeters: number)

formatCoordinates(coords: GeoCoordinates, format: 'decimal' | 'dms')

LocationService Tests

This is the most extensive section, testing the LocationService class. Each test block within LocationService is set up with beforeEach and afterEach hooks to ensure a clean, isolated LocationService instance for every test.

beforeEach(() => {
  resetLocationService(); class="hl-cmt">// Ensures no shared state from previous tests
  service = new LocationService({
    cacheEnabled: false,
    reverseGeocode: false,
  });
});

afterEach(() => {
  service.shutdown(); class="hl-cmt">// Cleans up any active timers/listeners
  resetLocationService();
});

Configuration

Location Retrieval

Caching

Reverse Geocoding

Timezone

Source Management

Distance & Direction (Service Context)

Auto-Update

Statistics

Manual Location

Singleton Tests

This section specifically tests the singleton pattern implementation for the LocationService.

Contributing to Tests

When adding new features or fixing bugs in the src/location module, developers should:

  1. Add New Test Cases: Create new it blocks within the relevant describe block to cover the new functionality or specific bug fix.
  2. Consider Edge Cases: Think about invalid inputs, boundary conditions, and asynchronous behaviors.
  3. Maintain Isolation: Ensure that tests are independent and do not rely on the state set by previous tests. The beforeEach and afterEach hooks for LocationService are critical for this.
  4. Use Mocks Appropriately: For LocationService tests, use createMockLocation and setMockLocation to control the location data, preventing reliance on external services during testing.
  5. Verify Event Emissions: If your feature involves events, add tests to ensure events are emitted correctly with the expected payload.