@@ -8,22 +8,24 @@ import 'package:amplify_analytics_pinpoint_dart/src/impl/analytics_client/event_
8
8
import 'package:amplify_flutter/amplify_flutter.dart' ;
9
9
import 'package:amplify_push_notifications_pinpoint/src/pinpoint_provider.dart' ;
10
10
import 'package:flutter_test/flutter_test.dart' ;
11
- import 'package:mockito/annotations.dart' ;
12
- import 'package:mockito/mockito.dart' ;
11
+ import 'package:mocktail/mocktail.dart' ;
13
12
14
- import 'pinpoint_provider_test.mocks.dart' ;
15
13
import 'test_data/fake_notification_messges.dart' ;
16
14
17
- @GenerateMocks (
18
- [
19
- AmplifyAuthProviderRepository ,
20
- AWSIamAmplifyAuthProvider ,
21
- UserProfile ,
22
- AnalyticsClient ,
23
- EndpointClient ,
24
- EventClient ,
25
- ],
26
- )
15
+ class MockAmplifyAuthProviderRepository extends Mock
16
+ implements AmplifyAuthProviderRepository {}
17
+
18
+ class MockAWSIamAmplifyAuthProvider extends Mock
19
+ implements AWSIamAmplifyAuthProvider {}
20
+
21
+ class MockUserProfile extends Mock implements UserProfile {}
22
+
23
+ class MockAnalyticsClient extends Mock implements AnalyticsClient {}
24
+
25
+ class MockEndpointClient extends Mock implements EndpointClient {}
26
+
27
+ class MockEventClient extends Mock implements EventClient {}
28
+
27
29
void main () {
28
30
TestWidgetsFlutterBinding .ensureInitialized ();
29
31
final pinpointProvider = PinpointProvider ();
@@ -35,9 +37,24 @@ void main() {
35
37
final awsIamAmplifyAuthProvider = MockAWSIamAmplifyAuthProvider ();
36
38
final mockAnalyticsClient = MockAnalyticsClient ();
37
39
group ('PinpointProvider' , () {
40
+ setUpAll (() {
41
+ registerFallbackValue (
42
+ const AmplifyAuthProviderToken <AmplifyAuthProvider >('mock-token' ),
43
+ );
44
+ registerFallbackValue (
45
+ const AWSCredentialsProvider (
46
+ AWSCredentials (
47
+ 'accessKeyId' ,
48
+ 'secretAccessKey' ,
49
+ ),
50
+ ),
51
+ );
52
+ });
38
53
test ('init fails when retrieving an Auth provider was not successful' , () {
39
- when (mockAmplifyAuthProviderRepository.getAuthProvider (any))
40
- .thenReturn (null );
54
+ when (
55
+ () => mockAmplifyAuthProviderRepository
56
+ .getAuthProvider <AmplifyAuthProvider >(any ()),
57
+ ).thenReturn (null );
41
58
expect (
42
59
() async => pinpointProvider.init (
43
60
config: notificationsPinpointConfig,
@@ -72,28 +89,32 @@ void main() {
72
89
73
90
test ('identifyUser should throw exception if the underlying call throws' ,
74
91
() async {
75
- when (mockAmplifyAuthProviderRepository.getAuthProvider (any))
76
- .thenReturn (awsIamAmplifyAuthProvider);
77
92
when (
78
- mockAnalyticsClient.init (
79
- pinpointAppId: anyNamed ('pinpointAppId' ),
80
- region: anyNamed ('region' ),
81
- authProvider: anyNamed ('authProvider' ),
93
+ () => mockAmplifyAuthProviderRepository.getAuthProvider (
94
+ APIAuthorizationType .iam.authProviderToken,
95
+ ),
96
+ ).thenReturn (awsIamAmplifyAuthProvider);
97
+ when (
98
+ () => mockAnalyticsClient.init (
99
+ pinpointAppId: any (named: 'pinpointAppId' ),
100
+ region: any (named: 'region' ),
101
+ authProvider: any (named: 'authProvider' ),
82
102
),
83
103
).thenAnswer ((realInvocation) async {});
84
104
85
105
final mockEndpointClient = MockEndpointClient ();
86
106
87
107
when (
88
- mockAnalyticsClient.endpointClient,
108
+ () => mockAnalyticsClient.endpointClient,
89
109
).thenReturn (mockEndpointClient);
90
110
91
111
await pinpointProvider.init (
92
112
config: notificationsPinpointConfig,
93
113
authProviderRepo: mockAmplifyAuthProviderRepository,
94
114
analyticsClient: mockAnalyticsClient,
95
115
);
96
- when (mockEndpointClient.setUser (any, any)).thenThrow (Exception ());
116
+ when (() => mockEndpointClient.setUser (any (), any ()))
117
+ .thenThrow (Exception ());
97
118
expect (
98
119
pinpointProvider.identifyUser (
99
120
userId: 'userId' ,
@@ -135,16 +156,32 @@ void main() {
135
156
});
136
157
137
158
group ('Happy path test' , () {
159
+ setUpAll (() {
160
+ registerFallbackValue (
161
+ const AmplifyAuthProviderToken <AmplifyAuthProvider >('mock-token' ),
162
+ );
163
+ registerFallbackValue (
164
+ const AWSCredentialsProvider (
165
+ AWSCredentials (
166
+ 'accessKeyId' ,
167
+ 'secretAccessKey' ,
168
+ ),
169
+ ),
170
+ );
171
+ });
138
172
TestWidgetsFlutterBinding .ensureInitialized ();
139
173
140
174
test ('init should run successfully' , () async {
141
- when (mockAmplifyAuthProviderRepository.getAuthProvider (any))
142
- .thenReturn (awsIamAmplifyAuthProvider);
143
175
when (
144
- mockAnalyticsClient.init (
145
- pinpointAppId: anyNamed ('pinpointAppId' ),
146
- region: anyNamed ('region' ),
147
- authProvider: anyNamed ('authProvider' ),
176
+ () => mockAmplifyAuthProviderRepository.getAuthProvider (
177
+ APIAuthorizationType .iam.authProviderToken,
178
+ ),
179
+ ).thenReturn (awsIamAmplifyAuthProvider);
180
+ when (
181
+ () => mockAnalyticsClient.init (
182
+ pinpointAppId: any (named: 'pinpointAppId' ),
183
+ region: any (named: 'region' ),
184
+ authProvider: any (named: 'authProvider' ),
148
185
),
149
186
).thenAnswer ((realInvocation) async {});
150
187
expect (
@@ -158,20 +195,26 @@ void main() {
158
195
});
159
196
160
197
test ('identifyUser should run successfully' , () async {
161
- when (mockAmplifyAuthProviderRepository.getAuthProvider (any))
162
- .thenReturn (awsIamAmplifyAuthProvider);
163
198
when (
164
- mockAnalyticsClient.init (
165
- pinpointAppId: anyNamed ('pinpointAppId' ),
166
- region: anyNamed ('region' ),
167
- authProvider: anyNamed ('authProvider' ),
199
+ () => mockAmplifyAuthProviderRepository.getAuthProvider (
200
+ APIAuthorizationType .iam.authProviderToken,
201
+ ),
202
+ ).thenReturn (awsIamAmplifyAuthProvider);
203
+ when (
204
+ () => mockAnalyticsClient.init (
205
+ pinpointAppId: any (named: 'pinpointAppId' ),
206
+ region: any (named: 'region' ),
207
+ authProvider: any (named: 'authProvider' ),
168
208
),
169
209
).thenAnswer ((realInvocation) async {});
170
210
171
211
final mockEndpointClient = MockEndpointClient ();
212
+ when (() => mockEndpointClient.setUser (any (), any ()))
213
+ .thenAnswer ((_) async => {});
214
+ when (mockEndpointClient.updateEndpoint).thenAnswer ((_) async => {});
172
215
173
216
when (
174
- mockAnalyticsClient.endpointClient,
217
+ () => mockAnalyticsClient.endpointClient,
175
218
).thenReturn (mockEndpointClient);
176
219
177
220
await pinpointProvider.init (
@@ -187,27 +230,33 @@ void main() {
187
230
),
188
231
completes,
189
232
);
190
- verify (mockEndpointClient.setUser (any, any));
233
+ verify (() => mockEndpointClient.setUser (any () , any () ));
191
234
});
192
235
193
236
test ('registerDevice should run successfully' , () async {
194
- when (mockAmplifyAuthProviderRepository.getAuthProvider (any))
195
- .thenReturn (awsIamAmplifyAuthProvider);
196
237
when (
197
- mockAnalyticsClient.init (
198
- pinpointAppId: anyNamed ('pinpointAppId' ),
199
- region: anyNamed ('region' ),
200
- authProvider: anyNamed ('authProvider' ),
238
+ () => mockAmplifyAuthProviderRepository.getAuthProvider (
239
+ APIAuthorizationType .iam.authProviderToken,
240
+ ),
241
+ ).thenReturn (awsIamAmplifyAuthProvider);
242
+ when (
243
+ () => mockAnalyticsClient.init (
244
+ pinpointAppId: any (named: 'pinpointAppId' ),
245
+ region: any (named: 'region' ),
246
+ authProvider: any (named: 'authProvider' ),
201
247
),
202
248
).thenAnswer ((realInvocation) async {});
203
249
204
250
final mockEndpointClient = MockEndpointClient ();
251
+ when (() => mockEndpointClient.setUser (any (), any ()))
252
+ .thenAnswer ((_) async => {});
253
+ when (mockEndpointClient.updateEndpoint).thenAnswer ((_) async => {});
205
254
206
255
when (
207
- mockAnalyticsClient.endpointClient,
256
+ () => mockAnalyticsClient.endpointClient,
208
257
).thenReturn (mockEndpointClient);
209
258
210
- expect (
259
+ await expectLater (
211
260
pinpointProvider.init (
212
261
config: notificationsPinpointConfig,
213
262
authProviderRepo: mockAmplifyAuthProviderRepository,
@@ -222,27 +271,37 @@ void main() {
222
271
),
223
272
completes,
224
273
);
225
- verify (mockEndpointClient.updateEndpoint () );
274
+ verify (mockEndpointClient.updateEndpoint);
226
275
});
227
276
228
277
test ('recordEvent should run successfully' , () async {
229
- when (mockAmplifyAuthProviderRepository.getAuthProvider (any))
230
- .thenReturn (awsIamAmplifyAuthProvider);
231
278
when (
232
- mockAnalyticsClient.init (
233
- pinpointAppId: anyNamed ('pinpointAppId' ),
234
- region: anyNamed ('region' ),
235
- authProvider: anyNamed ('authProvider' ),
279
+ () => mockAmplifyAuthProviderRepository.getAuthProvider (
280
+ APIAuthorizationType .iam.authProviderToken,
281
+ ),
282
+ ).thenReturn (awsIamAmplifyAuthProvider);
283
+ when (
284
+ () => mockAnalyticsClient.init (
285
+ pinpointAppId: any (named: 'pinpointAppId' ),
286
+ region: any (named: 'region' ),
287
+ authProvider: any (named: 'authProvider' ),
236
288
),
237
289
).thenAnswer ((realInvocation) async {});
238
290
239
291
final mockEventClient = MockEventClient ();
292
+ when (
293
+ () => mockEventClient.recordEvent (
294
+ eventType: any (named: 'eventType' ),
295
+ session: any (named: 'session' ),
296
+ properties: any (named: 'properties' ),
297
+ ),
298
+ ).thenAnswer ((_) async => {});
240
299
241
300
when (
242
- mockAnalyticsClient.eventClient,
301
+ () => mockAnalyticsClient.eventClient,
243
302
).thenReturn (mockEventClient);
244
303
245
- expect (
304
+ await expectLater (
246
305
pinpointProvider.init (
247
306
config: notificationsPinpointConfig,
248
307
authProviderRepo: mockAmplifyAuthProviderRepository,
@@ -251,7 +310,7 @@ void main() {
251
310
completes,
252
311
);
253
312
254
- expect (
313
+ await expectLater (
255
314
pinpointProvider.recordNotificationEvent (
256
315
eventType: PinpointEventType .foregroundMessageReceived,
257
316
notification:
@@ -260,10 +319,10 @@ void main() {
260
319
completes,
261
320
);
262
321
verify (
263
- mockEventClient.recordEvent (
322
+ () => mockEventClient.recordEvent (
264
323
eventType:
265
324
'${PinpointEventSource .campaign .name }.${PinpointEventType .foregroundMessageReceived .name }' ,
266
- properties: anyNamed ( 'properties' ),
325
+ properties: any (named : 'properties' ),
267
326
),
268
327
);
269
328
});
0 commit comments