whenObserve<State> function

void whenObserve<State>(
  1. MockStore<State> store, {
  2. required dynamic input(),
  3. Duration delay = const Duration(),
  4. List<Triple<State>> triples = const [],
  5. State? initialState,
})

Implementation

void whenObserve<State>(
  MockStore<State> store, {
  required Function() input,
  Duration delay = const Duration(),
  List<Triple<State>> triples = const [],
  State? initialState,
}) {
  if (initialState != null) {
    when(() => store.triple).thenReturn(
      Triple<State>(
        state: initialState,
      ),
    );
    when(() => store.state).thenReturn(initialState);
    when(() => store.error).thenReturn(null);
    when(() => store.isLoading).thenReturn(false);
  }

  when(input).thenAnswer((invocation) async {
    for (final triple in triples) {
      when(() => store.triple).thenReturn(
        triple,
      );
      if (triple.event == TripleEvent.state) {
        when(() => store.state).thenReturn(
          triple.state,
        );
      } else if (triple.event == TripleEvent.loading) {
        when(() => store.isLoading).thenReturn(
          triple.isLoading,
        );
      } else if (triple.event == TripleEvent.error) {
        when(() => store.error).thenReturn(
          triple.error,
        );
      }
      store.dispatcherTriple(triple);
      await Future.delayed(delay);
    }
  });
}