-
Notifications
You must be signed in to change notification settings - Fork 1
/
number_trivia_bloc_test.dart
232 lines (207 loc) · 7.38 KB
/
number_trivia_bloc_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import 'package:tddcleancourse/core/error/failures.dart';
import 'package:tddcleancourse/core/usecases/usecase.dart';
import 'package:tddcleancourse/core/util/input_converter.dart';
import 'package:tddcleancourse/features/number_trivia/domain/entities/number_trivia.dart';
import 'package:tddcleancourse/features/number_trivia/domain/usecases/get_concrete_number_trivia.dart';
import 'package:tddcleancourse/features/number_trivia/domain/usecases/get_random_number_trivia.dart';
import 'package:dartz/dartz.dart';
import 'package:mocktail/mocktail.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:tddcleancourse/features/number_trivia/presentation/bloc/number_trivia_bloc.dart';
class MockGetConcreteNumberTrivia extends Mock
implements GetConcreteNumberTrivia {}
class MockGetRandomNumberTrivia extends Mock implements GetRandomNumberTrivia {}
class MockInputConverter extends Mock implements InputConverter {}
class FakeParams extends Fake implements Params {}
class FakeNoParams extends Fake implements NoParams {}
void main() {
late NumberTriviaBloc bloc;
late MockGetConcreteNumberTrivia mockGetConcreteNumberTrivia;
late MockGetRandomNumberTrivia mockGetRandomNumberTrivia;
late MockInputConverter mockInputConverter;
setUp(() {
registerFallbackValue(FakeParams());
registerFallbackValue(FakeNoParams());
mockGetConcreteNumberTrivia = MockGetConcreteNumberTrivia();
mockGetRandomNumberTrivia = MockGetRandomNumberTrivia();
mockInputConverter = MockInputConverter();
bloc = NumberTriviaBloc(
concrete: mockGetConcreteNumberTrivia,
random: mockGetRandomNumberTrivia,
inputConverter: mockInputConverter,
);
});
test('initialState should be Empty', () {
// assert
expect(bloc.initialState, equals(Empty()));
});
group('GetTriviaForConcreteNumber', () {
const tNumberString = '1';
const tNumberParsed = 1;
const tNumberTrivia = NumberTrivia(number: 1, text: 'test trivia');
void setUpMockInputConverterSuccess() =>
when(() => mockInputConverter.stringToUnsignedInteger(any()))
.thenReturn(const Right(tNumberParsed));
void setUpMockGetConcreteTriviaSuccess() =>
when(() => mockGetConcreteNumberTrivia(any()))
.thenAnswer((_) async => const Right(tNumberTrivia));
test(
'should call the InputConverter to validate and convert the string to an unsigned integer',
() async {
// arrange
setUpMockInputConverterSuccess();
setUpMockGetConcreteTriviaSuccess();
// act
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
await untilCalled(
() => mockInputConverter.stringToUnsignedInteger(any()));
// assert
verify(() => mockInputConverter.stringToUnsignedInteger(tNumberString));
},
);
test(
'should emit [Error] when the input is invalid',
() async {
// arrange
when(() => mockInputConverter.stringToUnsignedInteger(any()))
.thenReturn(Left(InvalidInputFailure()));
// assert later
final expected = [
Error(message: INVALID_INPUT_FAILURE_MESSAGE),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
},
);
test(
'should get data from the concrete use case',
() async {
// arrange
setUpMockInputConverterSuccess();
setUpMockGetConcreteTriviaSuccess();
// act
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
await untilCalled(() => mockGetConcreteNumberTrivia(any()));
// assert
verify(() =>
mockGetConcreteNumberTrivia(const Params(number: tNumberParsed)));
},
);
test(
'should emit [Loading, Loaded] when data is gotten successfully',
() async {
// arrange
setUpMockInputConverterSuccess();
setUpMockGetConcreteTriviaSuccess();
// assert later
final expected = [
Loading(),
Loaded(trivia: tNumberTrivia),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
},
);
test(
'should emit [Loading, Error] when getting data fails',
() async {
// arrange
setUpMockInputConverterSuccess();
when(() => mockGetConcreteNumberTrivia(any()))
.thenAnswer((_) async => Left(ServerFailure()));
// assert later
final expected = [
Loading(),
Error(message: SERVER_FAILURE_MESSAGE),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
},
);
test(
'should emit [Loading, Error] with a proper message for the error when getting data fails',
() async {
// arrange
setUpMockInputConverterSuccess();
when(() => mockGetConcreteNumberTrivia(any()))
.thenAnswer((_) async => Left(CacheFailure()));
// assert later
final expected = [
Loading(),
Error(message: CACHE_FAILURE_MESSAGE),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(const GetTriviaForConcreteNumber(tNumberString));
},
);
});
group('GetTriviaForRandomNumber', () {
const tNumberTrivia = NumberTrivia(number: 1, text: 'test trivia');
void setUpMockGetRandomTriviaSuccess() =>
when(() => mockGetRandomNumberTrivia(any()))
.thenAnswer((_) async => const Right(tNumberTrivia));
test(
'should get data from the random use case',
() async {
// arrange
setUpMockGetRandomTriviaSuccess();
// act
bloc.add(GetTriviaForRandomNumber());
await untilCalled(() => mockGetRandomNumberTrivia(any()));
// assert
verify(() => mockGetRandomNumberTrivia(NoParams()));
},
);
test(
'should emit [Loading, Loaded] when data is gotten successfully',
() async {
// arrange
setUpMockGetRandomTriviaSuccess();
// assert later
final expected = [
Loading(),
Loaded(trivia: tNumberTrivia),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(GetTriviaForRandomNumber());
},
);
test(
'should emit [Loading, Error] when getting data fails',
() async {
// arrange
when(() => mockGetRandomNumberTrivia(any()))
.thenAnswer((_) async => Left(ServerFailure()));
// assert later
final expected = [
Loading(),
Error(message: SERVER_FAILURE_MESSAGE),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(GetTriviaForRandomNumber());
},
);
test(
'should emit [Loading, Error] with a proper message for the error when getting data fails',
() async {
// arrange
when(() => mockGetRandomNumberTrivia(any()))
.thenAnswer((_) async => Left(CacheFailure()));
// assert later
final expected = [
Loading(),
Error(message: CACHE_FAILURE_MESSAGE),
];
expectLater(bloc.stream, emitsInOrder(expected));
// act
bloc.add(GetTriviaForRandomNumber());
},
);
});
}