Skip to content

Instantly share code, notes, and snippets.

@danahartweg
danahartweg / user_view_test.dart
Created July 4, 2022 20:06
User view test - Unit testing Flutter GraphQL
void main() {
late MockGraphQLClient mockGraphQLClient;
setUp(() {
mockGraphQLClient = generateMockGraphQLClient();
});
Widget _buildTestScaffold() {
return GraphQLProvider(
client: ValueNotifier(mockGraphQLClient),
@danahartweg
danahartweg / user_view.dart
Created July 4, 2022 20:01
User view - Unit testing Flutter GraphQL
class UserView extends HookWidget {
final String _userId;
UserView(this._userId);
@override
Widget build(BuildContext context) {
final query = useQuery$UserById(
Options$Query$UserById(
variables: Variables$Query$UserById(
@danahartweg
danahartweg / user_repository_test.dart
Created July 4, 2022 19:43
User repository test - Unit testing Flutter GraphQL
void main() {
late MockGraphQLClient mockGraphQLClient;
setUp(() {
mockGraphQLClient = generateMockGraphQLClient();
});
group('UserRepository', () {
test('creates a profile', () async {
final result =
@danahartweg
danahartweg / user_repository.dart
Created July 4, 2022 19:31
User repository - Unit testing Flutter GraphQL
class UserRepository {
late final GraphQLClient _graphQLClient;
UserRepository({
GraphQLClient? graphQLClient,
}) {
_graphQLClient = graphQLClient ?? locator<GraphQLClient>();
}
Future<String?> create(String id, String? email) async {
@danahartweg
danahartweg / helpers.dart
Created July 3, 2022 22:00
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(
@danahartweg
danahartweg / helpers.dart
Created July 3, 2022 21:55
Mutation test harness - Unit testing Flutter GraphQL
MockQueryResult<T> generateMockMutation<T>(MockGraphQLClient graphQLClient) {
registerFallbackValue(_FakeMutationOptions<T>());
final result = MockQueryResult<T>();
when(() => graphQLClient.mutate<T>(any())).thenAnswer((_) async => result);
final queryManager = graphQLClient.queryManager;
when(() => queryManager.mutate<T>(any())).thenAnswer((_) async => result);
return result;
@danahartweg
danahartweg / helpers.dart
Created July 2, 2022 18:34
Query test harness - Unit testing Flutter GraphQL
MockQueryResult<T> generateMockQuery<T>(MockGraphQLClient graphQLClient) {
registerFallbackValue(_FakeQueryOptions<T>());
final result = MockQueryResult<T>();
when(() => graphQLClient.query<T>(any())).thenAnswer((_) async => result);
final queryManager = graphQLClient.queryManager;
when(() => queryManager.query<T>(any())).thenAnswer((_) async => result);
return result;
@danahartweg
danahartweg / helpers.dart
Last active July 2, 2022 17:55
Client test harness - Unit testing Flutter GraphQL
MockGraphQLClient generateMockGraphQLClient() {
final graphQLClient = MockGraphQLClient();
final queryManager = MockQueryManager();
when(() => graphQLClient.defaultPolicies).thenReturn(DefaultPolicies());
when(() => graphQLClient.queryManager).thenReturn(queryManager);
return graphQLClient;
}
@danahartweg
danahartweg / user-create.test.ts
Last active July 18, 2021 14:43
Create user Cloud Function test
import { auth, initializeApp } from 'firebase-admin';
import { waitForCloudFunctionExecution } from '@helpers/wait';
import { AdminFirestore } from '@test-helpers/types';
import { Collections } from '@test-helpers/constants';
import { getAdminApp, setup, teardown } from '@test-helpers/firestore';
describe('create', () => {
const email = '[email protected]';
const displayName = 'Test User';
@danahartweg
danahartweg / user-create.ts
Created July 18, 2021 14:40
Create user Cloud Function - Unit testing Cloud Firestore
import { auth } from 'firebase-functions';
import { getFirestore } from '../admin';
export const create = auth.user().onCreate(async (userRecord) => {
const db = getFirestore();
return db
.collection('users')
.doc(userRecord.uid)