Created
July 4, 2022 19:43
-
-
Save danahartweg/eb18bd8877ce4c3031e43bdc8da2b5f0 to your computer and use it in GitHub Desktop.
User repository 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(); | |
}); | |
group('UserRepository', () { | |
test('creates a profile', () async { | |
final result = | |
generateMockMutation<Mutation$CreateUser>(mockGraphQLClient); | |
when(() => result.hasException).thenReturn(false); | |
when(() => result.parsedData).thenReturn( | |
Mutation$CreateUser( | |
addUser: Mutation$CreateUser$addUser( | |
user: [ | |
Mutation$CreateUser$addUser$user( | |
xid: _userId, | |
email: _email, | |
$__typename: 'user', | |
) | |
], | |
$__typename: 'user', | |
), | |
$__typename: 'addUser', | |
), | |
); | |
final repository = UserRepository(graphQLClient: mockGraphQLClient); | |
final createdUserId = await repository.create(_userId, _email); | |
expect(createdUserId, _userId); | |
final executedMutation = verify( | |
() => mockGraphQLClient.mutate<Mutation$CreateUser>(captureAny())) | |
..called(1); | |
expect( | |
executedMutation.captured.last, | |
isA<Options$Mutation$CreateUser>().having( | |
(s) => s.variables, | |
'variables', | |
{ | |
'id': _userId, | |
'email': _email, | |
}, | |
), | |
); | |
}); | |
}); | |
} | |
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