parseActionMock function

Map<String, dynamic> parseActionMock(
  1. dynamic value,
  2. String location
)

Validates the wire-compatible mock used by the runtime dispatcher.

Implementation

Map<String, dynamic> parseActionMock(dynamic value, String location) {
  final fixture = _map(value, location);
  if (fixture.length != 1)
    _invalid(location, 'requires either events or responses');
  void outcome(dynamic value, String path) {
    final map = _map(value, path);
    _keys(map, {'events'}, path);
    final events = _map(map['events'], '$path.events');
    if (events.length != 1)
      _invalid('$path.events', 'requires exactly one event name');
    _name(events.keys.single, '$path.events');
    final payload =
        _map(events.values.single, '$path.events.${events.keys.single}');
    _keys(payload, {'data'}, '$path.events.${events.keys.single}');
  }

  if (fixture.containsKey('responses')) {
    final responses = fixture['responses'];
    if (responses is! List || responses.isEmpty)
      _invalid('$location.responses', 'must be a non-empty list');
    for (var i = 0; i < responses.length; i++) {
      outcome(responses[i], '$location.responses[$i]');
    }
  } else {
    outcome(fixture, location);
  }
  return fixture;
}