Skip to content

Instantly share code, notes, and snippets.

@danahartweg
Created July 3, 2022 22:00
Show Gist options
  • Save danahartweg/a9245cc46fef9256ce1faf0094f445ad to your computer and use it in GitHub Desktop.
Save danahartweg/a9245cc46fef9256ce1faf0094f445ad to your computer and use it in GitHub Desktop.
Watch query test harness - Unit testing Flutter GraphQL
MockQueryResult<T> generateMockWatchQuery<T>(MockGraphQLClient graphQLClient) {
registerFallbackValue(_FakeWatchQueryOptions<T>());
final query = MockObservableQuery<T>();
final result = MockQueryResult<T>();
when(query.close).thenReturn(QueryLifecycle.closed);
when(() => query.stream).thenAnswer((_) => Stream.value(result));
when(() => query.latestResult).thenReturn(result);
when(
() => query.onData(
any(),
removeAfterInvocation: any(named: 'removeAfterInvocation'),
),
).thenReturn(() {});
when(() => graphQLClient.watchQuery<T>(any())).thenReturn(query);
final queryManager = graphQLClient.queryManager;
when(() => queryManager.watchQuery<T>(any())).thenReturn(query);
return result;
}
class MockQueryResult<T> extends Mock implements QueryResult<T> {}
class _FakeWatchQueryOptions<T> extends Fake implements WatchQueryOptions<T> {}
class MockObservableQuery<T> extends Mock implements ObservableQuery<T> {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment