Get started

Testing

Every part of the SDK runs in a test with no network and no device. The client takes your HTTP client and reads a clock a zone overrides. The map and navigation have scripted twins.

Test code that calls the API

Every request leaves through package:http's Client, so a test answers it with MockClient:

import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:test/test.dart';

void main() {
  test('reads the activities', () async {
    final mountaya = Mountaya(
      credential: .publishable(key: 'pk_your_publishable_key'),
      httpClient: MockClient(
        (request) async => http.Response(
          jsonEncode({'data': <Object?>[]}),
          200,
          headers: {'content-type': 'application/json'},
        ),
      ),
    );
    final activities = await mountaya.studio.activities.getActivities();
    expect(activities.data, isEmpty);
    await mountaya.close();
  });
}

Add http to your test dependencies. A client handed httpClient: leaves it open on close().

Control time

The client reads the time through package:clock, which fake_async overrides. A test drives retries, the session engine and a stream's silence with no time spent:

import 'dart:convert';

import 'package:fake_async/fake_async.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:test/test.dart';

void main() {
  test('a 503 is asked again after its wait', () {
    fakeAsync((async) {
      var attempts = 0;
      final mountaya = Mountaya(
        credential: .publishable(key: 'pk_your_publishable_key'),
        httpClient: MockClient((request) async {
          attempts += 1;
          return attempts == 1
              ? http.Response('', 503)
              : http.Response(
                  jsonEncode({'data': <Object?>[]}),
                  200,
                  headers: {'content-type': 'application/json'},
                );
        }),
      );
      int? status;
      mountaya.studio.activities.getActivities().then(
        (response) => status = response.status,
      );
      async.elapse(const Duration(seconds: 30));
      expect(attempts, 2);
      expect(status, 200);
      mountaya.close();
    });
  });
}

Script the map

ScriptedMapSurface draws a map nowhere. It answers what a renderer would, records each move and fit, and plays taps and loaded tiles:

import 'package:flutter_test/flutter_test.dart';
import 'package:mountaya_client/mountaya_client.dart';
import 'package:mountaya_elements_flutter/mountaya_elements_flutter.dart';

void main() {
  test('a tap is told as the live map tells one', () async {
    final mountaya = Mountaya(credential: .publishable(key: 'pk_your_publishable_key'));
    final map = MapEngine(mountaya);
    final surface = ScriptedMapSurface(map, canvas: const [800, 600]);

    final tapped = map.events().firstWhere((event) => event is MapEventTap);
    surface.tap([400, 300], [6.8712, 45.9231]);
    expect(await tapped, isA<MapEventTap>());

    // A fit lands where the SDK frames the box on the canvas.
    map.fitBounds([6.80, 45.85, 7.00, 46.00]);
    expect(surface.frames, hasLength(1));

    map.close();
    await mountaya.close();
  });
}

Under flutter test, MapView draws on a ScriptedMapSurface too, so a widget test of your screen needs no device.

ScriptedNavigationEngine follows a route exactly as the device engine does, and your test answers each route request:

import 'package:mountaya_geo/mountaya_geo.dart';
import 'package:test/test.dart';

void main() {
  test('arrives at Argentière', () {
    final navigation = ScriptedNavigationEngine<String>();
    navigation.start('0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21');
    navigation.sent.single.answer(
      const NavigationRoute(
        line: [
          [6.8712, 45.9231],
          [6.9271, 45.9822],
        ],
        duration: 5400,
      ),
    );
    navigation.setPosition([6.9271, 45.9822]);
    expect(navigation.state.status, NavigationStatus.arrived);
    navigation.close();
  });
}

Build errors

Every error has a public constructor, so a test builds the failure your code handles:

import 'package:mountaya_client/mountaya_client.dart';
import 'package:test/test.dart';

String advice(Object failure) => switch (failure) {
  RateLimitException(:final retryAfter?) => 'Try again in ${retryAfter.inSeconds} s.',
  NotFoundException() => 'It is gone.',
  _ => 'Something failed.',
};

void main() {
  test('a refusal reads as advice', () {
    expect(
      advice(RateLimitException(retryAfter: const Duration(seconds: 30))),
      'Try again in 30 s.',
    );
    expect(advice(NotFoundException()), 'It is gone.');
  });
}

Next steps

Last updated