installTestApiOverlay static method

void installTestApiOverlay(
  1. EnsembleConfig config,
  2. TestApiProviderOverlay mock
)

Wraps real providers with mock for call recording and optional overrides.

Implementation

static void installTestApiOverlay(
  EnsembleConfig config,
  TestApiProviderOverlay mock,
) {
  final realProviders = Map<String, APIProvider>.from(
    config.apiProviders ?? const {},
  );

  final installed = <String, APIProvider>{};
  for (final entry in realProviders.entries) {
    var delegate = entry.value;
    if (delegate is TestApiProviderOverlay) {
      delegate = delegate.delegate;
    } else if (delegate is TestApiOverlay) {
      delegate = delegate.delegate;
    }

    if (entry.key == 'http') {
      mock.bindHttpDelegate(delegate as HTTPAPIProvider);
      installed['http'] = mock;
      continue;
    }
    installed[entry.key] = TestApiOverlay(mock, delegate);
  }

  if (!installed.containsKey('http')) {
    mock.bindHttpDelegate(HTTPAPIProvider());
    installed['http'] = mock;
  }

  final firebase = realProviders['firebase'];
  if (firebase != null && !installed.containsKey('firebaseFunction')) {
    installed['firebaseFunction'] =
        installed['firebase'] ?? TestApiOverlay(mock, firebase);
  }

  config.apiProviders = installed;
}