waitCondition method

Future<TestInfoList<St>> waitCondition(
  1. StateCondition<St> condition, {
  2. bool testImmediately = true,
  3. bool ignoreIni = true,
  4. int? timeoutInSeconds = defaultTimeout,
})

Runs until the predicate function condition returns true. This function will receive each testInfo, from where it can access the state, action, errors etc. When testImmediately is true (the default), it will test the condition immediately when the method is called. If the condition is true, the method will return immediately, without waiting for any actions to be dispatched. When testImmediately is false, it will only test the condition once an action is dispatched. Only END states will be received, unless you pass ignoreIni as false. Returns a list with all info until the condition is met.

Implementation

Future<TestInfoList<St>> waitCondition(
  StateCondition<St> condition, {
  bool testImmediately = true,
  bool ignoreIni = true,
  int? timeoutInSeconds = defaultTimeout,
}) async {
  TestInfoList<St> infoList = TestInfoList<St>();

  if (testImmediately) {
    var currentTestInfoWithoutAction = TestInfo<St>(
      _currentTestInfo.state,
      false,
      null,
      null,
      null,
      _currentTestInfo.dispatchCount,
      _currentTestInfo.reduceCount,
      _currentTestInfo.errors,
    );
    if (condition(currentTestInfoWithoutAction)) {
      infoList._add(currentTestInfoWithoutAction);
      lastInfo = infoList.last;
      return infoList;
    }
  }

  TestInfo<St> testInfo = await _next(timeoutInSeconds: timeoutInSeconds);

  while (true) {
    if (ignoreIni)
      while (testInfo.ini)
        testInfo = await (_next(
          timeoutInSeconds: timeoutInSeconds,
        ));

    infoList._add(testInfo);

    if (condition(testInfo))
      break;
    else
      testInfo = await _next(timeoutInSeconds: timeoutInSeconds);
  }

  lastInfo = infoList.last;
  return infoList;
}