Ark MVP Test

Pure Dart tools for testing your own Ark MVP Presenters: adapt a Model into ViewState, observe one-time effects, and verify shutdown without a Flutter app.

Русская версия

Installation

dependencies:
  ark_mvp: ^1.2.1
dev_dependencies:
  ark_mvp_test: ^1.0.0
  test: ^1.31.2

Import core directly when your test defines a Presenter. Keep the test helper out of application runtime dependencies.

What it removes from each feature test

A Presenter test normally creates a PresenterSession, subscribes to effects and invalidations before starting it, collects delivered events, and releases the session. PresenterTestDriver handles that mechanical setup, using the real Ark MVP session rather than a substitute implementation.

It does not invent a Model, aggregate business state, automatically build ViewState after invalidation, or run a ViewEffect such as navigation. Your test controls Model updates, presentation environment and the action under test.

One complete test

This is a pure Dart test. It changes the Model and the presentation vocabulary independently; neither change creates a new Presenter.

import 'package:ark_mvp/ark_mvp.dart';
import 'package:ark_mvp_test/ark_mvp_test.dart';
import 'package:test/test.dart';

void main() {
  test('adapt the current count for the chosen environment', () async {
    final PresenterTestDriver<int, String, String, String> driver =
        PresenterTestDriver<int, String, String, String>(
          presenter: _CountPresenter(),
          initialModel: 3,
        );
    addTearDown(driver.close);
    driver.start();

    expect(driver.buildViewState('tasks'), '3 tasks');
    expect(driver.buildViewState('задачи'), '3 задачи');
    driver.updateModel(4);
    expect(driver.buildViewState('tasks'), '4 tasks');

    await driver.close();
    await driver.observer.waitForDone();
    expect(driver.observer.effects, isEmpty);
  });
}

final class _CountPresenter extends Presenter<int, String, String, String> {
  @override
  String buildViewState(final String environment) => '$model $environment';
}

The four type arguments are Model, ViewState, ViewEffect and Environment, matching Presenter. The example uses integers and strings to keep the setup small; real features can use immutable classes and typed effect hierarchies. addTearDown is supplied by the test runner, not installed by this package.

Effects and invalidation

Use the concrete Presenter reference to perform the action. Then await a delivered count, for example await driver.observer.waitForEffectCount(1). Listeners are attached by the driver constructor before start, so an effect emitted in onModelAttached is captured too.

waitForInvalidationCount(1) observes a rebuild request. It does not perform the rebuild. Call driver.buildViewState(environment) explicitly to verify the resulting presentation state. viewStates records successful explicit builds only; a thrown adaptation error does not create a history entry.

A count means at least that many events, including existing history. It does not prove that another event cannot arrive later. For an exact final sequence, close the owned driver, await waitForDone, then inspect effects. List snapshots are unmodifiable and detached from later appends, but their elements are not deep-copied.

Observe an externally owned session

If another part of your test already owns the session, construct PresenterTestObserver<E>(effects: session.effects, invalidations: session.invalidations) before starting the session. Register observer.cancel in test cleanup. Unlike the driver, this observer neither starts nor closes the session or Presenter. The external owner still closes it.

There is no replay added by the observer. Events emitted before subscription remain unobserved, as required by Ark MVP.

Deadlines, cancellation and failures

Each wait has its own positive timeout (five seconds by default). Invalid counts or deadlines throw ArgumentError synchronously. A timeout stops only that wait; it does not stop observation, the Presenter action or cleanup.

PresenterTestObservationException.reason distinguishes timeout, cancelled, completedEarly and streamError. The exception identifies the awaited channel and counts. A stream error retains its original object and stack with the channel that delivered it; the first delivered error fails all pending/future waits, including completion waits. Later errors remain available in streamErrors.

Ordinary Ark MVP output channels do not turn failures from Presenter methods into stream errors. Errors thrown by start, updateModel, buildViewState and close propagate unchanged. Assert on those operations directly. Both output streams can finish even when Presenter cleanup fails: successful waitForDone is not evidence that driver.close() succeeded.

observer.cancel() releases only its two subscriptions and rejects unsatisfied waits. It is idempotent, attempts both cancellations, and propagates a cancellation failure. Cancellation is not natural completion. Already delivered history remains readable; an already-satisfied count can still succeed unless a stream error was recorded. After natural completion, cancellation is a no-op.

driver.close() returns the same Future on repeated calls. That Future represents the real cleanup and has no hidden timeout. The driver must not report success or drop subscriptions simply because an observer wait timed out. A hanging closePresenter must be released by its test-owned gate or fixed by the feature.

Deadlines use the current Dart Zone clock. In fake_async, advance that clock explicitly. In Flutter widget tests, pump the widget work before awaiting its results; these tools do not advance fake time, pump frames or run platform effects.

Complete examples

From this package directory, run:

dart test example/adaptation_test.dart
dart test example/composed_model_test.dart
dart test example/effects_and_close_test.dart
  • Adaptation and environment: build ViewState for two vocabularies, update Model, inspect completed build history.
  • Composed Model: combine task state and save state through an ordinary ModelBinding; the test explicitly reads and updates the complete snapshot. This is not a cross-manager transaction or an automatic binding subscription.
  • Effects and close: capture the initial effect and reject a late result through Presenter's guarded API after shutdown.

Boundaries

Keep this package in development/test dependencies. Runtime depends on ark_mvp only; Flutter, a test runner, DI and UseCase Forge are not runtime dependencies. Examples and tests use package:test as a development dependency.

The driver owns only its session and Presenter. Business sources captured by a ModelBinding remain application-owned. Event histories retain real effect and ViewState objects for the test lifetime; do not use this collector as production telemetry or an unbounded long-running event recorder. Cancelled observers are not automatically restarted.

Flutter host behavior still requires widget tests. A pure Dart passing test does not prove context lookup, widget removal, frame timing, navigation, or accessibility on a device.

For coding agents, the bundled instruction explains explicit builds, ownership and finite observation. It does not grant publication or deployment authority.

Licensed under Apache 2.0; see NOTICE.

Libraries

ark_mvp_test
Pure Dart tools for explicitly driving and observing Ark MVP tests.