Skip to content

Commit

Permalink
test(client): add test case for rebroadcast on mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
parisholley authored and micimize committed Jun 17, 2020
1 parent 645d462 commit a13eee2
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions packages/graphql/test/graphql_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,26 @@ import './helpers.dart';
class MockLink extends Mock implements Link {}

void main() {
const String readSingle = r'''
query ReadSingle($id: ID!) {
single(id: $id) {
id,
__typename,
name
}
}
''';

const String writeSingle = r'''
mutation WriteSingle($id: ID!, $name: String!) {
updateSingle(id: $id, name: $name) {
id,
__typename,
name
}
}
''';

const String readRepositories = r'''
query ReadRepositories($nRepositories: Int!) {
viewer {
Expand Down Expand Up @@ -178,6 +198,68 @@ void main() {
// test('partially success query with some errors', {});
});
group('mutation', () {
test('query stream notified', () async {
when(
link.request(any),
).thenAnswer(
(_) => Stream.fromIterable(
[
Response(
data: <String, dynamic>{
'single': {'id': '1', '__typename': 'Single', 'name': 'foo'},
},
),
],
),
);

final ObservableQuery observable = await graphQLClientClient.watchQuery(
WatchQueryOptions(document: parseString(readSingle), eagerlyFetchResults: true, variables: {'id': '1'}));

when(
link.request(any),
).thenAnswer(
(_) => Stream.fromIterable(
[
Response(
data: <String, dynamic>{
'updateSingle': {'id': '1', '__typename': 'Single', 'name': 'bar'},
},
),
],
),
);

bool result = false;

observable.stream.listen((event) {
if (event.data != null && event.data['single']['name'] == 'bar') {
result = true;
}
});

final hit = Future.doWhile(() async {
if (result) {
return false;
}

await Future.delayed(Duration(milliseconds: 10));

return true;
});

final variables = {'id': '1', 'name': 'bar'};

final QueryResult response =
await graphQLClientClient.mutate(MutationOptions(document: parseString(writeSingle), variables: variables));

expect(response.data['updateSingle']['name'], variables['name']);

await Future.any([hit, Future.delayed(const Duration(seconds: 3))]);

expect(result, true);
});

test('successful mutation', () async {
final MutationOptions _options = MutationOptions(
document: parseString(addStar),
Expand Down

0 comments on commit a13eee2

Please sign in to comment.