Created
July 4, 2022 20:06
-
-
Save danahartweg/cbaecf4554e7852212c40a6f4d8d4ab1 to your computer and use it in GitHub Desktop.
User view test - Unit testing Flutter GraphQL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
late MockGraphQLClient mockGraphQLClient; | |
setUp(() { | |
mockGraphQLClient = generateMockGraphQLClient(); | |
}); | |
Widget _buildTestScaffold() { | |
return GraphQLProvider( | |
client: ValueNotifier(mockGraphQLClient), | |
child: MaterialApp( | |
home: Material( | |
child: UserView(_userId), | |
), | |
), | |
); | |
} | |
group('Profile', () { | |
testWidgets('displays a loading indicator when query is loading', | |
(tester) async { | |
final result = generateMockWatchQuery<Query$UserById>(mockGraphQLClient); | |
when(() => result.isLoading).thenReturn(true); | |
await tester.pumpWidget(_buildTestScaffold()); | |
expect(find.byType(CircularProgressIndicator), findsOneWidget); | |
}); | |
testWidgets('renders a message on error', (tester) async { | |
final result = generateMockWatchQuery<Query$UserById>(mockGraphQLClient); | |
when(() => result.isLoading).thenReturn(false); | |
when(() => result.hasException).thenReturn(true); | |
await tester.pumpWidget(_buildTestScaffold()); | |
expect( | |
find.text('There was an issue loading your profile'), findsOneWidget); | |
}); | |
testWidgets('renders the appropriate elements', (tester) async { | |
final result = generateMockWatchQuery<Query$UserById>(mockGraphQLClient); | |
when(() => result.isLoading).thenReturn(false); | |
when(() => result.hasException).thenReturn(false); | |
when(() => result.parsedData).thenReturn( | |
Query$UserById( | |
getUser: Query$UserById$getUser( | |
xid: _userId, | |
email: _email, | |
displayName: _displayName, | |
$__typename: 'user', | |
), | |
$__typename: 'getUser', | |
), | |
); | |
await tester.pumpWidget(_buildTestScaffold()); | |
expect(find.text(_displayName), findsOneWidget); | |
expect(find.text(_email), findsOneWidget); | |
}); | |
}); | |
} | |
const _displayName = 'First Last'; | |
const _email = '[email protected]'; | |
const _userId = '123'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment