expectStateUpdated function

void expectStateUpdated(
  1. String stateName, {
  2. int? times,
})

Assert that a state update was fired to the given stateName.

Optionally check that it was fired a specific number of times.

Example:

fireStateUpdate('MyWidget', data: 'hello');
expectStateUpdated('MyWidget');
expectStateUpdated('MyWidget', times: 1);

Implementation

void expectStateUpdated(String stateName, {int? times}) {
  final updates = NyStateTestHelpers.getUpdatesFor(stateName);
  if (times != null) {
    expect(
      updates.length,
      times,
      reason:
          'Expected $times state update(s) for "$stateName" '
          'but found ${updates.length}',
    );
  } else {
    expect(
      updates,
      isNotEmpty,
      reason: 'Expected at least one state update for "$stateName"',
    );
  }
}