-
-
Notifications
You must be signed in to change notification settings - Fork 626
/
fetch_policy_test.dart
166 lines (148 loc) · 4.75 KB
/
fetch_policy_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import 'package:test/test.dart';
import 'package:mockito/mockito.dart';
import 'package:graphql/client.dart';
import 'package:gql/language.dart';
import './helpers.dart';
void main() {
const String readRepositories = r'''
query ReadRepositories($nRepositories: Int!) {
viewer {
repositories(last: $nRepositories) {
nodes {
__typename
id
name
viewerHasStarred
}
}
}
}
''';
readRepositoryData({withTypenames = true, withIds = true}) {
return {
'viewer': {
'repositories': {
'nodes': [
{
if (withIds) 'id': 'MDEwOlJlcG9zaXRvcnkyNDgzOTQ3NA==',
'name': 'pq',
'viewerHasStarred': false
},
{
if (withIds) 'id': 'MDEwOlJlcG9zaXRvcnkzMjkyNDQ0Mw==',
'name': 'go-evercookie',
'viewerHasStarred': false
},
{
if (withIds) 'id': 'MDEwOlJlcG9zaXRvcnkzNTA0NjgyNA==',
'name': 'watchbot',
'viewerHasStarred': false
},
]
.map((map) =>
withTypenames ? {'__typename': 'Repository', ...map} : map)
.toList(),
},
},
};
}
late MockLink link;
late GraphQLClient client;
group('FetchPolicy', () {
setUp(() {
link = MockLink();
client = GraphQLClient(
cache: getTestCache(),
link: link,
);
});
group('query', () {
// TODO cacheFirst code path: Return result from cache. Only fetch from network if cached result is not available.
// TODO cacheOnly code path: Return result from cache if available, fail otherwise.
// TODO noCache code path: Return result from network, fail if network call doesn't succeed, don't save to cache.
// TODO networkOnly code path: Return result from network, fail if network call doesn't succeed, save to cache.
test('switch to cacheOnly returns cached data', () async {
final _options = QueryOptions(
fetchPolicy: FetchPolicy.cacheAndNetwork,
document: parseString(readRepositories),
variables: <String, dynamic>{
'nRepositories': 42,
},
);
final repoData = readRepositoryData(withTypenames: true);
when(
link.request(any),
).thenAnswer(
(_) => Stream.fromIterable([
Response(data: repoData),
]),
);
final QueryResult r = await client.query(_options);
verify(
link.request(
Request(
operation: Operation(
document: parseString(readRepositories),
//operationName: 'ReadRepositories',
),
variables: <String, dynamic>{
'nRepositories': 42,
},
context: Context(),
),
),
);
expect(r.exception, isNull);
expect(r.data, equals(repoData));
final QueryResult cacheResult = await client.query(QueryOptions(
fetchPolicy: FetchPolicy.cacheOnly,
document: parseString(readRepositories),
variables: <String, dynamic>{
'nRepositories': 42,
},
));
expect(cacheResult.exception, isNull);
expect(cacheResult.data, equals(repoData));
});
test('cacheAndNetwork returns from cache (if exists)', () async {
final _options = QueryOptions(
fetchPolicy: FetchPolicy.cacheAndNetwork,
document: parseString(readRepositories),
variables: <String, dynamic>{
'nRepositories': 42,
},
);
final repoData = readRepositoryData(withTypenames: true);
when(
link.request(any),
).thenAnswer(
(_) => Stream.fromIterable([
Response(data: repoData),
]),
);
final QueryResult r = await client.query(_options);
verify(
link.request(
Request(
operation: Operation(
document: parseString(readRepositories),
//operationName: 'ReadRepositories',
),
variables: <String, dynamic>{
'nRepositories': 42,
},
context: Context(),
),
),
);
expect(r.exception, isNull);
expect(r.data, equals(repoData));
expect(r.source, QueryResultSource.network);
final QueryResult cacheResult = await client.query(_options);
expect(cacheResult.exception, isNull);
expect(cacheResult.data, equals(repoData));
expect(cacheResult.source, equals(QueryResultSource.cache));
});
});
});
}