-
Notifications
You must be signed in to change notification settings - Fork 4
/
detail_remote_service_test.dart
174 lines (153 loc) · 5.27 KB
/
detail_remote_service_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
167
168
169
170
171
172
173
174
import 'package:dio/dio.dart';
import 'package:fpdart/fpdart.dart';
import 'package:mocktail/mocktail.dart';
import 'package:now_in_dart_flutter/core/data/github_header.dart';
import 'package:now_in_dart_flutter/core/data/remote_response.dart';
import 'package:now_in_dart_flutter/core/domain/failure.dart';
import 'package:test/test.dart';
import '../../../../helpers/mocks.dart';
import '../../../../helpers/register_multiple_fallback_values.dart';
void main() {
final dio = MockDio();
final response = MockResponse<String>();
final headers = MockHeaders();
final headerCache = MockHeaderCache();
final detailRemoteService = MockDetailRemoteService(
dio: dio,
headerCache: headerCache,
);
setUpAll(() {
registerMultipleFallbackValues([
FakeUri(),
FakeOptions(),
FakeGithubHeader(),
]);
});
group(
'DetailRemoteService |',
() {
const fakeGithubHeader = GithubHeader(
id: 1,
eTag: '12345',
path: '/path',
);
test(
'should instantiate Dio() and HeaderCache() when not injected',
() => expect(MockDetailRemoteService(), isNotNull),
);
group(
'The method `getDetail`',
() {
setUpAll(
() => when(
() => headerCache.getHeader(any(that: isA<String>())),
).thenReturn(Task(() async => fakeGithubHeader)),
);
test(
'should return right of TaskEither<Failure, RemoteResponse<String>>'
' i.e. ModifiedRemoteResponse if the status code is 200',
() async {
when(() => headers.map).thenReturn(
{
'ETag': ['12345'],
},
);
when(() => response.statusCode).thenReturn(200);
when(() => response.data).thenReturn('html');
when(() => response.headers).thenReturn(headers);
when(
() => dio.getUri<String>(
any(that: isA<Uri>()),
options: any(named: 'options', that: isA<Options>()),
),
).thenAnswer((_) async => response);
when(
() => headerCache.saveHeader(any(that: isA<GithubHeader>())),
).thenReturn(Task(() async => unit));
final result =
await detailRemoteService.getDetail(1, '/path').run();
expect(
result,
right<Failure, RemoteResponse<String>>(
const ModifiedRemoteResponse('html'),
),
);
},
);
test(
'should return right of TaskEither<Failure, RemoteResponse<String>>'
' i.e. UnModifiedRemoteResponse if the status code is 304',
() async {
when(() => response.statusCode).thenReturn(304);
when(
() => dio.getUri<String>(
any(that: isA<Uri>()),
options: any(named: 'options', that: isA<Options>()),
),
).thenAnswer((_) async => response);
final result =
await detailRemoteService.getDetail(1, '/path').run();
expect(
result,
right<Failure, RemoteResponse<String>>(
const UnModifiedRemoteResponse(),
),
);
},
);
test(
'should return right of TaskEither<Failure, RemoteResponse<String>>'
' i.e. NoConnectionRemoteResponse if DioException.connectionError '
'is thrown',
() async {
when(
() => dio.getUri<String>(
any(that: isA<Uri>()),
options: any(named: 'options', that: isA<Options>()),
),
).thenThrow(
DioException.connectionError(
requestOptions: RequestOptions(),
reason: '',
),
);
final result =
await detailRemoteService.getDetail(1, '/path').run();
expect(
result,
right<Failure, RemoteResponse<String>>(
const NoConnectionRemoteResponse(),
),
);
},
);
test(
'should return left of TaskEither<Failure, RemoteResponse<String>>'
' i.e. ApiFailure if network request is unsuccessful',
() async {
const errorMessage = 'Error on network request';
when(
() => dio.getUri<String>(
any(that: isA<Uri>()),
options: any(named: 'options', that: isA<Options>()),
),
).thenThrow(Exception());
final result =
await detailRemoteService.getDetail(1, '/path').run();
expect(
result.match(
(failure) {
final isApiFailure = failure is ApiFailure;
return isApiFailure && failure.message == errorMessage;
},
(_) => false,
),
isTrue,
);
},
);
},
);
},
);
}